Medelwr Medelwr - 1 year ago 128
Java Question

Setting ThreadContext for all threads in application

From this answer I have routingAppender working but I want to set the ThreadContext for every thread in the program.

When I set

ThreadContext.put("logFileName", "TestLogFile");

It works for the main thread and logs as expected but not for any other threads in my application. How can I achieve this?

Answer Source

Every child thread will inherit fathers ThreadContext state if you set up system property isThreadContextMapInheritable to true. But this will not work for Executors so you need to manually copy data from one thread to another.

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