log4j2中

时间:2017-03-28 02:20:06

标签: java xml asynchronous logging log4j2

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

更新3/28 - 如果我在log.debug调用之前引入Thread.sleep(1),那么我可以通过Async logger --> Rewrite Appender --> Rolling File Appender获取所有调试消息但不知道如何在没有睡眠语句的情况下实现这一点。

<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");

}

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

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

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

我假设super.callAppenders(event)必须适用于被调用的引用的appender?如何通过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);
}
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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