Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log4j and logback conflict when running as packaged application #4341

Closed
scale9 opened this issue Oct 30, 2015 · 8 comments
Closed

log4j and logback conflict when running as packaged application #4341

scale9 opened this issue Oct 30, 2015 · 8 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@scale9
Copy link

scale9 commented Oct 30, 2015

When using spring-boot-starter-log4j2, the following exception is thrown while starting the application as a packaged application (java -jar target/myproject-0.0.1-SNAPSHOT.jar):

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:xxx/target/myproject-0.0.1-SNAPSHOT.jar!/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:xxx/target/myproject-0.0.1-SNAPSHOT.jar!/lib/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from jar:file:xxx/target/myproject-0.0.1-SNAPSHOT.jar!/lib/log4j-slf4j-impl-2.4.1.jar!/). If you are using Weblogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml Object of class [org.apache.logging.slf4j.Log4jLoggerFactory] must be an instance of class ch.qos.logback.classic.LoggerContext

When running the application with Maven plugin (mvn spring-boot:run), it starts successfully, and I've noticed that the ordering of the classpath is different between mvn spring-boot:run and java -jar and a different binding is being picked up:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/yang/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/yang/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.3/log4j-slf4j-impl-2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

The exception is identical to #541, although I'm using Maven 3.2.1 to package the jar, and there's no issue with running the application with Maven directly as mentioned previously.
Relevant dependencies:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.BUILD-SNAPSHOT</version>
</parent>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

We are using Java 1.8.0_66.

@snicoll
Copy link
Member

snicoll commented Oct 30, 2015

The classpath ordering is a separate issue but ideally you shouldn't rely on that.

As for the SLF4J error message, please check the documentation that explains you how to configure log4j2 with Spring Boot.

@snicoll snicoll closed this as completed Oct 30, 2015
@snicoll snicoll added question status: invalid An issue that we don't feel is valid labels Oct 30, 2015
@scale9
Copy link
Author

scale9 commented Oct 30, 2015

Thanks. Turned out I had another (non-springboot) dependency using the logback implementation.

@binnchx
Copy link

binnchx commented Sep 17, 2016

I encountered the same problem, but I prefer to use log4j, how could i do ?thanks.

@philwebb
Copy link
Member

@binnchx This issue has been closed for a while now. Please ask on stackoverflow.com instead.

@pozelim
Copy link

pozelim commented Sep 29, 2017

@scale9 @binnchx have you found a solution for that?

@archenroot
Copy link

archenroot commented Oct 10, 2017

I am mixing now few starters (springboot web, data, jpa, etc.) in multimodule project with Vaadin and custom libraries and get away from logback can become really nasty... I don't ask here a question, but instead of this mess, something like switch in application.properties defining the provider to pull automatically the right dependencies might, or anything else more easy ... this is simply mess for new users. But spring is not for new users 💃 --- I fixed my case myself, but the errors are not always straightforwardly leading to resolution....

As for reference from my point of view:

  1. I added springboot starter explicitely for log4j2 in parent pom:
<!-- LOGGING -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
  1. I added exclusion everywhere I detected logback is pulled in, in my case:
 <!-- SpringBoot AMQP -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!-- JPA -->
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Surprisingly the starter-parent doesn't pull in the logback either defined as type pom, but I put it as standard dependency -> no logback pulled in:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${springboot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

The problem in my case before fixed was also followed by logging failing completely as bellow (plus warding about multiple slf4j providers of course):

Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org/apache/logging/log4j/spi/AbstractLoggerAdapter
  at java.lang.ClassLoader.defineClassl(Native Method)
  ...

By completely get rid of lopback and enforce instead springboot starter for log4j2, both the warning and the error disapeared.

I also note, that I don't use (in the moment) log4j2-spring.xml, but standard log4j2.xml file which could possibly cause some unexpected behaviour, but probably other, not with classpath

@mrprk3
Copy link

mrprk3 commented Apr 22, 2022

hii Can you please help me i am getting error logback core and logback classic status as forbidden in my spring boot project

@mrprk3
Copy link

mrprk3 commented Apr 22, 2022

my mail id pramanikatim3@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

7 participants