Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we declare Loggers static final?

Tags:

java

logging

People also ask

Why do we declare logger static final?

Because that is usually the kind of functionnality that can be shared accross all instances of your objects. It does not make much sense (90% of the time) to have a different logger for two instances of the same class.

Should loggers be static?

Loggers should be declared to be static and final. It is good programming practice to share a single logger object between all of the instances of a particular class and to use the same logger for the duration of the program.

What is the purpose of creating a logger object as state?

A Logger object is used to log messages for a specific system or application component.

Is logger a constant?

The logger is not a constant as it is a reference to an object. Constants are values that can't be changed. The object reference is final (so the reference to it can't be changed, e.g. swapped with something else or set to null) but the object itself can.


  • private - so that no other class can hijack your logger
  • static - so there is only one logger instance per class, also avoiding attempts to serialize loggers
  • final - no need to change the logger over the lifetime of the class

Also, I prefer name log to be as simple as possible, yet descriptive.

EDIT: However there is an interesting exception to these rules:

protected final Logger log = LoggerFactory.getLogger(getClass());

as opposed to:

private static final Logger log = LoggerFactory.getLogger(Foo.class);

The former way allows you to use the same logger name (name of the actual class) in all classes throughout the inheritance hierarchy. So if Bar extends Foo, both will log to Bar logger. Some find it more intuitive.


Check this blog post: Get Rid of Java Static Loggers. This is how you use slf4j with jcabi-log:

import com.jcabi.log.Logger;
class Foo {
  void save(File f) {
    Logger.info(this, "file %s saved successfully", f);
  }
}

And never use that static noise any more.


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. Which is true, since you almost always throw all log messages (from one class) to the same logger. Even on the rare occasions where a class might want to send some messages to a different logger, it would be much clearer to create another logger variable (e.g. widgetDetailLogger) rather than by mutating the value of a static variable on the fly.


When would you want to change the value of the field?

If you're never going to change the value, making the field final makes it obvious that you'll never change the value.


Normally you initialize the logger to log using the class name -- which means that if they weren't static, you would end up with each instance of the class having an instance of it (high memory footprint), but all of these loggers would share the same configuration and behave exactly the same. That's the reason behind the static bit. Also because each Logger is initialised with the class name, to prevent conflicts with subclasses, you declare it private so it cannot be inherited. 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). Of course if you are going to use a Logger in a different way you might need NOT to use static final -- but I would venture to guess 80% of apps would use logging as explained above.