Medelwr Medelwr - 1 month ago 20
Java Question

Setting ThreadContext for all threads in application

From this answer http://stackoverflow.com/a/25125159/4367326 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

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.