无法加载类slf4g StaticLoggerBinder

2022-05-14 00:00:00 java maven slf4j

我不知道程序有什么问题。我收到了这张通知。我已正确设置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 logging或logback。

更新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>

相关文章