4
votes

I have a spring boot web application. Trying to customize the logging. I understand the difference between slf4j and log4j. I am required in this case to use log4j. For the most part it's working how I want.

The catch is, Hibernate is printing SQL queries to the console and based on the format I'm thinking that it's not using log4j at all. I'd like to do further customization of what Hibernate is logging. Most of the advice I see assumes that Hibernate is logging via log4j. How would I get hibernate to use log4j as its logging framework?

I tried excluding jboss logging, then I learned that jboss is actually a facade sort of like slf4j, so excluding it didn't seem to solve the problem (and when I got particularly aggressive about it, application failed to launch)

I don't yet know how exactly jboss or hibernate is configured. I am new at this, so I would greatly appreciate if you include a reasonably detailed description of how to implement your suggested solution.

Update: Someone asked, yes, I am using Maven. I followed directions on a Spring Boot page in order to exclude logback so that spring boot instead uses log4j. I am sure now that the majority of the application is logging with log4j - either directly or through slf4j - since the system is doing as my log4j configuration file instructs.

Whether the application is using log4j directly, or slf4j, is an important piece of the puzzle and that is something I will have to check. As to what in the world Hibernate is doing, that I don't know yet. I will provide further updates as I carry out the suggestions that I've been given.

Many thanks for all your help and advice!

Maven dependency:tree is as follows:

