Question

I am using JBOSS7. I store some information about the SOAP request in a ThreadLocal variable in order to adding it to the log4j header.

My ThreadLocal class:

class MyStorage
private static final ThreadLocal<String> storage = new ThreadLocal<String>();

public static void setRequestId(String requestId) {
    storage.set(requestId);
}

public static String getRequestId() {
    return storage.get();
}

...
}

My log4j appender class:

public class MyRollingFileAppender extends RollingFileAppender {

@Override
public void append(LoggingEvent event) {
    String reqId = MyStorage.getRequestId();
    event.setProperty("reqId", reqId == null ? "UNKNOWN" : reqId);
    super.append(event);
}
}

log4j.properties:

log4j.appender.Throttling.layout.ConversionPattern=[%d{HH:mm:ss,SSS}] (%properties{reqId}) (%t) [%-5p] [%c]: %m%n

When I start JBOSS first and deploy the EJB everything is working fine. I can see the correct requestId in the log header. However when I redeploy the EJB then I see in the log (I added System.out.println() to the methods of MyStorage) that the correct value of the requestId was passed to the method MyStorage.setRequestId(...) but the value of the requestId in the log header is UNKNOWN. Moreover the result of MyStorage.getRequestId() is null too.

If I restart the JBOSS then everything is working fine again until I redeploy the EJB. I am not quite sure why this is happening. Or is there a better way of passing information to the log4j RollingFileAppender than the ThreadLocal?

Thanks, V.

Was it helpful?

Solution

The solution was to get rid of log4j and use logback. I tried to use MDC too in log4j but I had the same result. In logback the MDC works perfectly. Probably the MyRollingFileAppender is being executed in a different thread in log4j after redeploying...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top