简体   繁体   English

log4j2 使用 slf4j 关闭所有附加程序

[英]log4j2 shuts down all appenders with slf4j

I could use some help with my log4j2 configuration.我可以对我的 log4j2 配置使用一些帮助。 When I debug the logger, it looks like it shuts down all of the appenders, and then I never receive any application logs.当我调试记录器时,它看起来像是关闭了所有的 appender,然后我再也没有收到任何应用程序日志。 Luckily I know that log4j is at least finding the properties file and giving logger initialization debug output.幸运的是,我知道 log4j 至少正在查找属性文件并提供记录器初始化调试输出。

Here is my log4j2.properties file:这是我的 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

Here is the logger debug init output:这是记录器调试初始化输出:

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

Thanks for any help!谢谢你的帮助! I need it.我需要它。

I'd really like to get the async appenders working at some point, but its hard to find examples via the log4j2.properties syntax for log4j2我真的很想让异步附加程序在某个时候工作,但很难通过 log4j2.properties 语法为 log4j2 找到示例

PS - here is my pom.xml: 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>

I just experienced what seems to be a similar problem.我刚刚经历了似乎是类似的问题。 In my case, I was able to receive application logs, but with what appeared to be a default log4j2 config.就我而言,我能够接收应用程序日志,但似乎是默认的 log4j2 配置。 The logs were INFO level.日志是 INFO 级别。 None of the levels set for loggers in the log4j2 config (XML) were being applied.没有应用 log4j2 配置 (XML) 中为记录器设置的任何级别。 I saw a very similar output in the startup logs as posted by the OP, including the message about "shutting down OutputStreamManager".我在 OP 发布的启动日志中看到了一个非常相似的输出,包括关于“关闭 OutputStreamManager”的消息。

Mine is a spring-boot application and I was able to resolve the problem by disabling the spring-boot LoggingSystem .我的是一个 spring-boot 应用程序,我能够通过禁用 spring-boot LoggingSystem来解决这个问题。 I added this system variable to my startup command: -Dorg.springframework.boot.logging.LoggingSystem=none我将此系统变量添加到我的启动命令中: -Dorg.springframework.boot.logging.LoggingSystem=none

Once I did that, the logging worked as expected , according to the levels set in my log4j2 XML config.一旦我这样做了,根据我的 log4j2 XML 配置中设置的级别,日志记录按预期工作 It appears to be spring-boot hijacking the log system, rather than a conflict between SLF4J an LOG4J2.它似乎是 spring-boot 劫持日志系统,而不是 SLF4J 和 LOG4J2 之间的冲突。

Note that I still see the same logging startup messages -- the OutputStreamManager is still shutting down.请注意,我仍然看到相同的日志记录启动消息——OutputStreamManager 仍在关闭。 Speculation: this is normal behavior, possibly just shutting down the bootstrap logger that prints the log4j2 initialization process.推测:这是正常行为,可能只是关闭了打印 log4j2 初始化过程的引导记录器。

Versions:版本:

  • log4j: 2.13.3 log4j:2.13.3
  • slf4j: 1.7.30 slf4j:1.7.30
  • spring-boot: 2.2.7-RElEASE弹簧靴:2.2.7-RELEEASE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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