asynchronous programming and Logback MDC

190 views
Skip to first unread message

Yann Simon

unread,
Jul 23, 2013, 11:40:33 AM7/23/13
to play-fr...@googlegroups.com
Hi all,

in a asynchronous programming, different threads are used to compute different part of the program
For example: a thread A is used to initiate a GET request, and a thread B is used to handle the response.

With this programming model, the Mapped Diagnostic Context (MDC) from Logback (http://logback.qos.ch/manual/mdc.html) cannot work, as it uses a thread local variable to hold the data.

Example:
if a thread A put a data into the MDC, the thread B cannot read it.

What could be a solution for this?
1. not using the Logback MDC, putting data in request.tags for example.
It means that the Play Logger cannot be used anymore.
Instead, we have to use a custom Logger that implements each method (info, debug,...) and add information read from the request tags for example.
-> the changes are very deep
-> the Logback MDC variables (like %X{myVariable}) cannot be used in the Logback configuration. It is quite sad, as they provide a good flexibility.

2. passing the Logback MDC from thread A to thread B.
For this, the default ExecutionContext could be extended to read the MDC map (http://www.slf4j.org/api/org/slf4j/MDC.html#getCopyOfContextMap()) and put it into the running thread (http://www.slf4j.org/api/org/slf4j/MDC.html#setContextMap(java.util.Map)).
-> change in playframework

Does somebody have another solution?
What do you think of it?

Cheers,
Yann


Reply all
Reply to author
Forward
0 new messages