受Leif 的 Hypoport 帖子的启发,这就是我将 Hibernate 4 “弯曲”回 slf4j 的方式:
假设您正在使用 Maven。
org.slf4j:log4j-over-slf4j
作为依赖添加到您的pom.xml
- 使用命令
mvn dependency:tree
,确保您使用的工件都不slf4j:slf4j
依赖于(准确地说,任何工件都不应具有编译范围依赖或运行时范围依赖slf4j:slf4j
)
背景:Hibernate 4.x 依赖于 artifact org.jboss.logging:jboss-logging
。传递地,这个工件对工件有一个提供的范围依赖slf4j:slf4j
。
因为我们现在已经添加了org.slf4j:log4j-over-slf4j
工件,org.slf4j:log4j-over-slf4j
所以模仿了slf4j:slf4j
工件。因此,JBoss Logging
现在记录的所有内容实际上都将通过 slf4j 进行。
假设您使用Logback作为您的日志记录后端。这是一个示例pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
....
<properties>
....
<slf4j-api-version>1.7.2</slf4j-api-version>
<log4j-over-slf4j-version>1.7.2</log4j-over-slf4j-version>
<jcl-over-slf4j-version>1.7.2</jcl-over-slf4j-version> <!-- no problem to have yet another slf4j bridge -->
<logback-core-version>1.0.7</logback-core-version>
<logback-classic-version>1.0.7</logback-classic-version>
<hibernate-entitymanager-version>4.1.7.Final</hibernate-entitymanager-version> <!-- our logging problem child -->
</properties>
<dependencies>
<!-- begin: logging-related artifacts .... -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${jcl-over-slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${log4j-over-slf4j-version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback-core-version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-classic-version}</version>
</dependency>
<!-- end: logging-related artifacts .... -->
<!-- begin: some artifact with direct dependency on log4j:log4j .... -->
<dependency>
<groupId>org.foo</groupId>
<artifactId>some-artifact-with-compile-or-runtime-scope-dependency-on-log4j:log4j</artifactId>
<version>${bla}</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- begin: some artifact with direct dependency on log4j:log4j .... -->
<!-- begin: a hibernate 4.x problem child........... -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-entitymanager-version}</version>
</dependencies>
<!-- end: a hibernate 4.x problem child........... -->
....
</project>
在您的类路径上,有一个logback.xml
,例如位于src/main/java
:
<!-- begin: logback.xml -->
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.hibernate" level="debug"/>
<root level="info">
<appender-ref ref="console"/>
</root>
</configuration>
<!-- end: logback.xml -->
某些组件可能希望logback.xml
在 JVM 启动时访问以进行正确的日志记录,例如 Jetty Maven 插件。在这种情况下,将 Java 系统添加logback.configurationFile=./path/to/logback.xml
到您的命令中(例如mvn -Dlogback.configurationFile=./target/classes/logback.xml jetty:run
)。
如果您仍然获得“原始”控制台标准输出 Hibernate 输出(如Hibernate: select ...
),则堆栈溢出问题“关闭休眠日志记录到控制台”可能适用。