简体   繁体   中英

How to disable error logs being printed to the console during unit tests execution with log4j2?

After upgrading to Log4j2, error logs are printing to console during unit tests execution.

I tried to add status=FATAL in the configuration file to avoid error printing on console.

# ----------------------------------------------------------------
# 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";

The reference doc .

But seems it is not taking status into consideration and it is still printing logs on console.

I tried with all the options below as well, but no luck:

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

The status of the Configuration only relates to the internal logging of log4j2 itself, ie you can use it to debug your configuration of log4j2 , but it is not used for your actual application.

If you want to disable all logging for your application , you can set the ThresholdFilter of your configuration to off :

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

or you can set your root logger to level = off (provided you don't have any other loggers defined):

rootLogger.level = off

A complete example of something that would work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM