【问题标题】:How to disable error logs being printed to the console during unit tests execution with log4j2?如何在使用 log4j2 执行单元测试期间禁用将错误日志打印到控制台?
【发布时间】:2017-12-11 07:33:41
【问题描述】:

升级到 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;

【问题讨论】:

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


    【解决方案1】:

    Configuration 中的status 仅与log4j2 本身的内部日志相关,即您可以使用它来调试您的log4j2 的配置,但它并不用于您的实际应用。

    如果您想为您的应用程序禁用所有日志记录,您可以将配置的ThresholdFilter 设置为off:

    filter.threshold.type = ThresholdFilter
    filter.threshold.level = off
    

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

    rootLogger.level = off
    

    可行的完整示例:

    status = warn
    name = TestConfig
    
    filter.threshold.type = ThresholdFilter
    filter.threshold.level = off
    
    appender.list.type = List
    appender.list.name = List
    appender.list.filter.threshold.type = ThresholdFilter
    appender.list.filter.threshold.level = trace
    
    logger.whatever.name = com.relentlesscoding.logging
    logger.whatever.level = trace
    logger.whatever.additivity = false
    logger.whatever.appenderRef.whatever.ref = List
    
    rootLogger.level = trace
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 2020-10-02
      • 1970-01-01
      • 2023-02-09
      • 2021-08-04
      • 1970-01-01
      • 2010-10-22
      • 2017-06-08
      相关资源
      最近更新 更多