gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:54:23 24 4
gpt4 key购买 nike

在顶级 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com