gpt4 book ai didi

java - 无法加载类 slf4g StaticLoggerBinder

转载 作者:行者123 更新时间:2023-11-30 04:35:14 25 4
gpt4 key购买 nike

不知道程序有什么问题。我收到这个通知了我已经在 pom.xml 文件上正确设置了依赖项。

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.2</version>
</dependency>
..

最佳答案

避免此警告的一个简单方法是将以下运行时依赖项添加到项目的 POM

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.2</version>
<scope>runtime</scope>
</dependency>

这将提供一个简单的日志记录实现,它将打印您的 INFO 级别语句。您很快就会无法适应此解决方案,但它允许您有时间研究您的代码最终将使用的日志记录框架。

SLF4J是一个旨在隐藏日志记录实际实现方式的细节的框架。例如,在运行时您可以决定使用 log4j , commons logging , Java logginglogback .

更新 1:如何重现问题

以下 Java 项目重现了您的问题:

|-- pom.xml
`-- src
`-- main
`-- java
`-- HelloWorld.java

从 Maven 运行程序如下:

$ mvn exec:java -Dexec.mainClass=HelloWorld
..
..
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.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

一旦将缺少的依赖项添加到 POM 中,您的程序就会有一个日志记录实现,它可以使用它来发出日志记录语句,如下所示:

$ mvn exec:java -Dexec.mainClass=HelloWorld
..
..
[HelloWorld.main()] INFO HelloWorld - Java says: Hello world
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

pom.xml

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspotontheweb.demo</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
</project>

HelloWorld.java

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

public class HelloWorld {
Logger log = LoggerFactory.getLogger(HelloWorld.class);

public String speak(String speech) {
log.info("Java says: {}", speech);

return speech;
}

public static void main(String[] args) {
HelloWorld hello = new HelloWorld();

hello.speak("Hello world");
}
}

更新 2:如何创建可执行 jar

如果您配置 Maven 创建可执行 jar,那么它将自动使用引用 POM 文件中列出的依赖项的类路径来打包该 jar。

$ mvn clean package
$ java -jar target/my-project-1.0-SNAPSHOT.jar
[main] INFO HelloWorld - Java says: Hello world

pom.xml

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspotontheweb.demo</groupId>
<artifactId>my-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>HelloWorld</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

关于java - 无法加载类 slf4g StaticLoggerBinder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13668784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com