IM42 IM42 - 1 year ago 64
Java Question

Request ID not logged on moving to log4j 1.2's async-appender

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.

  1. Is that because async-appender is a separate thread and did not inherit the same key-value pairs as the main application threads?

  2. Is there a way by which I can set these values in the MDC so that the application starts logging request ID again?

log4j.xml file for reference:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="" debug="false">
<appender name="Async" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="FILE"/>

<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" />

<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%-5p] %c %x - %m%n" />

<priority value="warn"/>
<appender-ref ref="Async"/>

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download