• Ask a Question
  • Create a Poll
150
    Ask a Question
    Cancel
    150
    More answer You can create 5 answer(s).
      Ask a Poll
      Cancel

      Log SQL to file with logback & hibernate

      I am trying to get the SQL logged directly to a file when running the dev profile. This is my logback.xml

      <configuration> <property name="SQL_LOG_FILE" value="${LOG_PATH:-${LOG_TEMP:-${TMPDIR:-/tmp}}}/${HIBERNATE_LOG_FILE:-hibernate.log}"/>  <springProfile name="dev">     <appender name="SQLDEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">         <file>${SQL_LOG_FILE}</file>         <encoder>             <charset>utf-8</charset>             <Pattern>%-5level %logger{0} - %msg%n</Pattern>         </encoder>     </appender>     <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">         <encoder>             <charset>utf-8</charset>             <Pattern>[%highlight(%p)] [%t] %c - %m%n</Pattern>         </encoder>     </appender>      <logger name="org.hibernate.SQL" additivity="false" level="DEBUG">         <appender-ref ref="SQLDEBUG"/>     </logger>     <logger name="org.hibernate.type.descriptor.sql" additivity="false" level="TRACE">         <appender-ref ref="SQLDEBUG"/>     </logger> </springProfile>  <root level="${logback.loglevel}">     <springProfile name="dev">         <appender-ref ref="CONSOLE"/>     </springProfile> </root> 

      I have removed the prod profile settings for simplicity. The logger for hibernate is inside the dev profile because I don’t want it enabled in prod. I have tried many combinations of these org.hibernate settings. This version generates SQL logs but only dumps them to console, not the log file. Some general startup information is added to the log file but no SQL.

      If I change org.hibernate.type.descriptor.sql to org.hibernate.type there is a lot of stack trace logs that are added directly to the file, but no SQL.

      Some posts recommend using org.hibernate.SQL level=TRACE but that did not seem to change anything.

      I also tried putting the logger outside of the dev profile but that also did not change the results.

      There is a lot of information for enabling logback & hibernate for simple console output but not for sending the SQL to its own log file.

      I also tried enabling hibernate.SQL=DEBUG in IntelliJ but that makes a lot of SQL on the console, I need to not do that.

      I have been try

      1 Answers

      I doubt you spring profile is being used. To get this to work rename the logback.xml file to logback-spring.xml, allowing the springProfile tag to be used. In this tag, a name can be provided that can be set via properties, environment variables, or VM options. Below is how you can set the springProfile name to dev , which has been used to represent a development environment.

      To set in application.properties or as an environment variable:

      spring.profiles.active=dev 

      Or as a VM option:

      -Dspring.profiles.active=dev 

      Also, modify your root-level tag to be inside the spring profile tag:

           <springProfile name="dev">        <root level="${logback.loglevel}">           <appender-ref ref="CONSOLE"/>        </root>     </springProfile>  
      Answered by Mohamedclarissaangie on July 16, 2020..