I have a Struts application with log4j to display information about application.
The pattern to format log\'s output is as follows:
log4j.appender.RA
Extend PatternLayout
as below, and then specify MyPatternLayout
with $X{threadId}
in the format string.
This implementation uses ThreadLocal
to minimize the performance impact of calculating the Thread ID:
MyPatternLayout extends PatternLayout {
private final ThreadLocal threadId = new ThreadLocal() {
@Override
protected String initialValue() {
String t = Long.toString(Thread.currentThread().getId());
MDC.put("threadId", t);
return t;
}
};
@Override
public String format(LoggingEvent event) {
this.threadId.get();
return super.format(event);
}
}