Java 带有 slf4j 和 log4j 的 Hibernate 3.4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/467879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate 3.4 with slf4j and log4j
提问by Steve Kuo
I'm attempting to upgrade from Hibernate 3.2 to 3.4, which apparently uses slf4j. Our project currently uses log4j. So my assumption is that I should be using the slf4j-log4j12 wrapped implementation.
我正在尝试从 Hibernate 3.2 升级到 3.4,它显然使用了 slf4j。我们的项目目前使用 log4j。所以我的假设是我应该使用 slf4j-log4j12 包装的实现。
The Maven slf4j dependency is:
Maven slf4j 依赖项是:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
While the log4j dependency is:
虽然 log4j 依赖项是:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
Both slf4j-log4j12 and log4j reference the latest version (that I could find in the Maven repository). When I run my app, Hibernate fails in its logging:
slf4j-log4j12 和 log4j 都引用了最新版本(我可以在 Maven 存储库中找到)。当我运行我的应用程序时,Hibernate 的日志记录失败:
java.lang.NoSuchFieldError: name
at org.slf4j.impl.Log4jLoggerAdapter.<init>(Log4jLoggerAdapter.java:75)
at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:75)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:103)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:163)
...
What am I missing?
我错过了什么?
Edit 1:If I remove the log4j dependency from my pom.xml I get the error:
编辑 1:如果我从 pom.xml 中删除 log4j 依赖项,则会收到错误消息:
java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory
at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)
at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:163)
...
Edit 2:This blogclaims the problem is caused by hibernate annotations shipping with the wrong version of slf4j-api.jar.
编辑 2:此博客声称该问题是由带有错误版本的 slf4j-api.jar 的休眠注释引起的。
回答by duffymo
A Google search found this:
谷歌搜索发现这个:
http://marc.info/?l=slf4j-user&m=122218775201271&w=2
http://marc.info/?l=slf4j-user&m=122218775201271&w=2
Maybe you should check your JAR versions to make sure they're compatible.
也许您应该检查您的 JAR 版本以确保它们兼容。
回答by Michael Rutherfurd
After checking the version 1.5.6 POM for slf4j-log4j (and then slf4j-parent) you should be using log4j-1.2.14. The slf4j-log4j POM uses dependency management to inherit the appropriate version of log4j from the slf4j-parent POM.
在检查 slf4j-log4j(然后是 slf4j-parent)的 1.5.6 POM 版本后,您应该使用 log4j-1.2.14。slf4j-log4j POM 使用依赖管理从 slf4j-parent POM 继承适当版本的 log4j。
You shouldn't, however, need to include log4j as a specific dependency as it is already a dependency of slf4j-log4j. That may have been where you caused your problem.
但是,您不应该需要将 log4j 作为特定依赖项包含在内,因为它已经是 slf4j-log4j 的依赖项。那可能是您导致问题的地方。
回答by Michael Pralow
i have no problems with
我没有问题
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.1.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
and
和
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>
<!-- concrete Log4J Implementation for SLF4J API-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
回答by Ceki
This question is answered by the SLF4J FAQ. Please see
SLF4J 常见问题解答回答了这个问题。请参见
http://slf4j.org/faq.html#compatibilityand http://slf4j.org/faq.html#IllegalAccessError
http://slf4j.org/faq.html#compatibility和 http://slf4j.org/faq.html#IllegalAccessError
回答by James Shade
I think you need to exclude the built-in SLF4J dependency from each of the Hibernate dependencies.
我认为您需要从每个 Hibernate 依赖项中排除内置的 SLF4J 依赖项。
I use Hibernate with JPA, so my config isn't identical, but I think the crucial thing is to explicitly include log4j and SLF4J and explicitly exclude slf4j-api from all org.hibernate dependencies:
我将 Hibernate 与 JPA 一起使用,所以我的配置不相同,但我认为关键是明确包含 log4j 和 SLF4J 并从所有 org.hibernate 依赖项中明确排除 slf4j-api:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.4.0.GA</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
<scope>runtime</scope>
</dependency>
回答by M Smith
I had the same problem with displaytag version 1.2 including old slf4j. Changing the displaytag to have the exclusion:
我在使用 displaytag 1.2 版时遇到了同样的问题,包括旧的 slf4j。更改 displaytag 以排除:
<dependency>
<groupId>displaytag</groupId>
<artifactId>displaytag</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl104-over-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
and adding the correct dependencies:
并添加正确的依赖项:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>
<!-- concrete Log4J Implementation for SLF4J API-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>
seemed to fix the problem.
似乎解决了这个问题。