gpt4 book ai didi

java - 使用 log4j2.xml 初始化 slf4j

转载 作者:行者123 更新时间:2023-12-05 05:14:36 29 4
gpt4 key购买 nike

我想在 log4j 上使用 slf4j。我在 pom.xml 中添加了以下依赖项(我对 slf4j 使用 1.7.25,对 log4j2 使用 2.10.0):

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>


<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>

一切都构建得很好,没有编译错误或缺少依赖项,但我未能在我的类中指定负责初始化 Logger 的配置 (log4j2.xml) 文件。在这种情况下,它总是打印相同的警告

log4j:WARN No appenders could be found for logger (com.mypackage.etc).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

我搜索了一种提供配置文件的正确方法,结果是这样的:

LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");
context.setConfigLocation(file.toURI());

问题是,在我的情况下,LogManager.getContext(false) 将始终返回 Slf4JLoggerContext 的实例(考虑到我使用 slf4j 作为记录器的外观)并且初始化将失败并出现 ClassCastException。我试图存储 Slf4JLoggerContext 的那个实例,但它没有为上下文提供 setter 。我也没有找到从 log4j 中检索 LoggerContext 的方法。

有什么方法可以向 slf4j 提供配置文件 (log4j2.xml) 以便查看所有的 appender 和 loggers?

更新考虑这是我的配置文件(我替换了原始包和附加程序名称):

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="def.files.backup.count">10</Property>
<Property name="log.file.path">${oo.home}/var/logs</Property>
<Property name="def.file.max.size">10MB</Property>
<Property name="log.level">WARN</Property>
</Properties>

<ThresholdFilter/>

<Appenders>
<RollingFile name="Appender1" fileName="${log.file.path}/file1.log" maxFileSize="${def.file.max.size}"
maxBackupIndex="${def.files.backup.count}">
<PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>

<RollingFile name="Appender2" fileName="${log.file.path}/file2.log"
maxFileSize="${def.file.max.size}"
maxBackupIndex="${def.files.backup.count}">
<PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>

<RollingFile name="Appender3" fileName="${log.file.path}/file3.log"
maxFileSize="${def.file.max.size}"
maxBackupIndex="${def.files.backup.count}">
<PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>

</Appenders>

<Loggers>
<AsyncLogger name="com.package1.oo" level="${log.level}" additivity="false">
<AppenderRef ref="Appender1"/>
</AsyncLogger>

<AsyncLogger name="io.package2" level="${log.level}" additivity="false">
<AppenderRef ref="Appender2"/>
</AsyncLogger>

<AsyncLogger name="com.package3.package3" level="${log.level}" additivity="false">
<AppenderRef ref="Appender3"/>
</AsyncLogger>

<AsyncLogger name="org.package4" level="${log.level}">

</AsyncLogger>

<AsyncLogger name="com.package5.Class1" level="${log.level}">

</AsyncLogger>

<AsyncRoot level="${log.level}">
<AppenderRef ref="Appender1"/>
<AppenderRef ref="Appender2"/>
<AppenderRef ref="Appender3"/>

</AsyncRoot>
</Loggers>

</Configuration>

最佳答案

我已经测试了这个配置。 log4j2 文件必须在您的类路径中。

行家

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

log4j2.xml

此外,添加 xsd 有助于您创建配置。我已经为 spring 框架添加了 Logger。请注意,需要 jcl-over-slf4j 才能使用 spring 的内部日志记录

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error"
xmlns="http://logging.apache.org/log4j/2.0/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config
https://raw.githubusercontent.com/apache/logging-log4j2/master/log4j-core/src/main/resources/Log4j-config.xsd">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
<Logger name="org.springframework" level="error">
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
</Configuration>

关于java - 使用 log4j2.xml 初始化 slf4j,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52300045/

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