简体   繁体   English

如何将 SLF4J 与 Log4J 绑定?

[英]How to bind SLF4J with Log4J?

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$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.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$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.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".我检查了类 org.apache.log4j.Level 在我的项目中存在于“Maven Dependencies> log4j-1.2.17.jar”下。 So why can't the VM find it?那么为什么VM找不到它呢?

I tried the below options already:我已经尝试了以下选项:

  1. slf4j-api + log4j slf4j-api + log4j
  2. slf4j-api + slf4j-log4j12 slf4j-api + slf4j-log4j12
  3. slf4j-api + log4j slf4j-api + log4j
  4. slf4j-api + slf4j-log4j12 + log4j 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.另外,我注意到在添加 slf4j-log4j12 时,它也会在树中添加 log4j 作为依赖项,所以我猜在这里显式添加 log4j 是多余的,但我还是添加了它。 Still no luck.仍然没有运气。

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 SampleDao and running it as Java application.我正在单击SampleDao并将其作为 Java 应用程序运行。 ( I have also removed dbunit dependency for now, to zero-in on the slf4j problem) (我现在还删除了 dbunit 依赖项,将 slf4j 问题归零)Maven项目结构

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>

Do you still get the error if you run mvn compile exec:java -D"exec.mainClass"=SampleDao from the command line?如果您从命令行运行mvn compile exec:java -D"exec.mainClass"=SampleDao ,您是否仍然收到错误mvn compile exec:java -D"exec.mainClass"=SampleDao I see the following on the command line after running this command, with Maven 3.1.1 on my machine and your code:运行此命令后,我在命令行上看到以下内容,我的机器上有 Maven 3.1.1 和您的代码:

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

So, your POM and code seem to be fine.所以,你的 POM 和代码似乎没问题。 It looks like something is wrong with how you are trying to run the code.您尝试运行代码的方式似乎有问题。

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。

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

Use this depedences.使用这个依赖。

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

Config file:配置文件:

src/main/resources/log4j.properties

Add below dependency :添加以下依赖项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM