带有 log4j2 实现的 Slf4j 异常

在顶级 log4j 2 实现上设置 slf4j 时,我得到以下信息。即使我将 log4J2 配置为我的 slf4j 接口实现..我仍然看到没有找到 SLF4J 提供程序。不知道我错过了什么

日志中的错误

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.

属性文件

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

导入 org.slf4j.Logger;导入 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 文件配置。

<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> 
stack overflow Exception with Slf4j with log4j2 implementation
原文答案

答案:

作者头像

Log4j 2.x 有 two different SLF4J bindings 。由于您想使用 SLF4J 1.8+,因此您导入了错误的。利用:

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

代替:

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

编辑: 从 Log4j2 的 2.19.0 版本开始, log4j-slf4j18-impl 绑定(仅支持 1.8.x beta 分支)被替换为 log4j-slf4j2-impl (支持 2.x SLF4J 分支)。

备注:您问题中的 Log4j 配置适用于 Log4j 1.2。它不适用于 Log4j 2.x 和 Spring Boot。

作者头像

对我来说,正确的解决方法是将 log4j-slf4j-impl 更改为 log4j-slf4j2-impl

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