Java 带有滚动文件附加程序的 Log4j2 AsyncLogger 未显示文件行号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23820765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:24:16  来源:igfitidea点击:

Log4j2 AsyncLogger with rolling file appender not showing file line number

javalogginglog4jlog4j2

提问by Lynx777

Im using log4j2 with following dependencies:

我使用具有以下依赖项的 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 content:

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>

Turn out that everything went fine except the log doesn't show Line number of throwed logger (the %L in pattern).

结果一切都很好,除了日志没有显示抛出的记录器的行号(模式中的 %L)。

I googled and found out that for async logger and rolling file appender, there is no one mentioned using %L, so how can i achieved it then? Or is it doesn't support %L?

我搜索了一下,发现对于异步记录器和滚动文件附加器,没有提到使用 %L,那么我该如何实现呢?还是它不支持 %L?

Edit: I have tried added includeLocation="true", but still same results

编辑:我尝试添加includeLocation="true",但结果仍然相同

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

Thanks

谢谢

采纳答案by Remko Popma

First, remove the Async appender, and point the appender-ref of the AsyncLogger to the ProcessorLoggingFile directly. Second, you must add includeLocation="true" on the AsyncLogger.

首先,移除Async appender,将AsyncLogger的appender-ref直接指向ProcessorLoggingFile。其次,您必须在 AsyncLogger 上添加 includeLocation="true"。

Having an async appender in addition to an async logger does not help and in this case might be what prevents the includeLocation from working correctly.

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