This is also possible with log4j. You can do this by implementing your own appender. I think the easiest way is to subclass AppenderSkeleton .
All logging events end in the append(LoggingEvent event) method, which you must implement.
In this method, you can access the MDC using event.getMDC("nameOfTheKeyToLookFor");
You can then use this information to open the file for writing. It may be useful to look at the implementation of standard applications such as RollingFileAppender to find out the rest.
I myself used this approach in an application to split logs of different threads into different log files, and it worked very well.
source share