How to enable Hibernate logs in Spring Boot

In Spring boot, hibernate logs should be enabled to debug the database connection and SQL query execution. By default hibernates logs are disabled in spring boot application. The hibernate logs can be enabled using the spring boot configurations. There are different configuration parameters that logs different features of hibernate. For example, print query strings with or without parameter values, hibernate cache logs, hibernate transactions etc.

There are two ways to enable hibernate logs in the spring boot application. The JPA spring boot module will allow the logging of the underlying ORM tools. Since hibernate is the default ORM tool used for spring boot, JPA logs will enable hibernate logs. The second choice is to allow hibernate logs directly using the logger. The hibernate classes log the queries using the loggers.

Hibernate logs can be printed in the spring boot console window. Hibernate logs must be forwarded to a log file so that they can be accessed later. The configuration of the log file must be added along with the configuration of the hibernate log.



Default Hibernate Logs

If the spring boot jpa and mysql are configured in the spring boot framework, the logs will be printed at the start of the application by default. These logs will be seen when the hibernate module is initialised in the spring boot application.

2020-11-10 15:13:48.804  INFO 89641 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-11-10 15:13:48.841  INFO 89641 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-11-10 15:13:48.846  WARN 89641 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-11-10 15:13:49.025  INFO 89641 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-11-10 15:13:49.112  INFO 89641 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-11-10 15:13:49.317  INFO 89641 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-10 15:13:49.319  INFO 89641 --- [           main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-11-10 15:13:49.556  INFO 89641 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-11-10 15:13:49.562  INFO 89641 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-11-10 15:13:49.712  INFO 89641 --- [           main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!


Enable Hibernate Log using JPA

The hibernate logs can be enabled using the below JPA configurations in the application.properties files. The first configuration enables the hibernate sql logs. The second configuration will beautify the sql query that reveals the user-friendly query format to be understood.

The JPA configuration will log the SQL queries. The parameterized query logs the query as it is. The values of the parameter will not be logged. This configuration would be used if the application deals with confidential data that should not be used.

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

hibernate logs

Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into employee (name, id) values (?, ?)

hibernate logs with formatted sql

Hibernate: 
    select
        next_val as id_val 
    from
        hibernate_sequence for update
            
Hibernate: 
    update
        hibernate_sequence 
    set
        next_val= ? 
    where
        next_val=?
Hibernate: 
    insert 
    into
        employee
        (name, id) 
    values
        (?, ?)


Enable Hibernate Log – SQL query with Parameter value

If the hibernate logger configuration will enable to log the sql query string with parameter values. The following configuration will enable the hibernate logs. The log will show the hibernate class from where the log is written.

The hibernate query will be printed using the first configuration. The second configuration will show all values of the parameters in the same order. These configurations will be used in the development and debug mode. These configurations are not recommended for use in the production environment or in applications dealing with sensitive data.

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

Hibernate log

2020-11-10 15:40:24.582 DEBUG 92677 --- [nio-8080-exec-1] org.hibernate.SQL                        : select next_val as id_val from hibernate_sequence for update
2020-11-10 15:40:24.592 DEBUG 92677 --- [nio-8080-exec-1] org.hibernate.SQL                        : update hibernate_sequence set next_val= ? where next_val=?
2020-11-10 15:40:24.608 DEBUG 92677 --- [nio-8080-exec-1] org.hibernate.SQL                        : insert into employee (name, id) values (?, ?)
2020-11-10 15:40:24.610 TRACE 92677 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [test]
2020-11-10 15:40:24.611 TRACE 92677 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [BIGINT] - [1]


Enable Hibernate Log – Using JdbcTemplate

If the Jdbc template is used in the spring boot application to connect the database and run the database queries, t he following configuration should be enabled to log in the queries. Custom queries used with the Jdbc template will be logged in using this configuration.

application.properties

logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG
logging.level.org.springframework.jdbc.core.StatementCreatorUtils=TRACE


Enable Hibernate Log to File

By default, hibernate logs are printed in the spring boot console. Using the configuration below, hibernate logs can be moved to the files. Configuring the log file name would transfer all the log of the spring boot console to the file. The second configuration would remove logs from the console window.

application.properties

logging.file.name=hibernate_log_file_name.log
logging.pattern.console=

In the above spring boot configuration, all the log statements will be moved to the configured log file. you can not differentiate the hibernate log from the other log statements. If you want to log the hibernate logs in a different log file, the logbook.xml or underlying log configuration file must be used to configure the log file names in the spring boot application.



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *