0

I have the following logback.xml:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

<appender name="FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>C:/logs/external-web-services.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>C:/logs/external-web-services.%d{yyyy-MM-dd}.log
        </fileNamePattern>
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <append>true</append>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

<logger name="org.hibernate.SQL" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="FILE" />
</logger>

<logger name="org.hibernate.type" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="FILE" />
</logger>

<root level="DEBUG">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
</root>

I want to log Hibernate sql queries in my log file. I only see Hibernate queries in console when running from Eclipse (also in JBoss log file server.log, but this is something else I do not understand). In my log file (external-web-services.log) only my log messages are printed. No Hibernate message is ever printed. In eclipse I get the following:

11:50:05,367 INFO  [stdout] (default task-9) Hibernate: 
11:50:05,367 INFO  [stdout] (default task-9)     select
11:50:05,367 INFO  [stdout] (default task-9)         ftibssettl0_.settlcycleinstanceid as settlcycleinstance1_3_,
11:50:05,367 INFO  [stdout] (default task-9)         ftibssettl0_.endtime as endtime2_3_,
11:50:05,367 INFO  [stdout] (default task-9)         ftibssettl0_.PARTSET_0_PART as PARTSET_3_3_,
11:50:05,367 INFO  [stdout] (default task-9)         ftibssettl0_.PARTSET_1_PART as PARTSET_4_3_,
11:50:05,367 INFO  [stdout] (default task-9)         ftibssettl0_.PARTSET_2_PART as PARTSET_5_3_,
11:50:05,367 INFO  [stdout] (default task-9)         ftibssettl0_.PARTSET_3_PART as PARTSET_6_3_,
11:50:05,367 INFO  [stdout] (default task-9)         ftibssettl0_.PARTSET_4_PART as PARTSET_7_3_,
11:50:05,367 INFO  [stdout] (default task-9)         ftibssettl0_.settlcycletemplateid as settlcycletemplate8_3_,
11:50:05,368 INFO  [stdout] (default task-9)         ftibssettl0_.settlementdate as settlementdate9_3_,
11:50:05,368 INFO  [stdout] (default task-9)         ftibssettl0_.starttime as starttime10_3_,
11:50:05,368 INFO  [stdout] (default task-9)         ftibssettl0_.status as status11_3_ 
11:50:05,368 INFO  [stdout] (default task-9)     from
11:50:05,368 INFO  [stdout] (default task-9)         ibsprod.FT_IBS_SETTL_CYCLE_INSTANCE ftibssettl0_ 
11:50:05,368 INFO  [stdout] (default task-9)     where
11:50:05,368 INFO  [stdout] (default task-9)         ftibssettl0_.settlementdate=?

11:50:05,417 INFO  [stdout] (default task-9) 11:50:05.417 [default task-9] DEBUG g.d.n.s.CashMovementReportService - filterAndMarkCashMovements elapsed time: PT0.017S

What is strange about this log is that the format of the Hibernate message is not the format that I define in logback.xml encoder pattern, which can be seen in the last line of the log, which is my application's log message

Other Info:

I am using JBoss so I need to exclude JBoss own logging subsystem so that logback works, I am using the file jboss-deployment-structure.xml:

<jboss-deployment-structure>
  <deployment>
    <exclude-subsystems>
        <subsystem name="logging" />
    </exclude-subsystems>
  </deployment>
</jboss-deployment-structure>

I am using JPA so in case this might help this is my persistence.xml file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">

<persistence-unit name="external-ws" transaction-type="JTA">
    <description>Oracle Persistence Unit</description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>${ds.name}</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.default_schema" value="${ds.schema}"/>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="org.hibernate.flushMode" value="manual"/>
    </properties>
</persistence-unit>

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
alex
  • 705
  • 9
  • 21

1 Answers1

1

Try adding:

<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  <level>TRACE</level>
</filter>

to both the CONSOLE and the FILE appender. The default threshold is WARN.

You can also check if this is the configuration file log4j uses. It may be that a different file might be loaded, due to class loading miss configurations.

The ch.qos.logback.classic.util.ContextInitializer has a:

public void configureByResource(URL url) {

method you can try debugging at runtime to check which configuration file is actually loaded.

Also, try removing:

<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />

since those should only print to console.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • I am not sure I understand what you are saying. Just to clarify, I am not using log4j i am using logback. Also, the file is correctly picked up I know that because my own messages are logged correctly. For example when I change the root level to INFO I do not see my DEBUG messages. The problem is with the Hibernate messages which are not logged in the file. – alex Jul 09 '14 at 09:42
  • I updated my response for logback. In case your config changes are visible, it means logback has selected the right file. Check my updated response. – Vlad Mihalcea Jul 09 '14 at 09:56
  • I just realised that what is printed to the console from Hibernate is printed because of the persistence.xml file properties and not because of my logback.xml configuration. When I removed nothing is printing in the console either – alex Jul 09 '14 at 10:23
  • I tried the filter but I do not see anything different in the log or the console – alex Jul 09 '14 at 10:25
  • The file seems fine to me. I suspect logback is loadign a totally different logback.xml configuration file. Start your app in debug mode and attach your IDE to the debug port, and check the URL resolved in the ContextInitializer#configureByResource method. – Vlad Mihalcea Jul 09 '14 at 11:05
  • I failed to mention that I am using Wildfly, which complicates things. It seems that Hibernate 4 (used by Wildfly) uses jboss logging and not slf4j. So it does not log using logback probably? This is what I found: http://stackoverflow.com/questions/21971576/hibernate-4-wildfly-8-logging-not-working – alex Jul 09 '14 at 11:37
  • That could explain why your config file got ignored. I suggest you try [the following solution](http://stackoverflow.com/questions/23079492/slf4j-logback-does-not-log-in-wildfly). – Vlad Mihalcea Jul 09 '14 at 11:42
  • thank you for all your help. I already exclude logging subsystem from wildfly for my application. That is why logback works for my application. But I have to find a way to tell Hibernate to use logback (if I understand correctly) – alex Jul 09 '14 at 12:18
  • Hibernate 4 uses slf4j and slf4j uses the current available logging provider. It's just a matter of configuring slf4j with the logging provider you want (logback + your config file). The hibernate related appenders are fine. – Vlad Mihalcea Jul 09 '14 at 12:21
  • I tried to set System Property org.jboss.logging.provider=slf4j as stated here: http://docs.jboss.org/hibernate/orm/4.3/topical/html/logging/Logging.html and I get java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory. It should not be that difficult to log Hibernate message to my own file, should it? – alex Jul 10 '14 at 06:07
  • 1
    JBoss didn;t find the slf4j-api. You need to check your global libraries or your project pom.xml file for this dependency. For Masven try "mvn dependency:tree" – Vlad Mihalcea Jul 10 '14 at 06:13
  • Sorry, I should have mentioned that the server fails to start completely, not the application. Even though Wildfly seems to have installed slf4j as a module, I get the NoClassDefFoundError on server startup (without any deployment) – alex Jul 10 '14 at 06:29