簡體   English   中英

log4j2中異步記錄器的行為

[英]Behavior of Async logger in log4j2

我的log4j2.xml包含同步和異步記錄器。 但是,當我使用異步記錄器時,我只能得到第一個(5個之外)log.debug語句進行打印。

更新3/28-如果在log.debug調用之前引入Thread.sleep(1) ,那么我能夠通過Async logger --> Rewrite Appender --> Rolling File Appender獲取所有調試消息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");

}

但是,如果我在<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)。

看起來這是由於應用程序的shutdown事件關閉了尚未完成寫入的異步日志線程。 與此處描述的問題相同-[關機鈎子為時過早] https://issues.apache.org/jira/browse/LOG4J2-658

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM