Spring Boot Webapp:让 Hibernate 使用 log4j

Posted

技术标签:

【中文标题】Spring Boot Webapp:让 Hibernate 使用 log4j【英文标题】:Spring Boot Webapp: Get Hibernate to use log4j 【发布时间】:2015-06-24 23:45:18 【问题描述】:

我有一个 Spring Boot Web 应用程序。尝试自定义日志记录。我了解 slf4j 和 log4j 之间的区别。在这种情况下,我需要使用 log4j。在大多数情况下,它按我的意愿工作。

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

我尝试排除 jboss 日志记录,然后我了解到 jboss 实际上是一种类似于 slf4j 的门面,因此排除它似乎并没有解决问题(当我对此特别激进时,应用程序无法启动)

我还不知道 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>

【问题讨论】:

【参考方案1】:

您可以将slf4jlog4j 一起使用。

你在用maven吗?如果是这样,您能否发布与 slf4jlog4j 相关的 pom.xml 依赖项?

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

对于 log4j 1.2.x,以下 maven 依赖项将使您启动并运行您的 slf4j 日志消息在您的 log4j 附加程序中:

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

对于 log4j 2.x,以下 maven 依赖项将使您启动并运行您的 slf4j 日志消息在您的 log4j 附加程序中:

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

希望有帮助!

【讨论】:

【参考方案2】:

log4j 是一个日志 API,slf4j 是在各种支持的日志 API 之上的抽象

我怀疑在您的情况下,您使用的是直接 log4j,而休眠日志记录是通过 slf4j 完成的,它与其他一些实现(除了 log4j)绑定,尝试找出它与您的 maven 依赖关系树绑定或绑定的日志记录

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

【讨论】:

以上是关于Spring Boot Webapp:让 Hibernate 使用 log4j的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot webapp localhost only

由于缺少 WebApp 库,在 Spring-boot-starter 项目中构建失败

如何为我的 Spring Boot webapp 选择 URL?

在 spring boot angular 8 webapp 中验证和管理用户的最简单方法是啥?

Spring-Boot静态文件处理

Spring Boot 返回字符串而不是视图