We have an application where a log message without the context in which it was run (i.e. the database) does nearly never make sense. The information is always available, but it is quite cumbersome to include it everywhere by hand. Is there a more elegant way of doing it?
I was thinking along the lines of implementing a wrapper around logging that always includes the information. I have tried it, but I’d like the LogFactory (either slf4j or Log4j) to pick this class by default, and I don’t know how to do that…
Any suggestions?
You could take a look at the
NDCorMDCclasses. You can push context information on the stack and include this automatically in your log messages.You can find a small example here.
In your
PatternLayout, use'%x'to logNDCinformation and'%X'forMDC.