Log4J: Strategies for creating Logger instances

Typically, you’d have loggers setup per class because that’s a nice logical component. Threads are already part of the log messages (if your filter displays them) so slicing loggers that way is probably redundant.

Regarding application or layer based loggers, the problem is that you have to find a place to stick that Logger object. Not a really big deal. The bigger issue is that some classes may be used at multiple levels of from multiple applications… it could be difficult to get your logger right. Or at least tricky.

…and the last thing you want is bad assumptions in your logging setup.

If you care about applications and layers and have easy separation points, the NDC is the way to go. The code can be a little excessive sometimes but I don’t know how many times I’ve been saved by an accurate context stack showing me that Foo.bar() was called from application X in layer Y.

Leave a Comment