jay moyo
jay moyo

Reputation: 29

Excluding Logback dependency from spring-boot-starter-web stops log4j system from initialising properly

I was previously having trouble with multiple bindings error for spring application, i learned that the fix was to exclude a component of the spring-boot-starter-web dependency. The issue i'm having now is that the log4j wants to be initilized but the logback-classic dependency.

heres the error i had initially:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]

i can't exclude the slf4-log4j12 dependency because it is required for that module to run and cant exclude the logback dependency because it needs to initialised when then spring app runs.

heres the issues i get when exclude logback:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

heres the error i get when i exclude slf4j-log4j:

Exception in thread "main" java.lang.AbstractMethodError: org.springframework.boot.context.config.ConfigFileApplicationListener.supportsSourceType(Ljava/lang/Class;)Z

Upvotes: 0

Views: 1410

Answers (1)

Why do you think that logback is a dependency?

Logback directly implements slf4j, you use logback through the sl4j api so your code will never depend on logback, but it may depend on slf4j.

The issue you outlined here:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Is the result of your project not having a configuration specified for log4j.

The library slf4j-log4j12-1.6.1.jar is a sl4j binding for log4j. What this means is any code logging using slf4j will have its logging forwarded to log4j.

Excluding logback is the correct approach. All you need after excluding logback is a configuration for log4j.

Please refer to the log4j documentation about how to specify a configuration. If you think the project has existing configuration, look for a log4j.properties or log4j.xml file. Make sure they are on the class path. A question with a similar problem was asked here: Should log4.properties be on the classpath?

Upvotes: 2

Related Questions