Spring Boot Webapp:让Hibernate使用log4j

时间:2015-04-17 23:03:32

标签: java hibernate spring-mvc log4j slf4j

我有一个spring boot web应用程序。尝试自定义日志记录。我理解slf4j和log4j之间的区别。在这种情况下我需要使用log4j。在大多数情况下,它的工作方式如我所愿。

问题是,Hibernate正在向控制台打印SQL查询,并且基于我认为它根本不使用log4j的格式。我想进一步定制Hibernate的日志记录。我看到的大多数建议都假设Hibernate通过log4j进行日志记录。我如何让hibernate使用log4j作为其日志框架?

我尝试排除jboss日志记录,然后我了解到jboss实际上是一种类似于slf4j的外观,所以排除它似乎没有解决问题(当我特别咄咄逼人时,应用程序无法启动)< / p>

我还不知道jboss或hibernate是如何配置的。我是新手,所以如果您包含有关如何实施建议解决方案的详细说明,我将不胜感激。

更新 有人问,是的,我正在使用Maven。 我按照Spring Boot页面上的说明来排除logback,以便spring boot使用log4j。我现在确定大多数应用程序都使用log4j进行日志记录 - 直接或通过slf4j进行日志记录 - 因为系统正在按照我的log4j配置文件的指示进行操作。

应用程序是直接使用log4j还是slf4j,这是一个重要的难题,我将不得不检查。至于Hibernate正在做什么,我还不知道。当我执行我给出的建议时,我会提供进一步的更新。

非常感谢您的帮助和建议!

Maven依赖:树如下:

[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 个答案:

答案 0 :(得分:2)

log4j是一个日志记录api,slf4j是在各种支持的日志记录apis之上的ab抽象

我怀疑在你的情况下你正在使用直接log4j并且hibernate日志记录是通过slf4j完成的,它与其他一些实现(除了log4j之外)绑定,尝试找出它被绑定到maven依赖树的日志记录< / p>

删除所有slf4j- *(slfj-api除外)并将其作为运行时依赖项slf4j-log4j,将slf4j调用绑定到log4j,它将引用放在classpath中的log4j.properties并尊重

答案 1 :(得分:2)

您可以将slf4jlog4j一起使用。

你正在使用maven吗?如果是,您是否可以发布与slf4jlog4j相关的pom.xml依赖项?

另外,请查看以下链接:http://www.mkyong.com/hibernate/how-to-configure-log4j-in-hibernate-project/

对于log4j 1.2.x,以下maven依赖项将启动并运行slf4j appenders中的log4j日志消息:

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

对于log4j 2.x,以下maven依赖项将启动并运行slf4j appenders中的log4j日志消息:

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

希望有所帮助!