How to bind SLF4J with Log4J?

我只是一个虾纸丫 提交于 2019-12-05 05:15:48

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? I see the following on the command line after running this command, with Maven 3.1.1 on my machine and your code:

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.

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.

Add below dependency :

<dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>
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:

<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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!