log4j2 中异步记录器的行为



My log4j2.xml包含同步和异步记录器。但是,当我使用异步记录器时,我只能打印第一个(共 5 条(log.debug 语句。

更新 3/28 --如果我在 log.debug 调用之前引入一个Thread.sleep(1)。 然后我能够通过Async logger --> Rewrite Appender --> Rolling File Appender获取所有调试消息 但是不确定如何在没有该 sleep 语句的情况下实现这一目标。.

<Configuration status="WARN" packages="com.loggy.test">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <!--  Custom Appender Approach below -->
    <Stub name="myapp" fileName="/Users/loggy/logs/myapp.log"
                 filePattern="logs/myapp-%d{MM-dd-yyyy}.log.gz">
         <RegexFilter regex=".* special_log .*" onMatch="ACCEPT" onMismatch="DENY"/>
        <PatternLayout>
                    <pattern>%d %p %c{1.} [%t] %m%n</pattern>
          </PatternLayout>
          <TimeBasedTriggeringPolicy />
    </Stub>
    <!--  Custom Appender Approach above ends here -->
    <RollingFile name="RollingFile" fileName="/Users/loggy/logs/roll_file_app.log"
                 filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
           <PatternLayout>
                    <pattern>%d %p %c{1.} [%t] %m%n</pattern>
          </PatternLayout>
          <TimeBasedTriggeringPolicy />
    </RollingFile>

    <Rewrite name="Rewrite" ignoreExceptions = "false">
        <CookieAppenderPolicy cookieNeeded="true">
        </CookieAppenderPolicy>
        <AppenderRef ref="RollingFile"/>
    </Rewrite>
  </Appenders>
    <Loggers>
        <AsyncLogger name="com.loggy.test" level="debug" includeLocation="true">
            <AppenderRef ref="Rewrite" />
        </AsyncLogger>  
    <Root level="trace">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>


package com.loggy.test;
public class TestJ {
    @Inject
    public static void main(String[] args)
    {
        final Logger log4j = LogManager.getLogger(TestJ.class
                .getName());

        log4j.trace(" special_log Trace");
/* When Async logger is used, Only this line below gets printed to the Rewrite Appender and hence to the Rolling File appender */
        log4j.debug(" special_log debug published!");
    log4j.info(" special_log test info");

    log4j.debug(" sd_log test info");
   /* Following lines NEVER gets published to the rolling file appender or to the Rewrite Appender */
    log4j.debug(" special_log debug 2");
    log4j.debug(" special_log debug 3");
    log4j.debug(" special_log debug 4");
    log4j.debug(" special_log debug 5");
}

但是,如果我在<AsyncLogger>之外包含<AppenderRef ref="Rewrite" />,那么我会发布所有行。

关于应该使用 log4j2 更改什么的任何想法.xml以便在使用 AsyncLogger 时获得所有适当的行?

我还注意到,在 log4j2 包AsyncLoggerConfig.java类中,代码永远不会到达super.callAppenders(event);

我假设super.callAppenders(event)必须为调用引用的追加器工作?如何通过 log4j2.xml 执行此行?

/**  AsyncLoggerConfig.java
 * Passes on the event to a separate thread that will call
 * {@link #asyncCallAppenders(LogEvent)}.
 */
@Override
protected void callAppenders(final LogEvent event) {
    // populate lazily initialized fields
    event.getSource();
event.getThreadName();
// pass on the event to a separate thread
if (!helper.callAppendersFromAnotherThread(event)) {
    super.callAppenders(event);
}
}

此问题已在更高版本的 Log4j 中得到解决。解决方案是从版本 2.0-rc1 升级到最新版本(撰写本文时为 2.8.1(。

看起来这是由于应用程序的关闭事件关闭了尚未完成写入的异步日志线程。与此处描述的相同问题 - [关闭钩子调用过早] https://issues.apache.org/jira/browse/LOG4J2-658。

最新更新