如何在使用log4j2执行单元测试期间禁用打印到控制台的错误日志?

时间:2017-12-11 07:33:42

标签: java unit-testing logging log4j2 error-logging

升级到Log4j2后,错误日志在单元测试执行期间打印到控制台。

我尝试在配置文件中添加status=FATAL以避免在控制台上打印错误。

# ----------------------------------------------------------------
# LOGGING
# ----------------------------------------------------------------

# Note - this section is similar to the log4j2 properties syntax
# https://logging.apache.org/log4j/2.x/manual/configuration.html#Properties

status=FATAL;
test.*.log4j2.rootLogger.level=INFO;
*.*.log4j2.rootLogger.level=INFO;

*.*.log4j2.rootLogger.appenderRefs=(APPLICATION);
*.*.log4j2.appenders=(APPLICATION);

*.*.log4j2.appender.APPLICATION.type=AmazonRollingRandomAccessFile;
*.*.log4j2.appender.APPLICATION.name=APPLICATION;
*.*.log4j2.appender.APPLICATION.filePattern="var/output/logs/$APP.%d{yyyy-MM-dd-HH}";
*.*.log4j2.appender.APPLICATION.layout.type=PatternLayout;
# Standard log format but with the purchase id appended after the message.
# This location is to not break rtla processing, which parses everything up-to & including the logger (%c)
*.*.log4j2.appender.APPLICATION.layout.pattern="%d{DATE} [%p] %X{RequestId} (%t) %c: %m [Purchase: %X{PurchaseId}]%n";

reference doc

但似乎没有考虑status,它仍然在控制台上打印日志。

我尝试了以下所有选项,但没有运气:

status=FATAL; ..log4j2.status=INFO; ..log4j2.status=ERROR;

1 个答案:

答案 0 :(得分:0)

status的{​​{1}}仅与Configuration本身的内部日志记录有关,即您可以使用它来调试log4j2的配置,但它不是用于您的实际应用。

如果要禁用应用程序的所有日志 ,可以将配置的log4j2设置为ThresholdFilter

off

或者您可以将根记录器设置为filter.threshold.type = ThresholdFilter filter.threshold.level = off (前提是您没有定义任何其他记录器):

level = off

一个可行的完整示例:

rootLogger.level = off