无法加载类slf4g StaticLoggerBinder

时间:2012-12-02 11:23:14

标签: 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> 
..

3 个答案:

答案 0 :(得分:3)

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

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

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

SLF4J是一个框架,旨在隐藏日志记录实际实现的细节。例如,在运行时,您可以决定使用log4jcommons loggingJava 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>

答案 1 :(得分:0)

您只有API。提供实施。

答案 2 :(得分:-1)

你的班级路径中似乎缺少slf4j jars。下载slf4j libraries并将其添加到项目类路径中。