gpt4 book ai didi

java - 如何将不同的记录器记录到不同的文件中

转载 作者:行者123 更新时间:2023-11-30 03:47:27 25 4
gpt4 key购买 nike

这是我的java代码

private static final Logger SUCCESS = LogManager.getLogger("success");
private static final Logger ERROR = LogManager.getLogger("error");

我需要为这些日志创建 2 个日志文件。 (我试图遵循 Creating multiple log files of different content with log4j )但我认为它不完全相同。但这是我在 log4j.properties 中创建的内容。

log4j.rootLogger=TRACE, SuccessAppender, ErrorAppender

# setup SuccessAppender
log4j.appender.SuccessAppender=org.apache.log4j.RollingFileAppender
log4j.appender.SuccessAppender.Threshold=INFO
log4j.appender.SuccessAppender.File=success.log
log4j.appender.SuccessAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.SuccessAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# setup ErrorAppender
log4j.appender.ErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.ErrorAppender.Threshold=INFO
log4j.appender.ErrorAppender.File=error.log
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

我想知道如何将此“成功”日志映射到“SuccessAppender”。

最佳答案

首先,您必须确保 RootLogger 没有记录任何内容。这可以通过不同的方式实现,但我的第一个解决方案是让它附加到 NullAppender .

log4j.rootLogger=OFF, NullAppender

# setup NullAppender
log4j.appender.NullAppender=org.apache.log4j.varia.NullAppender

然后,您必须为记录器设置日志级别和附加程序(成功错误):

log4j.logger.success = TRACE, SuccessAppender
log4j.logger.error = TRACE, ErrorAppender

剩下的 log4j.properties 保持原样。

<小时/>

如果您不想使用 NullAppender,另一种解决方案是在记录器上设置可加性标志:

log4j.rootLogger=OFF, SuccessAppender, ErrorAppender

log4j.logger.success = TRACE, SuccessAppender
log4j.additivity.success = false
log4j.logger.error = TRACE, ErrorAppender
log4j.additivity.error = false
<小时/>

有关可加性的更多信息可以在 Log4J manual 中找到。 .

Appender Additivity

The output of a log statement of logger C will go to all the appenders in C and its ancestors. This is the meaning of the term "appender additivity".

However, if an ancestor of logger C, say P, has the additivity flag set to false, then C's output will be directed to all the appenders in C and its ancestors upto and including P but not the appenders in any of the ancestors of P.

Loggers have their additivity flag set to true by default.

关于java - 如何将不同的记录器记录到不同的文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25280842/

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