首页 > 解决方案 > log4j2 使用 slf4j 关闭所有 appender

问题描述

我可以对我的 log4j2 配置使用一些帮助。当我调试记录器时,它似乎关闭了所有的附加程序,然后我再也没有收到任何应用程序日志。幸运的是,我知道 log4j 至少可以找到属性文件并提供记录器初始化调试输出。

这是我的 log4j2.properties 文件:

status = debug
name = PropertiesConfig
filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = info

appenders = console

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

appender.file.type = File
appender.file.name = application
appender.file.fileName=${filename}/propertieslogs.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

这是记录器调试初始化输出:

DEBUG Starting LoggerContext[name=ROOT] from configuration at null
DEBUG Starting LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext@448dc39b] with configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@70ffa0b4...
DEBUG Shutdown hook enabled. Registering a new one.
DEBUG Apache Log4j Core 2.11.1 initializing configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@70ffa0b4
DEBUG Installed 1 script engine
DEBUG Oracle Nashorn version: 1.8.0_162, language: ECMAScript, threading: Not Thread Safe, compile: true, names: [nashorn, Nashorn, js, JS, JavaScript, javascript, ECMAScript, ecmascript], factory class: jdk.nashorn.api.scripting.NashornScriptEngineFactory
DEBUG PluginManager 'Core' found 118 plugins
DEBUG PluginManager 'Level' found 0 plugins
DEBUG PluginManager 'Lookup' found 14 plugins
DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
DEBUG PluginManager 'TypeConverter' found 26 plugins
DEBUG createAppenderRef(ref="STDOUT", level="null", Filter=null)
DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
DEBUG createLogger(additivity="null", level="DEBUG", includeLocation="null", ={STDOUT}, ={}, Configuration(PropertiesConfig), Filter=null)
DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
DEBUG createLoggers(={root})
DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
DEBUG PatternLayout$Builder(pattern="%d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n", PatternSelector=null, Configuration(PropertiesConfig), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
DEBUG PluginManager 'Converter' found 44 plugins
DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.ConsoleAppender].
DEBUG ConsoleAppender$Builder(target="null", follow="null", direct="null", bufferedIo="null", bufferSize="null", immediateFlush="null", ignoreExceptions="null", PatternLayout(%d{yy-MM-dd HH:mm:ss:SSS} %-5p %c{1}:%L - %m%n), name="STDOUT", Configuration(PropertiesConfig), Filter=null)
DEBUG Starting OutputStreamManager SYSTEM_OUT.false.false
DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
DEBUG createAppenders(={STDOUT})
DEBUG Building Plugin[name=filter, class=org.apache.logging.log4j.core.filter.ThresholdFilter].
DEBUG createFilter(level="INFO", onMatch="null", onMismatch="null")
DEBUG Configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@70ffa0b4 initialized
DEBUG Starting configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@70ffa0b4
DEBUG Started configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@70ffa0b4 OK.
DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false-1
DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false-1, all resources released: true
DEBUG Appender DefaultConsole-1 stopped with status true
DEBUG Stopped org.apache.logging.log4j.core.config.DefaultConfiguration@605d8cc OK
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=StatusLogger
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=ContextSelector
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=Loggers,name=
DEBUG Registering MBean org.apache.logging.log4j2:type=ROOT,component=Appenders,name=STDOUT
DEBUG org.apache.logging.log4j.core.util.SystemClock does not support precise timestamps.
DEBUG LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext@448dc39b] started OK with configuration org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@70ffa0b4.
DEBUG Log4jServletContextListener ensuring that Log4j starts up properly.
DEBUG Log4jServletContextListener ensuring that Log4j shuts down properly.
DEBUG Removing LoggerContext for [ROOT].
DEBUG Stopping LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext@448dc39b]...
DEBUG Shutting down OutputStreamManager SYSTEM_OUT.false.false
DEBUG Shut down OutputStreamManager SYSTEM_OUT.false.false, all resources released: true
DEBUG Appender STDOUT stopped with status true
DEBUG Stopped org.apache.logging.log4j.core.config.properties.PropertiesConfiguration@70ffa0b4 OK
DEBUG Stopped LoggerContext[name=ROOT, org.apache.logging.log4j.core.LoggerContext@448dc39b] with status true

谢谢你的帮助!我需要它。

我真的很想让异步附加程序在某个时候工作,但很难通过 log4j2 的 log4j2.properties 语法找到示例

PS - 这是我的 pom.xml:

    <!-- log4j is used for logging within the application -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.11.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.11.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <version>2.11.1</version>
        <scope>runtime</scope>
    </dependency>

    <!-- slf4j is used to tie Spring to log4j  -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.11.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.25</version>
    </dependency>

标签: javaspring-mvclog4jslf4jlog4j2

解决方案


我刚刚遇到了类似的问题。就我而言,我能够接收应用程序日志,但似乎是默认的 log4j2 配置。日志是信息级别。没有应用 log4j2 配置 (XML) 中为记录器设置的级别。我在 OP 发布的启动日志中看到了非常相似的输出,包括有关“关闭 OutputStreamManager”的消息。

我的是一个 spring-boot 应用程序,我能够通过禁用 spring-boot LoggingSystem来解决问题。我将此系统变量添加到我的启动命令中: -Dorg.springframework.boot.logging.LoggingSystem=none

一旦我这样做了,根据我的 log4j2 XML 配置中设置的级别,日志记录按预期工作。它似乎是 spring-boot 劫持了日志系统,而不是 SLF4J 和 LOG4J2 之间的冲突。

请注意,我仍然看到相同的日志记录启动消息——OutputStreamManager 仍在关闭。推测:这是正常行为,可能只是关闭了打印 log4j2 初始化过程的引导记录器。

版本:

  • log4j:2.13.3
  • slf4j:1.7.30
  • 弹簧启动:2.2.7-RELEASE

推荐阅读