hibernateEnable/Disable SQL log


Logging these queries is slow, even slower than Hibernate usually is. It also uses up a massive amount of log space. Do not use logging in scenarios where performance is required. Use this only when testing the queries that Hibernate actually generates.

Using a logging config file

In the logging configuration file of your choice set the logging of the following packages to the levels shown.:

# log the sql statement
# log the parameters

There will probably be some logger specific prefixes that are required.

Log4j config:


Spring Boot application.properties:


Logback logback.xml:

<logger name="org.hibernate.SQL" level="DEBUG"/>
<logger name="org.hibernate.type" level="TRACE"/>

Using Hibernate properties

This will show you the generated SQL, but will not show you the values contained within the queries.

<bean id="sessionFactory"
    <property name="hibernateProperties">
            <!-- show the sql without the parameters -->
            <prop key="hibernate.show_sql">true</prop>
            <!-- format the sql nice -->
            <prop key="hibernate.format_sql">true</prop>
            <!-- show the hql as comment -->
            <prop key="use_sql_comments">true</prop>

Enable/Disable SQL log in debug

Some applications that use Hibernate generate a huge amount of SQL when the application is started. Sometimes it's better to enable/disable the SQL log in specific points when debugging.

To enable, just run this code in your IDE when you are debugging the aplication:


To disable: