Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should logger be private static or not

Tags:

java

logging

Should logger be declared static or not? Usually I've seen two types of declaration for a logger :

     protected Log log = new Log4JLogger(aClass.class); 

or

     private static Log log = new Log4JLogger(aClass.class); 

Which one should be used? what are the pro's and con's of both?

like image 580
Drahakar Avatar asked Oct 01 '10 20:10

Drahakar


People also ask

Why do we declare logger static final?

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.

Should logger be capitalized?

So to be correct, if you are using log4j or JUL, it should be "logger", and if you are using slf4j, it should be LOGGER.

What is the purpose of creating a logger?

A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.

How does a logger work internally?

When an application generates the logging call, the Logger records the event in the LogRecord. After that, it sends to the corresponding handlers or appenders. Before sending it to the console or file, the appenders format that log record by using the formatter or layouts.


2 Answers

The advantage of the non-static form is that you can declare it in an (abstract) base class like follows without worrying that the right classname will be used:

protected Log log = new Log4JLogger(getClass()); 

However its disadvantage is obviously that a whole new logger instance will be created for every instance of the class. This may not per se be expensive, but it adds a significant overhead. If you'd like to avoid this, you'd like to use the static form instead. But its disadvantage is in turn that you have to declare it in every individual class and take care in every class that the right classname is been used during logger's construction because getClass() cannot be used in static context. However, in the average IDE you can create an autocomplete template for this. E.g. logger + ctrl+space.

On the other hand, if you obtain the logger by a factory which in turn may cache the already-instantiated loggers, then using the non-static form won't add that much overhead. Log4j for example has a LogManager for this purpose.

protected Log log = LogManager.getLogger(getClass()); 
like image 164
BalusC Avatar answered Sep 18 '22 20:09

BalusC


I used to think that all loggers should be static; however, this article at wiki.apache.org brings up some important memory concerns, regarding classloader leaks. Declaring a logger as static prevents the declaring class (and associated classloaders) from being garbage collected in J2EE containers that use a shared classloader. This will result in PermGen errors if you redeploy your application enough times.

I don't really see any way to work around this classloader leak issue, other than declaring loggers as non-static.

like image 21
piepera Avatar answered Sep 16 '22 20:09

piepera