简体   繁体   English

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

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

I have a maven project which use slf4j with logback as the logger.我有一个 maven 项目,它使用带有 logback 的 slf4j 作为记录器。 I could see that both artifact are in my maven dependencies tree item.我可以看到这两个工件都在我的 maven 依赖关系树项中。 But whenever I tried to run my project, I keep prompted:但是每当我尝试运行我的项目时,我都会提示:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J:无法加载 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. SLF4J:默认为无操作 (NOP) 记录器实现 SLF4J:有关详细信息,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder

I check the link, and it was said:我检查了链接,有人说:

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. 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.

I have add the logback-classic to my project (I'm using 1.0.9), but I keep prompted that message and my log doesn't running.我已经将 logback-classic 添加到我的项目中(我使用的是 1.0.9),但我一直提示该消息并且我的日志没有运行。

Could anyone help me to solve this problem?谁能帮我解决这个问题? Thanks.谢谢。

UPDATE:更新:

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

and here is my logback.xml:这是我的 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>

How I use it in my program:我如何在我的程序中使用它:

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);
    }
}

Here is my maven dependencies:这是我的 maven 依赖项:

Maven 依赖项

Had the same error.有同样的错误。 I went to Maven Repository, and looked for the latest non-beta versions.我去了 Maven Repository,寻找最新的非测试版。 Simply changing the versions worked.只需更改版本即可。 A working pom:一个工作的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>

Guess this happens when you have a custom sbt task and the task use slf4j.猜猜当您有自定义 sbt 任务并且任务使用 slf4j 时会发生这种情况。 custom sbt task runned with runner has classPaths which commonly specified as使用runner自定义 sbt 任务具有通常指定为的类路径

(dependencyClasspath in Compile) value

This caused the error where logback-classic commonly appended on % runtime .这导致了logback-classic通常附加在% runtime上的错误。

To solve this error, you have to include logback-classic into libraryDependencies on compile at wherever the custom task defined or used.要解决此错误,您必须在定义或使用自定义任务的任何libraryDependenciescompile时将logback-classic包含到libraryDependencies

You can only have that specific error message if logback-classic or logback-core isn't on the classpath.如果 logback-classic 或 logback-core 不在类路径中,您只能收到该特定的错误消息。 As they are in your maven dependencies (see pom) and in your eclipse classpath (see screenshot), this is weird.由于它们位于您的 maven 依赖项(请参阅 pom)和您的 eclipse 类路径(请参阅屏幕截图)中,因此这很奇怪。

First, check if you're still getting the same error message (maybe you fixed that problem and now got a different error message).首先,检查您是否仍然收到相同的错误消息(也许您解决了该问题,现在收到了不同的错误消息)。

Second, in eclipse, open type "StaticLoggerBinder" and put a breakpoint there.其次,在 eclipse 中,打开类型“StaticLoggerBinder”并在那里放置一个断点。 Also put a breakpoint in "LoggerFactory.getLogger".还要在“LoggerFactory.getLogger”中放置一个断点。 Then debug.然后调试。

I faced same issue.我遇到了同样的问题。 The problem was the artifact "slf4j-log4j12" not included in the jar file.问题是 jar 文件中未包含工件“slf4j-log4j12”。 It should work when you run the application via IDE since dependency is already added to the pom file.当您通过 IDE 运行应用程序时,它应该可以工作,因为依赖项已经添加到 pom 文件中。 It's actually a maven packaging issue.这实际上是一个maven包装问题。 You need to add following plugin to the pom file.您需要将以下插件添加到 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>

For me the problem was solved when using the correct version of logback-classic .对我来说,当使用正确版本的logback-classic时,问题就解决了。

SLF4J in version 1.7.36 only seems to work with logback-classic in version 1.2.11 .版本1.7.36中的 SLF4J 似乎只能与版本1.2.11中的logback-classic一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder”。 Java应用程序错误 - SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. Java Application Error SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。 在运行JUnit测试时 - SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. when running JUnit test 错误[STDERR] SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder” - ERROR [STDERR] SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder” Intellij IDEA SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder” - Intellij IDEA SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder” 无法解决“ SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder””的问题 - Can't solve 'SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”' issue SLF4J:无法加载 class “org.slf4j.impl.StaticLoggerBinder” - SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”IntelliJ 问题 - SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" IntelliJ issue Apache jena:SLF4J:无法加载 class“org.slf4j.impl.StaticLoggerBinder” - Apache jena: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder”的API - SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”, API Hadoop CDH4错误:SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder” - Hadoop CDH4 Error:SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM