3

I have an application that uses several different Java classes and would like to be able to supply debugging context to logging messages.

This (application and data-specific) context (basically, a string) will be constructed or retrieved somehow by each class individually and will be passed to the appropriate logging call, e.g.

logger.debug(context + ", whatever error message");

So, my question is, what would be the best pattern to use in order to implement this functionality across the application?

I am thinking of having all classes that need to support context-sensitive logging implement a Java interface with a few appropriate methods, e.g.

public interface ContextSensitive
{
    public String getContext();
    public String setContext();
}

where the setContext() method will be useful for cases where an object (say) secondObject is instantiated from an object (say) firstObject that has context, but secondObject has not. So, firstObject would also do something like

secondObject = new SecondObject();
secondObject.setContext(context);

Does the above make sense, or should I do something better?

An alternative I have been considering is to write a wrapper around the logging library to offer this functionality, but I don't see any value to this approach, compared to the one I described above, based on the ContextSensitive interface.

If there are any examples of open source projects that had to tackle the same issue, I would appreciate a link.

Many thanks!

PNS
  • 19,295
  • 32
  • 96
  • 143
  • Thanks, but not all answers really work. :-) – PNS Sep 21 '11 at 09:12
  • If you found your own answer, then post it and accept it to provide feed back to the community or at least accept the answer that at least partially answer the question. If there is really no good answer, then probably you asked the wrong question :) – Andrey Adamovich Sep 21 '11 at 09:21
  • Ah, yes, I can post my own answer! :-) – PNS Sep 21 '11 at 09:26

3 Answers3

3

I suspect you are looking for something similar to MDC (mapped diagnostic context) which is a feature of log4j, logback and slf4j frameworks. The context can be set only once before the method call and then logging framework can be configured to produce logging messages including fields from the context.

Andrey Adamovich
  • 20,285
  • 14
  • 94
  • 132
  • I have played with MDC, but the problem is that it works per thread and I need to change the context within the same thread, numerous times. – PNS Sep 21 '11 at 09:21
  • Use AOP (or Servlet Filter if you have a web app) to set your context. That's pretty standard aproach to this problem and it will kepp your code cleaner as you will not have to care about passing the context to all your methods all the time. – Andrey Adamovich Sep 21 '11 at 09:22
  • What is your archicture/frameworks/application server? Answer will depend on that. – Andrey Adamovich Sep 21 '11 at 09:29
  • Just plain vanilla Java. For debugging I use Log4j/Log5j. – PNS Sep 21 '11 at 09:30
  • Actually I had a closer look at AOP and it is not what I would like to do at this point. I am interested in a "plain" Java solution (as opposed to using things like AspectJ). – PNS Sep 21 '11 at 12:21
  • "plain" Java sounds like you don't use any of dependecy injection frameworks, right? – Andrey Adamovich Sep 21 '11 at 13:08
  • Exactly. No Spring or anything else. – PNS Sep 21 '11 at 17:13
1

This is a perfect example of a cross-cutting concern, so use AOP.

DaveFar
  • 7,078
  • 4
  • 50
  • 90
  • Actually I had a closer look at AOP and it is not what I would like to do at this point. I am interested in a "plain" Java solution (as opposed to using things like AspectJ). – PNS Sep 21 '11 at 12:22
  • If you would give a concrete example of the information you want to log, maybe someone will suggest a library. I did so for performance analysis below, but that doesn't seem to be your situation.... – DaveFar Sep 21 '11 at 12:25
  • It is just a few String values that need to be passed across objects, but may change numerous times within the same thread (data-specific). Sort of session management. – PNS Sep 21 '11 at 12:50
1

What is the information used for that you want to log?

If it's for performance analysis, take a look at What is the best macro-benchmarking tool / framework to measure a single-threaded complex algorithm in Java?, there are some tools listed that use logging mechanisms.

Community
  • 1
  • 1
DaveFar
  • 7,078
  • 4
  • 50
  • 90