带有 logback 的 SLF4J 仍然提示无法加载类 “org.slf4j.impl.StaticLoggerBinder”

2022-09-04 22:43:21

我有一个maven项目,它使用slf4j和logback作为记录器。我可以看到这两个项目都在我的maven依赖项树项中。但是每当我尝试运行我的项目时,我都会收到提示:

SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。SLF4J:默认为无操作 (NOP) 记录器实现 SLF4J:有关更多详细信息,请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder

我检查了链接,它说:

在类路径上放置一个(并且只有一个)slf4j-nop.jar,slf4j-simple.jar slf4j-log4j12.jar,slf4j-jdk14.jar或logback-classic.jar应该可以解决问题。

我已将 logback-classic 添加到我的项目中(我使用的是 1.0.9),但我一直收到提示,并且我的日志没有运行。

任何人都可以帮我解决这个问题吗?谢谢。

更新:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.employee.scheduler</groupId>
<artifactId>rostering</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nurserostering</name>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!-- or whatever version you use -->
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <!-- Needed for runExamples.sh and runExamples.bat -->
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>../../binaries/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <configuration>
                    <executable>java</executable>
                    <classpathScope>runtime</classpathScope>
                    <arguments>
                        <argument>-Xms256m</argument>
                        <argument>-Xmx1024m</argument>
                        <argument>-server</argument>
                        <argument>-cp</argument>
                        <classpath />
                        <argument>com.employee.scheduler.nurserostering.app.NurseRosteringApp</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<dependencies>
    <dependency>
        <groupId>org.optaplanner</groupId>
        <artifactId>optaplanner-core</artifactId>
        <version>6.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>jdom</groupId>
        <artifactId>jdom</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.optaplanner</groupId>
        <artifactId>optaplanner-benchmark</artifactId>
        <version>6.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.0.9</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.9</version>
    </dependency>
</dependencies>

这是我的日志.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright 2013 JBoss Inc
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<configuration>

  <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <!-- %l lowers performance -->
      <!--<pattern>%d [%t] %-5p %l%n  %m%n</pattern>-->
      <pattern>%d [%t] %-5p %m%n</pattern>
    </encoder>
  </appender>
  <!--<appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
    <!--<file>local/log/optaplannerBenchmark.log</file>-->
    <!--<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">-->
      <!--<fileNamePattern>local/log/optaplannerBenchmark.%i.log.zip</fileNamePattern>-->
      <!--<minIndex>1</minIndex>-->
      <!--<maxIndex>3</maxIndex>-->
    <!--</rollingPolicy>-->
    <!--<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">-->
      <!--<maxFileSize>5MB</maxFileSize>-->
    <!--</triggeringPolicy>-->
    <!--<encoder>-->
      <!--&lt;!&ndash; %l lowers performance &ndash;&gt;-->
      <!--&lt;!&ndash;<pattern>%d [%t] %-5p %l%n  %m%n</pattern>&ndash;&gt;-->
      <!--<pattern>%d [%t] %-5p %m%n</pattern>-->
    <!--</encoder>-->
  <!--</appender>-->

  <logger name="org.optaplanner" level="debug"/>
  <logger name="com.employee.scheduler" level="debug"/>

  <root level="warn">
    <appender-ref ref="consoleAppender" />
    <!--<appender-ref ref="fileAppender" />-->
  </root>

</configuration>

我如何在我的程序中使用它:

public class SolutionBusiness {
    protected final transient Logger logger = LoggerFactory.getLogger(getClass());

// some other codes

public void doMove(Move move) {
        if (solver.isSolving()) {
            logger.error("Not doing user move ({}) because the solver is solving.", move);
            return;
        }
        if (!move.isMoveDoable(guiScoreDirector)) {
            logger.warn("Not doing user move ({}) because it is not doable.", move);
            return;
        }
        logger.info("Doing user move ({}).", move);
        move.doMove(guiScoreDirector);
    }
}

这是我的 maven 依赖项:

maven dependencies


答案 1

有同样的错误。我去了Maven Repository,寻找最新的非测试版。只需更改版本即可。一个工作 pom:

<properties>
    <slf4j.version>1.7.26</slf4j.version>
    <logback.version>1.2.3</logback.version>
</properties>

<dependencies>
    <!-- LOGGING -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>${logback.version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
</dependencies>

答案 2

猜猜当你有一个自定义的sbt任务并且该任务使用slf4j时,就会发生这种情况。使用 运行的自定义 sbt 任务具有通常指定为runner

(dependencyClasspath in Compile) value

这导致了通常追加在 上的错误。logback-classic% runtime

要解决此错误,您必须在定义或使用自定义任务的任何位置包含 into on。logback-classiclibraryDependenciescompile


推荐