我刚刚建立了一个项目以使用junit jupiter(junit 5)进行测试,无论我在xml配置中设置的级别如何,我都在TRACE级别上获得一堆log4j2输出。输出示例如下。如何甚至在解析xml之前更改log4j2记录的级别?
这是我的xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE" packages="com.travisfw">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="WARN">
<AppenderRef ref="console" />
</Root>
<Logger level="WARN" name="com.travisfw" additivity="false">
<AppenderRef ref="console" />
</Logger>
</Loggers>
</Configuration>
然后,尽管WARN是我设置的唯一级别,但我在junit报告中得到了DEBUG和TRACE输出,从它找到我的xml配置的地方开始:
2018-10-04 16:04:48,532 Test worker DEBUG Apache Log4j Core 2.11.1 initializing configuration XmlConfiguration[location=/home/.../build/resources/main/log4j2.xml]
2018-10-04 16:04:48,539 Test worker DEBUG Installed 1 script engine
2018-10-04 16:04:48,920 Test worker DEBUG Oracle Nashorn version: 1.8.0_181, language: ECMAScript, threading: Not Thread Safe, compile: true, names: [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript], factory class: jdk.nashorn.api.scripting.NashornScriptEngineFactory
2018-10-04 16:04:48,924 Test worker DEBUG Took 0.004073 seconds to load 0 plugins from package com.travisfw
2018-10-04 16:04:48,925 Test worker DEBUG PluginManager 'Core' found 117 plugins
2018-10-04 16:04:48,925 Test worker DEBUG PluginManager 'Level' found 0 plugins
2018-10-04 16:04:48,928 Test worker DEBUG PluginManager 'Lookup' found 13 plugins
2018-10-04 16:04:48,930 Test worker DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2018-10-04 16:04:48,936 Test worker TRACE TypeConverterRegistry initializing.
2018-10-04 16:04:48,937 Test worker DEBUG PluginManager 'TypeConverter' found 26 plugins
2018-10-04 16:04:48,944 Test worker DEBUG PatternLayout$Builder(pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n", PatternSelector=null, Configuration(/home/.../build/resources/main/log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2018-10-04 16:04:48,945 Test worker DEBUG PluginManager 'Converter' found 44 plugins
2018-10-04 16:04:48,946 Test worker DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
2018-10-04 16:04:48,954 Test worker DEBUG ConsoleAppender$Builder(target="SYSTEM_OUT", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n), name="console", Configuration(/home/.../build/resources/main/log4j2.xml), Filter=null)
2018-10-04 16:04:48,958 Test worker DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
2018-10-04 16:04:48,959 Test worker DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2018-10-04 16:04:48,964 Test worker DEBUG createAppenders(={console})
2018-10-04 16:04:48,965 Test worker DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2018-10-04 16:04:48,970 Test worker DEBUG createAppenderRef(ref="console", level="null", Filter=null)
2018-10-04 16:04:48,970 Test worker DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
我将为您保留完整的日志,但是它将持续一会儿。