I am trying to set the hibernate logging of SQL statements in my springboot and springdata application.
I setting the following properties in the application.properties file:
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
But somehow they are not getting picked up and no sql logs are coming. Have tried setting spring.jpa.show-sql property also, but does'nt seem to work.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>in.abcd</groupId>
<artifactId>spring-boot-commons</artifactId>
<version>1.0.10</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</exclusion>
<exclusion>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.1.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
Where you declare your entity manager bean, add the following properties.
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql=true
Or in Java annotations
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(HIBERNATE_ENTITIES);
log.info("Created entity manager successfully");
JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
//Properties to show SQL format of tables on deploy
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.show_sql", true); // Mark as true to log hibernate queries
jpaProperties.put("hibernate.format_sql", true); // Mark as true to log hibernate queries
entityManagerFactoryBean.setJpaProperties(jpaProperties);
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
return entityManagerFactoryBean;
}
Edit: Since you aren't using an entity manager, you can do this directly on your Configuration
private SessionFactory createSessionFactory(String options) {
Configuration configuration = new Configuration();
configuration.setProperty("spring.jpa.properties.hibernate.format_sql", "true");
configuration.setProperty("hibernate.show_sql", "true");
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties());
SessionFactory sessionFactory = `enter code here`configuration.buildSessionFactory(builder.build());
return sessionFactory;
}```
Using the logger this should suffice
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
Or, using spring.jpa
properties, you could use them like this:
spring.jpa.show-sql = true
Additionally, to beautify or pretty print the SQL, we can add:
spring.jpa.properties.hibernate.format_sql=true
With Hibernate 6 following config can be used in SpringBoot3:
logging.level.org.hibernate.orm.jdbc.bind=trace
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.