spring-boot - 带有 log4j2 实现的 Slf4j 异常

标签 spring-boot log4j2 slf4j

在顶级 log4j 2 实现上设置 slf4j 时,我得到以下信息。即使我将 log4J2 配置为我对 slf4j 接口(interface)的实现。我仍然看到没有找到 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> 

最佳答案

two different SLF4J bindings对于 Log4j 2.x。由于您要使用 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 绑定(bind)(仅支持 1.8.x beta 分支)被替换为 log4j-slf4j2-impl(支持 2.x SLF4J 分支)。

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

关于spring-boot - 带有 log4j2 实现的 Slf4j 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70660379/

相关文章:

apache-spark - 我可以将具有 log4j.appender.file.File 的 dataproc 的 log4j.properties 文件作为 gcs 路径吗?

java - log4j2 使用 slf4j 关闭所有附加程序

slf4j - java.lang.NoSuchMethodError : ch. qos.logback.classic.LoggerContext.getBithTime()

java - 为什么不建议每次都调用 LoggerFactory.getLogger(...)?

java - Spring Boot 应用程序未部署 - 无法创建 XADataSource 实例

spring-boot - Spring boot - 如何使用故障转移 URL 连接到外部 ActiveMQ 主/从集群

java - 使用 Spring Boot Maven 着色无法正常工作

java - Aspectj 通过外部库实现功能

java - log4j2 pkg 中记录器接口(interface)的继承方法

jetty - 为 Jetty 的 Maven 插件配置日志记录?