簡體   English   中英

帶logback的SLF4J還是提示failed to load class "org.slf4j.impl.StaticLoggerBinder"

[英]SLF4J with logback still prompt failed to load class "org.slf4j.impl.StaticLoggerBinder"

我有一個 maven 項目,它使用帶有 logback 的 slf4j 作為記錄器。 我可以看到這兩個工件都在我的 maven 依賴關系樹項中。 但是每當我嘗試運行我的項目時,我都會提示:

SLF4J:無法加載 class“org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默認為無操作 (NOP) 記錄器實現 SLF4J:有關詳細信息,請參閱http://www.slf4j.org/codes.html#StaticLoggerBinder

我檢查了鏈接,有人說:

Placing one (and only one) of slf4j-nop.jar, slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.

我已經將 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>

這是我的 logback.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 依賴項

有同樣的錯誤。 我去了 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>

猜猜當您有自定義 sbt 任務並且任務使用 slf4j 時會發生這種情況。 使用runner自定義 sbt 任務具有通常指定為的類路徑

(dependencyClasspath in Compile) value

這導致了logback-classic通常附加在% runtime上的錯誤。

要解決此錯誤,您必須在定義或使用自定義任務的任何libraryDependenciescompile時將logback-classic包含到libraryDependencies

如果 logback-classic 或 logback-core 不在類路徑中,您只能收到該特定的錯誤消息。 由於它們位於您的 maven 依賴項(請參閱 pom)和您的 eclipse 類路徑(請參閱屏幕截圖)中,因此這很奇怪。

首先,檢查您是否仍然收到相同的錯誤消息(也許您解決了該問題,現在收到了不同的錯誤消息)。

其次,在 eclipse 中,打開類型“StaticLoggerBinder”並在那里放置一個斷點。 還要在“LoggerFactory.getLogger”中放置一個斷點。 然后調試。

我遇到了同樣的問題。 問題是 jar 文件中未包含工件“slf4j-log4j12”。 當您通過 IDE 運行應用程序時,它應該可以工作,因為依賴項已經添加到 pom 文件中。 這實際上是一個maven包裝問題。 您需要將以下插件添加到 pom 文件中。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.7.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>se.cambiosys.connect.publish.ei.client.BulkEiSenderClient</mainClass>
            </transformer>
          </transformers>
          <artifactSet>
            <includes>
              <inlude>org.slf4j:slf4j-log4j12</inlude>
              <inlude>log4j:log4j</inlude>
            </includes>
          </artifactSet>
        </configuration>
      </execution>
    </executions>
  </plugin>

對我來說,當使用正確版本的logback-classic時,問題就解決了。

版本1.7.36中的 SLF4J 似乎只能與版本1.2.11中的logback-classic一起使用。

暫無
暫無

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

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