Can a non-static journal be justified?

I am the only codebase support where logging is done using entries in the Apocal commons.

All classes contain these two imports:

import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 

Then many classes contain non-stationary creation of a log instance as follows:

 /** The log. */ private Log log = LogFactory.getLog(Xyz.class); 

Can this be justified?

Is it safe to change all of these parameters to static calls?

EDIT Regarding special cases, when it can (apparently) be convenient: my question is really bigger: "Can a non-static log throughout the code base be justified?"

+8
java static logging
source share
4 answers

You have to be very careful that non-static registrars initialize the way that makes your code snippet in Serializable classes.

Firstly, because Log not serializable, so any attempt to serialize your class will not work either. If you declare your transient logger, then, as is logical, your Log field will not be initialized after deserialization, so you will get NPE when you try to write material. Not a good situation.

So, to summarize, you can have non-static loggers if you want, but make sure they are initialized before they are used. But other than that, I wouldn't worry about non-static loggers, most logging implementations will always return the same log object anyway (log4j definitely does).

+5
source share

It depends. This is from the documentation :

Note that for application code, declaring a member of a journal as β€œstatic” is more efficient because one journal object is created for each class and is recommended. However, this is not safe for a class that can be deployed through a "generic" class loader in a servlet or j2ee container or similar environment. If a class can be called with different thread-context-classloader values, then the member should not be declared static. Therefore, the use of "static" should be avoided in code in any project such as "library".

+8
source share

One example was a non-static logger, which is convenient - it's a kind of base class that provides a log instance for child classes (as a convenience). Consider the following example:

 public abstract class Pet { protected Log log; public Pet() { log = LogFactory.getLog(this.getClass()); } public void wash() { log.info("Get the hose."); } ... } public class Cat extends Pet { ... public void doSomethingUseful() { log.warn("I can't, I am a cat."); } } 

In this example, logging will come from an instance of the Cat log. Is this a reasonable justification for using a static recorder? Perhaps not for messages recorded in the Cat class, but messages recorded from the Pet class in the Cat journal instance may be useful.

+2
source share

Here is an article that implies the opposite. In fact, in complex situations, problems with loading classes can occur with static registrars in libraries. So yes, non-static recorders can be justified.

However, consider the case where a class using "private static Log log = ..." is deployed through ClassLoader, which is located in the pedigree of several supposedly independent "applications". In this case, the journal member is initialized only once because there is only one copy of the class. This initialization (usually) occurs on the first attempt at code to create an instance of this class or call a static method on it. when the class is initialized, what should the log member be set to?

http://wiki.apache.org/commons/Logging/StaticLog

0
source share

All Articles