Looking for a "logical multi layer logging" pattern


General J2EE: Looking for a "logical multi layer logging" pattern

  1. Hi all, I have a issue to solve regarding the logging process in a logical multi layer application. I have a multilayer software, running on the same JVM: note that this is not a physical multilayer logging problem. Lets say my layers are : View (HttpRequest...), Business layer, Delegates layer, web service client layer. The problem is that the logger from the View knows well the "UserId" using the system, but from the "stateless" Business to the "stateless" Web Service Layers it lost the "UserId" data because it doesn't need it for its process. I would like the logger at the Web Service Layer to log a nice debug trace with the "UserId"! A need that because my system admin need a complete log to trace all the process efficently in a multi thread environment. So here are the layers and the method they have : 1- View (from an HttpRequest), [doJob(int UserId);] 2- Business, [doJob()] 3- Delegates, [doJob()] 4- web service client [doJob()] I need a logger able to do this kind of tracing: logger.debug("My userId is..." + " doing this web service job"); I have checked many possibilities, I want to know what would be the best pattern for this issu, is there a better way to do that...? 1- Pass the UserId in the parameter. -- Comment: This is obviously bad. 2- Put the a kind of ThreadId (or other Id) as a key in a static Map and store some UserData. -- Comment: I dont like that at first glance. The logging might be done at lower level but now use or is dependant of data that are not passed in the method signature. This smell bad. 3- Use some sort of listener and Event handling to get the logs events from the lower layers and log it from the upper layer. Use ThreadId as a key of some sort. The listener can be a single process listening for all logs. -- Comment: I prefer this because the logging is done at the upper level, but trigered from the lower level. 4- Simply sort the log with the TreadId if available? Sort the log while writing, or sort the log while reading. It must be a very common problem. Am I missing a trivial solution? Or is there some common library or pattern for this issu? Thanks for answering Etienne Paris

    Threaded Messages (2)

  2. Diagnostic Context[ Go to top ]

    Etienne, I think you are looking for something that I have seen called a "Diagnostic Context".   In the log4j logging system they have a "Nested Diagnostic Context" and a "Mapped Diagnostic Context".   You might want to read up on the NDC & MDC classes in the log4j documentation and see if that at all resemblse the abstraction you are looking for. Mike
  3. Thanks[ Go to top ]

    Thanks Mike, that was what I was looking for. It is using the ThreadLocal to pass value on the same Thread. Can be tricky, but works well in most environment. Etienne