Exception with Slf4j with log4j2 implementation
Asked Answered
M

3

6

I'm getting the following while setting slf4j on top log4j 2 implementation. Even though I configured log4J2 to be my implementation for slf4j interface..I'm still seeing No SLF4J providers were found. Not sure what I'm missing

Errors in logs

01-10-2022 19:22:46,115 ERROR [stderr] (MSC service thread 1-5) SLF4J: No SLF4J providers were found.
01-10-2022 19:22:46,115 ERROR [stderr] (MSC service thread 1-5) SLF4J: Defaulting to no-operation (NOP) logger implementation
01-10-2022 19:22:46,115 ERROR [stderr] (MSC service thread 1-5) SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
01-10-2022 19:22:46,115 ERROR [stderr] (MSC service thread 1-5) SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions prior to 1.8.
01-10-2022 19:22:46,116 ERROR [stderr] (MSC service thread 1-5) SLF4J: Ignoring binding found at [vfs:/content/abc.war/WEB-INF/lib/log4j-slf4j-impl-2.17.0.jar/org/slf4j/impl/StaticLoggerBinder.class]
01-10-2022 19:22:46,116 ERROR [stderr] (MSC service thread 1-5) SLF4J: See http://www.slf4j.org/codes.html#ignoredBindings for an explanation.

Properties file

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=/sasa.log
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=100MB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.DatePattern='.'yyyy-MM-dd'.log'
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n

Java file

import org.slf4j.Logger; import org.slf4j.LoggerFactory;

@Component
public class ABC{
    
    private static final Logger log = LoggerFactory.getLogger(abc.class);
    
    public String getCall(String url) throws Exception {
        try {
            log.info("getCall url - " + url );
            response = new String(get.getResponseBody(), UTFSTR);
            log.info(": Response code:"+response);
        } catch (Exception e) {
            log.error(" getCall - EXCEPTION - " + e.getLocalizedMessage());
            throw e;
        } 
        return response;
    } 
}

pom file configured.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.2</version>
    <properties>
        <java.version>1.8</java.version>
        <slf4j.version>2.0.0-alpha5</slf4j.version>
        <log4j2.version>2.17.0</log4j2.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies> 
Motorboating answered 11/1, 2022 at 0:35 Comment(0)
W
9

There are two different SLF4J bindings for Log4j 2.x. Since you want to use SLF4J 1.8+, you imported the wrong one. Use:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j18-impl</artifactId>
</dependency>

instead of:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
</dependency>

Edit: Since version 2.19.0 of Log4j2 the log4j-slf4j18-impl binding (that supported only the 1.8.x beta branch) was replaced with log4j-slf4j2-impl (which supports the 2.x SLF4J branch):

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j2-impl</artifactId>
</dependency>

Remark: The Log4j configuration in your question is for Log4j 1.2. It will not work with Log4j 2.x and Spring Boot.

Winder answered 11/1, 2022 at 3:52 Comment(2)
Thanks!! but when I configure this... I'm getting this error... INFO [org.hibernate.validator.internal.util.Version] (MSC service thread 1-6) HV000001: Hibernate Validator 6.0.22.Final ERROR [stderr]Unexpected problem occured during version sanity check ERROR [stderr] Reported exception: ERROR [stderr]java.lang.AbstractMethodError: Receiver class org.apache.logging.slf4j.SLF4JServiceProvider does not define or inherit an implementation of the resolved method 'abstract java.lang.String getRequestedApiVersion()' of interface org.slf4j.spi.SLF4JServiceProvider.Motorboating
See this question.Winder
D
2

For me, the right fix was changing log4j-slf4j-impl to log4j-slf4j2-impl.

Reference: https://logging.apache.org/log4j/2.x/log4j-slf4j-impl/

Dewar answered 2/10, 2022 at 4:6 Comment(0)
P
2

Spring boot 3 version of org.springframework.boot:spring-boot-starter-log4j2 pulls in log4j-slf4j2-impl, but Spring Boot 2 version of org.springframework.boot:spring-boot-starter-log4j2 pulls in log4j-slf4j-impl. In Gradle for Spring Boot 2.7.6 when using slf4j 2.0.x with log4j 2.19.0, I had to do this:

implementation('org.springframework.boot:spring-boot-starter-log4j2') {
    exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl'
}
implementation 'org.apache.logging.log4j:log4j-slf4j2-impl:2.19.0'
Pocketful answered 3/1, 2023 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.