【发布时间】:2018-05-11 05:27:08
【问题描述】:
现成的 Spring Boot 模板:spring-boot-starter-log4j2、spring-boot-starter-web 和 spring-boot-starter-actuator。
我试图让 log4j2 只记录 Spring 相关的消息,如果这些消息是 WARN 及以上。其余的,我希望它记录DEBUG 及以上。
Log4j2 配置如下所示:
<Configuration status="DEBUG">
<Appenders>
<Console name="STDERR" target="SYSTEM_ERR">
<PatternLayout>
<pattern>%highlight{[%level]} %logger %message %throwable%n</pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="warn" />
<Root level="debug">
<AppenderRef ref="STDERR"/>
</Root>
</Loggers>
</Configuration>
由于某种原因它不起作用:org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping 附加到 STDERR。 org.springframework.boot.web.servlet.FilterRegistrationBean、org.springframework.boot.logging.ClasspathLoggingApplicationListener 和许多其他人也是如此。
我可以手动设置org.springframework.boot.actuate.endpoint.mvc、org.springframework.boot.web.servlet、org.springframework.boot.logging 等的级别。但不知道为什么它是必要的。
那些记录器不是应该绑定到org.springframework 记录器配置并使用它的WARN 级别吗?
Log4j 诊断输出:
21:03:58,102 Loaded configuration from /src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml
21:03:58,103 Starting LoggerContext[name=42a57993, org.apache.logging.log4j.core.LoggerContext@6193932a] with configuration XmlConfiguration[location=/src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml]...
21:03:58,104 Initializing configuration XmlConfiguration[location=/src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml]
21:03:58,105 Installed script engines
21:03:58,120 Oracle Nashorn Version: 1.8.0_152, Language: ECMAScript, Threading: Not Thread Safe, Compile: true, Names: {nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript}
21:03:58,120 PluginManager 'Core' found 110 plugins
21:03:58,120 PluginManager 'Level' found 0 plugins
21:03:58,123 Log4j2 ConfigurationScheduler: No scheduled items
21:03:58,123 PluginManager 'Lookup' found 13 plugins
21:03:58,124 Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
21:03:58,131 PluginManager 'TypeConverter' found 23 plugins
21:03:58,138 PatternLayout$Builder(pattern="%highlight{[%level]} %logger %message %throwable%n", PatternSelector=null, Configuration(/src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml), Replace=null, charset="null", alwaysWriteExceptions="null", noConsoleNoAnsi="null", header="null", footer="null")
21:03:58,139 PluginManager 'Converter' found 44 plugins
21:03:58,141 Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
21:03:58,147 ConsoleAppender$Builder(target="SYSTEM_ERR", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%highlight{[%level]} %logger %message %throwable%n), name="STDERR", Filter=null)
21:03:58,147 Starting OutputStreamManager SYSTEM_ERR.false.false
21:03:58,147 Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
21:03:58,148 createAppenders(={STDERR})
21:03:58,149 Building Plugin[name=logger, class=org.apache.logging.log4j.core.config.LoggerConfig].
21:03:58,154 createLogger(additivity="true", level="WARN", name="org.springframework", includeLocation="null", ={}, ={}, Configuration(/src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml), Filter=null)
21:03:58,155 Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
21:03:58,155 createAppenderRef(ref="STDERR", level="null", Filter=null)
21:03:58,156 Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
21:03:58,156 createLogger(additivity="null", level="DEBUG", includeLocation="null", ={STDERR}, ={}, Configuration(/src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml), Filter=null)
21:03:58,157 Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
21:03:58,157 createLoggers(={org.springframework, root})
21:03:58,158 Configuration XmlConfiguration[location=/src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml] initialized
21:03:58,158 Starting configuration XmlConfiguration[location=/src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml]
21:03:58,158 Started configuration XmlConfiguration[location=/src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml] OK.
21:03:58,159 Shutting down OutputStreamManager SYSTEM_OUT.false.false-2
21:03:58,159 Shut down OutputStreamManager SYSTEM_OUT.false.false-2, all resources released: true
21:03:58,159 Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@654f0d9c OK
21:03:58,162 Registering MBean org.apache.logging.log4j2:type=42a57993
21:03:58,163 Registering MBean org.apache.logging.log4j2:type=42a57993,component=StatusLogger
21:03:58,163 Registering MBean org.apache.logging.log4j2:type=42a57993,component=ContextSelector
21:03:58,164 Registering MBean org.apache.logging.log4j2:type=42a57993,component=Loggers,name=
21:03:58,165 Registering MBean org.apache.logging.log4j2:type=42a57993,component=Loggers,name=org.springframework
21:03:58,165 Registering MBean org.apache.logging.log4j2:type=42a57993,component=Appenders,name=STDERR
21:03:58,165 LoggerContext[name=42a57993, org.apache.logging.log4j.core.LoggerContext@6193932a] started OK with configuration XmlConfiguration[location=/src/SpringBootLoggingTest/log-configuration/log4j2-desktop.xml].
21:03:58,173 AsyncLogger.ThreadNameStrategy=CACHED
【问题讨论】:
标签: spring spring-boot logging log4j2