user2473346
user2473346

Reputation: 11

Can log4j MDC be used for non-threaded application?

I have a scenario wherein I invoke a third party REST URL from inside a FOR loop for different values of the field JOBNAME. I wish to log the logging statements for each JOBNAME inside a separate log file i.e. the request sent to the REST URL, all the intermediate business logic logging and the response received would need to be part of different log file for each JOBNAME.

The catch here is that the processing involves no threads. Is the MDC approach for log4j feasible for non-threaded scenario's as well. If not, how can I achieve this?

Upvotes: 1

Views: 295

Answers (1)

Dima
Dima

Reputation: 4128

Having a single thread you can still put a variable into MDC context and let the calls propagate and log whatever they do. Just make sure that you do MDC.put(..) before interesting staff gets called and remove it when you are done so it doesn't linger. For example, you can do MDC.put(..) within the FOR loop and each iteration will be with different context even though it's the same thread. Not sure about elsewhere but in Java it works fine.

Upvotes: 2

Related Questions