Medelwr Medelwr - 11 months ago 91
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.