I have a web application which has been logging synchronously using log4j1.2 so far. I am trying to move it to use the async-appender (from log4j 1.2). I have written an XML file - log4j.xml that initializes async-appender. However when I check the logs I see that the ID of the request is not logged (though it used to be logged so far). After some investigation I think that the (key, value) pair for request ID is not present in the MDC anymore.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="Async" class="org.apache.log4j.AsyncAppender">
<appender name="FILE" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/log/directory/logFile"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="Append" value="true" />
<param name="ConversionPattern" value="%d [%-5p] %c %x - %m%n" />
With Async-appender, you need to read the mdcCopy off the LoggingEvent passed into the Async Appender thread. I had a custom PatternLayout which used to try to read request ID and such values from the MDC of the current thread - in essence, it would work only with synchronous logging because the main thread spawning the application threads was setting the values into the respective MDCs.
The Async-appender thread's MDC did not have the (key, value) pairs set, but the LoggingEvents passed to it did have the (key, value) pairs in its mdcCopy.