java 如何将 SLF4J 与 Log4J 绑定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25704527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:30:32  来源:igfitidea点击:

How to bind SLF4J with Log4J?

javamavenlogginglog4jslf4j

提问by Pat

I am trying to setup a project with slf4j + log4j, but the damn thing just doesn't work... I keep getting exception:

我正在尝试使用 slf4j + log4j 设置一个项目,但该死的东西不起作用......我不断收到异常:

Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:129)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:302)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
at TestLog.main(TestLog.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at java.net.URLClassLoader.run(URLClassLoader.java:372)
at java.net.URLClassLoader.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:129)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:302)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
at TestLog.main(TestLog.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at java.net.URLClassLoader.run(URLClassLoader.java:372)
at java.net.URLClassLoader.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more

I checked and the class org.apache.log4j.Level is there in my project under "Maven Dependencies > log4j-1.2.17.jar". So why can't the VM find it?

我检查了类 org.apache.log4j.Level 在我的项目中存在于“Maven Dependencies > log4j-1.2.17.jar”下。那么为什么VM找不到它呢?

I tried the below options already:

我已经尝试了以下选项:

  1. slf4j-api + log4j
  2. slf4j-api + slf4j-log4j12
  3. slf4j-api + log4j
  4. slf4j-api + slf4j-log4j12 + log4j
  1. slf4j-api + log4j
  2. slf4j-api + slf4j-log4j12
  3. slf4j-api + log4j
  4. slf4j-api + slf4j-log4j12 + log4j

None of the options worked :(

没有一个选项有效:(

Here is my currnet pom.xml:

这是我的 currnet pom.xml:

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.sample</groupId>
  <artifactId>DBUnitSample</artifactId>
  <version>1.0</version>

  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.7</version>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.7</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

  </dependencies>

</project>

Also, I noticed that when adding slf4j-log4j12, it adds log4j as a dependency as well in the tree, so I am guessing that adding log4j explicitly here is redundant, but I have added it anyway. Still no luck.

另外,我注意到在添加 slf4j-log4j12 时,它也会在树中添加 log4j 作为依赖项,所以我猜在这里显式添加 log4j 是多余的,但我还是添加了它。仍然没有运气。

My main code is pretty simple:

我的主要代码非常简单:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SampleDao {
    private static final Logger log = LoggerFactory.getLogger(SampleDao.class);

    public static void main(String[] args) {
        System.out.println("Hello");
        log.info("Logged");
    }
}

And my log4j.properties:

还有我的 log4j.properties:

log4j.rootLogger=DEBUG, STDOUT
log4j.logger.deng=INFO
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Can someone please tell me what am I doing wrong? I already tried searching for answers on SO, but all answers mentioned to add log4j dependency, which I have already added... So not sure, whats wrong.

有人可以告诉我我做错了什么吗?我已经尝试在 SO 上搜索答案,但所有答案都提到添加 log4j 依赖项,我已经添加了......所以不确定,有什么问题。

EDIT: Here is my entire project structure. I am R-clicking SampleDaoand running it as Java application. ( I have also removed dbunit dependency for now, to zero-in on the slf4j problem) Maven Project Structure

编辑:这是我的整个项目结构。我正在单击SampleDao并将其作为 Java 应用程序运行。(我现在还删除了 dbunit 依赖项,将 slf4j 问题归零) Maven项目结构

回答by Dmytro Plekhotkin

Try these dependencies

试试这些依赖

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.5</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
    <scope>runtime</scope>
</dependency>

回答by user10272294

Add below dependency :

添加以下依赖项:

<dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>

回答by kai

I met the same mistake as you. I solved my problem according to the following two Suggestions.I hope it helps.

我遇到了和你一样的错误。我根据以下两个建议解决了我的问题,希望对您有所帮助。

1. Modify your pom file:

1.修改你的pom文件:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.2</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.12</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.2</version>
</dependency>

2. Add the jar to your startup script

2. 将 jar 添加到您的启动脚本中

java -cp ./lib/log-producer-1.0.4-SNAPSHOT.jar:./lib/slf4j-log4j12-1.7.2.jar:./lib/slf4j-api-1.7.2.jar:./lib/log4j-1.2.12.jar cn.tiakon.core.Bootstrap

回答by manish

Do you still get the error if you run mvn compile exec:java -D"exec.mainClass"=SampleDaofrom the command line? I see the following on the command line after running this command, with Maven 3.1.1 on my machine and your code:

如果从命令行运行mvn compile exec:java -D"exec.mainClass"=SampleDao,您是否仍然收到错误消息?运行此命令后,我在命令行上看到以下内容,我的机器上有 Maven 3.1.1 和您的代码:

Hello
 INFO [SampleDao.main()] (SampleDao.java:9) - Logged

So, your POM and code seem to be fine. It looks like something is wrong with how you are trying to run the code.

所以,你的 POM 和代码似乎没问题。您尝试运行代码的方式似乎有问题。

Also note that the following simplified configuration should work:

另请注意,以下简化配置应该有效:

<dependencies>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.7</version>
  </dependency>
</dependencies>

You would have to specify a particular version of Log4j if the POM has other dependencies that add different versions of Log4j to the project.

如果 POM 具有将不同版本的 Log4j 添加到项目的其他依赖项,则必须指定特定版本的 Log4j。