8

Im using log4j2 with following dependencies:

<!-- 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:

<?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).

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?

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

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

Thanks

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Lynx777
  • 332
  • 1
  • 6
  • 17

1 Answers1

15

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.

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.

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • Thank You!!! It works.. But uhm, how does having async appender is a no no in addition to asyn logger? – Lynx777 May 23 '14 at 11:29
  • 3
    If your app logs to an async logger it will essentially just put log events onto a queue (the LMAX disruptor). A separate thread will take the events out of the queue and send them to the appender. If the appender is async, the event is again put onto a queue. Finally, the async appender's thread will take the event out of that queue and write to the file. So one of them is unnecessary. AsyncLoggers are much faster so I'd recommend to remove the async appender. Also, if you keep both you need includeLocation for both (not sure if that would work, not tested...) – Remko Popma May 23 '14 at 15:25