Log4j2 AsyncLogger,滚动文件appender没有显示文件行号

时间:2014-05-23 04:02:03

标签: java logging log4j log4j2

我正在使用log4j2以及以下依赖项:

<!-- LOG4J2 -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0-rc1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0-rc1</version>
    </dependency>
    <dependency>
        <groupId>com.lmax</groupId>
        <artifactId>disruptor</artifactId>
        <version>3.0.1</version>
    </dependency>

Log4j2.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF">

<Appenders>
    <Console name="CONSOLE" target="SYSTEM_OUT">
        <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
    </Console>

    <!-- Generate rolling log for router with per hour interval policy -->
    <RollingFile name="ProcessorRollingFile" fileName="D:/ocsprocessor.log"
        filePattern="D:/ocsprocessor.log.%d{MM-dd-yyyy}-%i.log">
         <PatternLayout>
      <pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
  </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" />
        </Policies>
        <DefaultRolloverStrategy max="24" />
    </RollingFile>



    <!-- Register Async appender -->
    <Async name="AsyncRollingFile">
        <AppenderRef ref="ProcessorRollingFile" />
    </Async>
</Appenders>

<Loggers>
    <AsyncLogger name="com.tritronik.logger.log4j2" level="error"
        additivity="false">
        <AppenderRef ref="AsyncRollingFile" />
    </AsyncLogger>


</Loggers>
</Configuration>

事实证明一切都很顺利,除了日志没有显示投掷记录器的行号(模式中的%L)。

我用谷歌搜索,发现对于异步记录器和滚动文件追加器,没有人提到使用%L,那么我怎么能实现呢?或者它不支持%L?

修改:我尝试添加 includeLocation =&#34; true&#34; ,但结果仍然相同

2014-05-23 11:42:40,368 [threadPoolTaskExecutor-5] ERROR (AsyncLogger:) - THIS IS TEST MESSAGE FOR LOGGING TEST PURPOSE

谢谢

1 个答案:

答案 0 :(得分:13)

首先,删除Async appender,并将AsyncLogger的appender-ref直接指向ProcessorLoggingFile。其次,你必须添加includeLocation =&#34; true&#34;在AsyncLogger上。

除了异步记录器之外还有一个异步appender没有帮助,在这种情况下可能会阻止includeLocation正常工作。