1

I have a Java Spring application using:

spring.jpa.hibernate.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.show_sql=true

in my application.properties file.

I use logback/logstash for all of my logs, but the Hibernate logs are not going into the appender. I have this in my logback.xml file:

    <logger name="org.hibernate.type" level="TRACE">
        <appender-ref ref="json" />
    </logger>

With my appender (json) being:

    <appender name="json" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">

            <timestampPattern>yyyy-MM-dd'T'HH:mm:ss.SSSZ</timestampPattern>
            <includeMdcKeyName>RID</includeMdcKeyName>
            <mdcKeyFieldName>RID=rid</mdcKeyFieldName>

            <fieldNames>
                <timestamp>timestamp</timestamp>
                <level>level</level>
                <levelValue>[ignore]</levelValue>
                <logger>logger</logger>
                <version>[ignore]</version>
            </fieldNames>
        </encoder>
    </appender>

So a "normal" log looks like this: (replaced the actual stuff with {...})

{"timestamp":"2023-04-20T16:11:25.133-0600","message":"My normal log message","logger":"{...}","thread_name":"{...}","level":"INFO","rid":"{...}"}

However my Hibernate logs still look like:

Hibernate: SELECT * FROM my_table mt WHERE mt.name = ?

As you can see it does not seem to go to my appender for formatting. It also doesn't show the values, just ?

How do I fix this so my Hibernate logs look more like:

{"timestamp":"2023-04-20T16:11:25.133-0600","message":"Hibernate: SELECT * FROM my_table mt WHERE mt.name = ?","logger":"{...}","thread_name":"{...}","level":"INFO","rid":"{...}"}

but also with the actual values, not "?" ? Thanks for the help!

Ebad
  • 131
  • 11

1 Answers1

0

The property hibernate.show_sql does not direct its output through any logging framework. It always sends its output directly to the console.

Instead, you should use the log category org.hibernate.SQL if you want the SQL logged via a logging framework.

Gavin King
  • 3,182
  • 1
  • 13
  • 11