Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can non static log be justified?

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?"

like image 696
Gugussee Avatar asked Jan 27 '11 14:01

Gugussee


People also ask

Is it necessary to make the logger static?

Thus, When you are creating a logger for a singleton, you don't need to make it static. Because there will be only one instance thus one logger. On the other hand, if you are creating a logger for a model or entity class, you should make it static not to create duplicated loggers.

What is a non static method in Java?

Non-static method: Any method whose definition doesn’t contain the static keyword is a non-static method. In the non-static method, the method can access static data members and static methods as well as non-static members and method of another class or same class, also can change the values of any static data member.

What is the difference between static and jcabi-log?

I.e. jcabi-log impedes the "high-performance" aspect of high-performance loggers. static means that you only create one Logger per class, not one logger per instance of your class. Generally, this is what you want - as the loggers tend to vary solely based on class. final means that you're not going to change the value of the logger variable.

Why make the logger final after initialization?

The final comes from the point that you normally don't change the Logger during the execution -- so once initialized you never "re-configured" it -- in which case it makes sense to make it final to ensure no one can change it (by mistake or otherwise).


2 Answers

It depends. This is from the documentation:

Note that for application code, declaring the log member as "static" is more efficient as one Log object is created per class, and is recommended. However this is not safe to do for a class which may be deployed via a "shared" classloader in a servlet or j2ee container or similar environment. If the class may end up invoked with different thread-context-classloader values set then the member must not be declared static. The use of "static" should therefore be avoided in code within any "library" type project.

like image 102
Michael Berry Avatar answered Oct 06 '22 19:10

Michael Berry


You have to be extra careful with having non-static loggers initialised the way your code snippet is doing in Serializable classes.

Firstly because Log isn't serializable, so any attempt of serializing your class will fail too. If you declare your logger transient then, as is the logical thing to do, your log field will not be initialised after deserialization, so you'll get an NPE while 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).

like image 35
biziclop Avatar answered Oct 06 '22 21:10

biziclop