簡體   English   中英

slf4j 沒有找到 log4j2 jars

[英]slf4j does not find log4j2 jars

我知道將SLF4J與 Log4j2 一起用作記錄器實現應該很容易。

您只需要將 jars 添加到類路徑中。

我以為我這樣做了,但我仍然得到

INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

我的pom.xml的關鍵部分

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.30</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j18-impl</artifactId>
    <version>2.14.0</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.14.0</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.0</version>
    <scope>runtime</scope>
</dependency>

然后是.classpath的摘錄

<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30.jar" sourcepath="M2_REPO/org/slf4j/slf4j-api/1.7.30/slf4j-api-1.7.30-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/apache/logging/log4j/log4j-slf4j18-impl/2.14.0/log4j-slf4j18-impl-2.14.0.jar" sourcepath="M2_REPO/org/apache/logging/log4j/log4j-slf4j18-impl/2.14.0/log4j-slf4j18-impl-2.14.0-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/apache/logging/log4j/log4j-api/2.14.0/log4j-api-2.14.0.jar" sourcepath="M2_REPO/org/apache/logging/log4j/log4j-api/2.14.0/log4j-api-2.14.0-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/apache/logging/log4j/log4j-core/2.14.0/log4j-core-2.14.0.jar" sourcepath="M2_REPO/org/apache/logging/log4j/log4j-core/2.14.0/log4j-core-2.14.0-sources.jar"/>

然后,查看lib文件夾

$ find ./ -name "*slf4*.jar"
./target/my-app-0.0.1-SNAPSHOT/WEB-INF/lib/log4j-slf4j18-impl-2.14.0.jar
./target/my-app-0.0.1-SNAPSHOT/WEB-INF/lib/slf4j-api-1.7.30.jar

$ find ./ -name "*log4j*.jar"
./target/my-app-0.0.1-SNAPSHOT/WEB-INF/lib/log4j-slf4j18-impl-2.14.0.jar
./target/my-app-0.0.1-SNAPSHOT/WEB-INF/lib/log4j-api-2.14.0.jar
./target/my-app-0.0.1-SNAPSHOT/WEB-INF/lib/log4j-core-2.14.0.jar

我錯過了什么?

我需要添加 log4j2配置 xml 文件嗎?

但我沒有收到相應的錯誤信息

我在用

  • Eclipse 版本:2020-09 (4.17.0)
  • Apache Maven 3.6.3

maven-war-plugin

 <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.3</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <:-- https.//stackoverflow.com/a/33390519/2092322 annotations instead of web.xml --> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin>

這是因為您使用的是slf4j 1.7.* ,但您使用logj4j-slf4j18-impl ,它假設與 slf4j 1.8 一起使用(尚未發布或更改為當前處於 alpha 版本的 2.0.0 版本)。 你需要改變

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j18-impl</artifactId>
    <version>2.14.0</version>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.14.0</version>
    <scope>runtime</scope>
</dependency>

為了使其與您在項目中使用的 slf4j 兼容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM