public class PerThreadLog4jAppender
extends org.apache.log4j.AppenderSkeleton
Usage:
final String threadName = ...;
Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %p: %m%n");
FileAppender toFile = new FileAppender(layout, "C:/" + threadName);
Runnable runnable = new Runnable() {
public void run() {
NDC.push(threadName);
try {
Logger.getLogger(getClass()).info("Some text!");
}
finally { NDC.remove(); }
}
};
Thread thread = new Thread(runnable, threadName);
PerThreadLog4jAppender
.getSharedInstanceAndAddToLog4jIfNecessary()
.addThreadAppender(thread, toFile);
There is no need to remove appenders from this object once threads exit.| Modifier and Type | Method and Description |
|---|---|
void |
addThreadAppender(Thread t,
org.apache.log4j.AppenderSkeleton a) |
void |
close() |
static PerThreadLog4jAppender |
getSharedInstanceAndAddToLog4jIfNecessary() |
void |
registerSubThread(Thread mainThread,
Thread subThread)
Makes logs from subThread go to the same appender as the calling thread
|
boolean |
requiresLayout() |
public static PerThreadLog4jAppender getSharedInstanceAndAddToLog4jIfNecessary()
public void addThreadAppender(Thread t, org.apache.log4j.AppenderSkeleton a)
public void registerSubThread(Thread mainThread, Thread subThread)
public void close()
public boolean requiresLayout()