Take the following scenario:
A class is instantiated several times with a different id for each instance. Logging for respective id should go to it's own file. One file for each id. The values for id is not known when writing the code (it of course is when writing the Main
class in this example).
public class Test implements Runnable {
private static final Logger LOG = LogManager.getLogger();
private final String id;
public Test(String id) {
this.id = id;
}
@Override
public void run() {
ThreadContext.put("id", id); //org.apache.logging.log4j.ThreadContext
while (true) {
try { Thread.sleep(10000); } catch (InterruptedException e) { break; }
LOG.info("{}: I have been sleeping for 10 seconds", id);
}
}
}
public class Main {
private static final Logger LOG = LogManager.getLogger();
public static void main(String[] args) {
LOG.info("Starting thread 1");
Thread t1 = new Thread(new Test("mode1"));
t1.start();
LOG.info("Starting thread 2");
Thread t2 = new Thread(new Test("mode2"));
t2.start();
LOG.info("Starting thread 3");
Thread t3 = new Thread(new Test("mode3"));
t3.start();
}
}
The following is the configuration for log4j2.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Appenders>
<Routing name="Routing">
<Routes pattern="$${ctx:id}">
<!-- If a special id should be treated differently
<Route key="mymode">
...
</Route> -->
<!-- Threads that don't have the id value set -->
<Route key="$${ctx:id}">
<File name="File"
fileName="normal.log">
<PatternLayout pattern="%d{HH:mm:ss} [%p] %c %msg%n" />
</File>
</Route>
<!-- Threads with the id value set, and is not one of the special ones above -->
<Route>
<File name="File-${ctx:id}"
fileName="id-${ctx:id}.log">
<PatternLayout pattern="%d{HH:mm:ss} [%p] %c %msg%n" />
</File>
</Route>
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Routing" />
</Root>
</Loggers>
</Configuration>
The idea for this is taken from the log4j2 FAQ. Also see Lookups.
If you still need to get your custom appender to work with log4j2, maybe this question with answer gives you some good hints on how to proceed.