简体   繁体   English

log4j2:在运行时重新配置记录器的问题

[英]log4j2 : Problem with reconfiguration of the loggers during runtime

This is related to log4j2 version 2.13.2 .这与log4j2 版本 2.13.2相关。

We are consuming an API which comes with it's own logger configuration file say - log4j2_api.xml .我们正在使用一个 API ,它带有它自己的记录器配置文件,比如log4j2_api.xml As an application developer, we have our own log4j2.xml in the classpath.作为应用程序开发人员,我们在类路径中有自己的log4j2.xml

The API we are consuming is internally loading log4j2 entities (loggers and appenders) from log4j2_api.xml .我们正在使用的 API 在内部从log4j2_api.xml加载 log4j2 实体(记录器和附加程序)。 It looks like when that happens, our own configuration from log4j2.xml gets overwritten by log4j2_api.xml and our application produces no further logs.看起来当这种情况发生时,我们自己的log4j2.xml配置被log4j2_api.xml覆盖,我们的应用程序不再生成日志。

Here is a brief sequence of events -这是一个简短的事件序列-

  1. Application starts and log4j2.xml gets loaded from classpath.应用程序启动并从类路径加载log4j2.xml
  2. Application proceeds with appropriate logging as expected.应用程序按预期进行适当的日志记录。
  3. First API call happens.第一个 API 调用发生。 Internally log4j2_api.xml gets loaded and API logging happens as per it's configuration.内部log4j2_api.xml被加载,API 日志记录根据其配置发生。
  4. Application receives the needed data from the API and proceeds further, but without desired logging.应用程序从 API 接收所需的数据并继续进行,但没有所需的日志记录。

My exepectation here is - if pre-loaded ( by initial configuration) loggers/appenders are found in the new configuration, they should be updated and other ones should remain untouched.我的期望是- 如果在新配置中找到预加载(通过初始配置)记录器/附加器,它们应该被更新并且其他的应该保持不变。 And ofcourse, the newly configured loggers/appenders should get added.当然,应该添加新配置的记录器/附加器。

Is this understanding correct?这种理解正确吗?

Thanks in advance.提前致谢。

Configuration Files -配置文件 -

log4j2.xml log4j2.xml

<Configuration status="warn">
    <Appenders>
        <RollingFile name="applicationAppender" fileName="sample-log-executor-2008.log" filePattern="sample-log-executor-2008-%i.log">      
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            
            <Policies>
                <SizeBasedTriggeringPolicy size="5 MB" />
            </Policies>
        </RollingFile>
        
        <Console name="console_window" target="SYSTEM_OUT">
            <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
        </Console>
    </Appenders>
    
    <Loggers>
        <Logger name="com.abc.samplecode" level="trace" additivity="false">
            <appender-ref ref="applicationAppender" level="trace" />
        </Logger>

        <Root level="error" additivity="false">
            <appender-ref ref="console_window" />
        </Root>
    </Loggers>
</Configuration>

log4j_api.xml log4j_api.xml

<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d %-5p [%t] %C (%F:%L) - %m%n" />
        </Console>
        
        <RollingFile name="xyzAPIAppender" fileName="xyzAPILog.log"
            filePattern="xyzAPILog-%i.log">
            <PatternLayout
                pattern="%d{MMM dd yyyy HH:mm:ss.SSS} %-5p [%t] (%27F:%-5L) - %m%n" />
            <Policies>
                <SizeBasedTriggeringPolicy size="5 MB" />
            </Policies>
            <DefaultRolloverStrategy max="10" />
        </RollingFile>
    </Appenders>
    
    <Loggers>
        <logger name="com.xyz.api" level="warn"
            additivity="false">
            <AppenderRef ref="xyzAPIAppender" />
        </logger>
    </Loggers>
</Configuration>

I suspect you are missing log4j2 core jar in your class path.我怀疑您在 class 路径中缺少 log4j2 核心 jar 。

Please see the arch for more details http://logging.apache.org/log4j/2.x/faq.html#which_jars有关更多详细信息,请参阅拱门http://logging.apache.org/log4j/2.x/faq.html#which_jars

From what you are describing the library you are consuming is calling Log4j to configure with its own configuration.根据您所描述的库,您正在使用调用 Log4j 来配置其自己的配置。 This is very rude as it causes the problem you are running into.这是非常粗鲁的,因为它会导致您遇到的问题。 When it loads its configuration it is reconfiguring Log4j so your configuration is being removed.当它加载其配置时,它正在重新配置 Log4j 因此您的配置将被删除。

You need to get more information on the library you are calling and find out how to get it to stop reinitializing Log4j.您需要获取有关您正在调用的库的更多信息,并了解如何让它停止重新初始化 Log4j。

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

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