I’m the only maintainer on a codebase where logging is done using Apache commons logging.
All classes contains these two imports:
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Then a lot of classes contains non static log instantiation like this:
/** The log. */
private Log log = LogFactory.getLog(Xyz.class);
Can this be justified?
Can I safely change all these to static calls?
EDIT Regarding the special cases where it can (apparently) be handy: my question is really more “Can non static log all over the codebase be justified?”
You have to be extra careful with having non-static loggers initialised the way your code snippet is doing in
Serializableclasses.Firstly because
Logisn’t serializable, so any attempt of serializing your class will fail too. If you declare your loggertransientthen, as is the logical thing to do, yourlogfield will not be initialised after deserialization, so you’ll get anNPEwhile trying to log stuff. Not a very nice situation.So to sum it up, you can have non-static loggers if you prefer, but make sure they are initialised before you use them. But apart from that, I wouldn’t worry much about non-static loggers, most logging implementations will always return the same logger object anyway (log4j definitely does).