SLF4J with logback still prompt failed to load class "org.slf4j.impl.StaticLoggerBinder"
Asked Answered
P

6

18

I have a maven project which use slf4j with logback as the logger. I could see that both artifact are in my maven dependencies tree item. But whenever I tried to run my project, I keep prompted:

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.

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.

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.

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:

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

Precinct answered 26/2, 2015 at 22:50 Comment(16)
I guess you have already seen #11917206Rembert
Is logback-core on your class path too? Can you edit your post to contain your exact Java invocation?Elidiaelie
@GrzegorzOledzki : yes I have seen it and try it out, but it doesn't solve my problem. My problem is different, it was not the eclipse fault, because the log was actually not running.Precinct
@Elidiaelie : yes, the logback-core jar library was in my classpath, I use maven and already added the dependency for log-back, and I can see it from my maven dependencies tree. My java code actually didn't invoke it directly, it was invoke by other library (optaplanner)Precinct
Could you please share the maven dependencies and logback.xml configuration?Casie
logback.xml configuration won't cause this problem. We need to see the pom.xml and the way he's actually trying to run the programElidiaelie
@Casie : I have already post my pom.xml and logback.xml, please take a look at it.Precinct
@Elidiaelie : I have post my pom.xml and the way I run the program.Precinct
Not the java code, I mean like java -jar foo.jar -cp blah blahElidiaelie
Are the libs actually placed in the ../../binaries/ folder?Elidiaelie
@Elidiaelie : I just run it from eclipse. The libs was placed in my local maven repo. I will attached my screenshot for you. ThanksPrecinct
"I keep prompted that message and my log doesn't running." the freeze is not related to log configuration in any way. You have something completely different wrong.Moy
can you try to run it from mvn. you can do mvn clean install -DskipTests and then run it on server or from main class.Gracielagracile
@rishman : yes I can do mvn clean and install, and it run successfully.Precinct
@Precinct : in that case, it seems like there is something wrong with eclipse and not with project. check few things as libraries exist in java build path of the project, try Run as -> maven build from eclipse. In my experience, mvn commands from console works better than IDE maven plugins. so if its acceptable then try to use console commands.Gracielagracile
I see the StaticLoggerBinder class inside version 1.2.12 of the logback-classic jar. However, I don't see StaticLoggerBinder in versions 1.3.8 or 1.4.8 of the logback-classic jar. Has standard way of integrating logback and slf4j via logback-classic inclusion changed?Loxodromics
G
10

Had the same error. I went to Maven Repository, and looked for the latest non-beta versions. Simply changing the versions worked. A working 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>
Grumous answered 11/5, 2019 at 10:34 Comment(3)
You just saved my day. ThanksAlfie
Just mini FYI, you may get by with just including "logback-classic" as it will (usually) include "logback-core" with it. +--- ch.qos.logback:logback-classic:1.2.3 | +--- ch.qos.logback:logback-core:1.2.3Henkel
A crucial part of this solution is also the compatibility between the versions.Slope
E
6

I think there are some version mismatch. I experimented a little bit, and seems this combination works:(with latest slf4j-api 1.7.36):

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.11</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.11</version>
            <scope>runtime</scope>
        </dependency>
Ernieernst answered 19/2, 2023 at 12:13 Comment(0)
C
2

Guess this happens when you have a custom sbt task and the task use slf4j. custom sbt task runned with runner has classPaths which commonly specified as

(dependencyClasspath in Compile) value

This caused the error where logback-classic commonly appended on % runtime.

To solve this error, you have to include logback-classic into libraryDependencies on compile at wherever the custom task defined or used.

Cess answered 1/5, 2018 at 3:22 Comment(0)
O
1

You can only have that specific error message if logback-classic or logback-core isn't on the classpath. As they are in your maven dependencies (see pom) and in your eclipse classpath (see screenshot), this is weird.

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. Also put a breakpoint in "LoggerFactory.getLogger". Then debug.

Oraorabel answered 2/3, 2015 at 9:53 Comment(1)
First, I still getting the same error message whenever I tried to run the apps. Second, I have tried to put a breakpoint there, and it didn't step in when I run the apps. Any suggestion what else I should do? Been stuck here for days. ThanksPrecinct
S
1

For me the problem was solved when using the correct version of logback-classic.

SLF4J in version 1.7.36 only seems to work with logback-classic in version 1.2.11.

Slope answered 19/1, 2023 at 12:28 Comment(0)
G
0

I faced same issue. The problem was the artifact "slf4j-log4j12" not included in the jar file. It should work when you run the application via IDE since dependency is already added to the pom file. It's actually a maven packaging issue. You need to add following plugin to the pom file.

<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>
Guildroy answered 17/6, 2020 at 6:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.