Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

log4j static vs non-static loggers

807580Feb 27 2008 — edited Oct 20 2009
I've searched the web but have been unable to find a really good discussion on static vs. non-static loggers in log4j. If you know of one, point me to it. Otherwise, maybe we can start one up here.

With a non-static logger, your logger declaration looks like this:
private final Logger log = Logger.getLogger(getClass());
With a static logger, your logger declaration looks like this:
private static final Logger log = Logger.getLogger(MyClass.class);
What I would like to do is compile a list of pros and cons for each, and where possible, a way to test or demonstrate and possibly measure each pro/con.

Since a con for one is always a pro for the other, I will just list out cons (assume con for one is pro for the other). Here is what I can think of off the top of my head:

STATIC:

Con: Developer has to remember to change the class name when copying the log statement from class to class.
Con: Some sort of superclass issue? I have heard a few people say something about an issue with superclasses. Perhaps something to do with knowing what class log statements are coming from? But I have been unable to figure out how to demonstrate this issue (perhaps because I don't understand what they were saying). Anyone able to contribute on this one?

NON-STATIC:

Con: Can't log within static methods.
Con: More overhead when a large number of instances of the class exist? What's a good way to measure this to determine it's significance?
Con: Developer has to keep serialization in mind -- will probably want to declare the logger transient in some serializable classes.

Another unknown to me: Is there some potential value -- some kind of information that can be obtained through non-static loggers that could not be obtained through static loggers? Something instance-specific?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 17 2009
Added on Feb 27 2008
4 comments
7,760 views