[INFO] [dependency:tree {execution: default-cli}]
[INFO] <redacted>
[INFO] +- <redacted>
[INFO] +- <redacted>
[INFO] |  +- log4j:log4j:jar:1.2.17:compile (version managed from 1.2.16)
[INFO] |  \- commons-codec:commons-codec:jar:1.3:compile
[INFO] +- <redacted>
[INFO] |  +- org.springframework:spring-context:jar:4.1.5.RELEASE:compile (version managed from 3.1.1.RELEASE)
[INFO] |  +- org.springframework:spring-beans:jar:4.1.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-orm:jar:4.1.5.RELEASE:compile (version managed from 3.1.1.RELEASE)
[INFO] |  |  \- org.springframework:spring-jdbc:jar:4.1.5.RELEASE:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.7.10:compile (version managed from 1.5.11)
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.10:compile (version managed from 1.5.11)
[INFO] |  +- org.slf4j:slf4j-log4j12:jar:1.7.10:compile (version managed from 1.5.11)
[INFO] |  \- c3p0:c3p0:jar:0.9.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.2.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.2.2.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.2.2.RELEASE:compile
[INFO] |  |  \- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.2.RELEASE:compile
[INFO] |  |     \- org.yaml:snakeyaml:jar:1.14:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.5:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.5:compile (version managed from 2.4.0)
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.4.5:compile
[INFO] |  +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.0.0:compile
[INFO] |  +- org.springframework:spring-core:jar:4.1.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-web:jar:4.1.5.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-log4j:jar:1.2.2.RELEASE:compile
[INFO] |  \- org.slf4j:jul-to-slf4j:jar:1.7.10:compile
[INFO] +- org.springframework.boot:spring-boot-starter-ws:jar:1.2.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-jms:jar:4.1.5.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-messaging:jar:4.1.5.RELEASE:compile
[INFO] |  +- org.springframework:spring-oxm:jar:4.1.5.RELEASE:compile
[INFO] |  +- org.springframework.ws:spring-ws-core:jar:2.2.0.RELEASE:compile
[INFO] |  \- org.springframework.ws:spring-ws-support:jar:2.2.0.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.2.2.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.2.2.RELEASE:compile
[INFO] |  |  +- org.aspectj:aspectjrt:jar:1.8.5:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.5:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.2.2.RELEASE:compile
[INFO] |  |  \- org.apache.tomcat:tomcat-jdbc:jar:8.0.20:compile
[INFO] |  |     \- org.apache.tomcat:tomcat-juli:jar:8.0.20:compile
[INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.7.2.RELEASE:compile
[INFO] |  |  \- org.springframework.data:spring-data-commons:jar:1.9.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-aspects:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-security:jar:1.2.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.1.5.RELEASE:compile
[INFO] |  +- org.springframework.security:spring-security-config:jar:3.2.6.RELEASE:compile
[INFO] |  |  +- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  \- org.springframework.security:spring-security-core:jar:3.2.6.RELEASE:compile
[INFO] |  +- org.springframework.security:spring-security-web:jar:3.2.6.RELEASE:compile
[INFO] |  \- org.springframework:spring-aop:jar:4.1.5.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:1.2.2.RELEASE:test
[INFO] |  +- junit:junit:jar:4.12:test
[INFO] |  +- org.hamcrest:hamcrest-library:jar:1.3:test
[INFO] |  \- org.springframework:spring-test:jar:4.1.5.RELEASE:test
[INFO] +- org.powermock:powermock-reflect:jar:1.6.1:test
[INFO] +- org.powermock:powermock-module-junit4:jar:1.6.1:test
[INFO] |  \- org.powermock:powermock-module-junit4-common:jar:1.6.1:test
[INFO] |     \- org.powermock:powermock-core:jar:1.6.1:test
[INFO] +- org.powermock:powermock-api-mockito:jar:1.6.1:test
[INFO] |  +- org.mockito:mockito-all:jar:1.10.8:test
[INFO] |  \- org.powermock:powermock-api-support:jar:1.6.1:test
[INFO] +- org.mockito:mockito-core:jar:1.10.8:compile
[INFO] |  +- org.hamcrest:hamcrest-core:jar:1.3:runtime
[INFO] |  \- org.objenesis:objenesis:jar:2.1:runtime
[INFO] +- org.hibernate:hibernate-core:jar:4.3.8.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  +- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:4.3.8.Final:compile
[INFO] +- javax.transaction:jta:jar:1.1:compile
[INFO] +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.2.2.RELEASE:provided
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.20:provided
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.20:provided
[INFO] |  +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.20:provided
[INFO] |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.20:provided
[INFO] +- org.apache.ws.security:wss4j:jar:1.6.15:compile
[INFO] |  +- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] |  +- org.apache.santuario:xmlsec:jar:1.5.6:compile
[INFO] |  \- org.opensaml:opensaml:jar:2.5.1-1:compile
[INFO] |     \- org.opensaml:openws:jar:1.4.2-1:compile
[INFO] |        \- org.opensaml:xmltooling:jar:1.3.2-1:compile
[INFO] |           \- joda-time:joda-time:jar:2.5:compile (version managed from 1.6.2)
[INFO] +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.0:compile
[INFO] |  \- org.codehaus.jackson:jackson-core-asl:jar:1.9.0:compile
[INFO] +- org.springframework.ws:spring-ws-security:jar:2.2.0.RELEASE:compile
[INFO] |  +- org.springframework.ws:spring-xml:jar:2.2.0.RELEASE:compile
[INFO] |  \- org.springframework:spring-tx:jar:4.1.5.RELEASE:compile
[INFO] +- com.oracle:ojdbc14:jar:10.2.0.4:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.3.5:compile
[INFO] |  \- org.apache.httpcomponents:httpcore:jar:4.3.2:compile
[INFO] +- org.json:json:jar:20140107:compile
[INFO] +- org.springframework:spring-context-support:jar:4.1.3.RELEASE:compile
[INFO] +- net.sf.ehcache:ehcache:jar:2.9.1:compile
[INFO] \- <redacted>
2

2 Answers

3
votes

You can use slf4j with log4j.

Are you using maven? If so, can you post your pom.xml dependencies related to either slf4j or log4j?

Also, check out this link: http://www.mkyong.com/hibernate/how-to-configure-log4j-in-hibernate-project/

For log4j 1.2.x, the following maven dependency will get you up and running with your slf4j log messages in your log4j appenders:

    <!-- slf4j-log4j -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>

For log4j 2.x, the following maven dependency will get you up and running with your slf4j log messages in your log4j appenders:

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.2</version>
  </dependency>
</dependencies>

Hope that helps!

2
votes

log4j is a logging api, slf4j is ab abstraction on top of various supported logging apis

I suspect in your case you are using straight log4j and hibernate logging is done through slf4j which gets binded with some other implementation (other than log4j), try to find out what logging it gets binded to or from your maven dependency tree

remove all slf4j-* (except for slfj-api) and place as runtime dependency slf4j-log4j, that will bind slf4j calls to log4j and it will refer to log4j.properties placed in classpath and respect that