Should the log be private static or not

Should the logger be declared static or not? Usually I saw two types of declarations for the registrar:

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

or

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

Which one should I use? what is pro and con of both?

+98
java logging
01 Oct '10 at 20:25
source share
4 answers

The advantage of a non-static form is that you can declare it in a (abstract) base class, as it should, without worrying about the right class name being used:

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

However, its drawback, obviously, is that for each instance of the class a whole new instance of the journal will be created. This, perhaps, is not expensive in itself, but it creates significant overhead. If you want to avoid this, you would like to use the static form. But its drawback is, in turn, that you must declare it in each individual class and take care of each class, that the correct class name is used during the construction of the log, because getClass() cannot be used in a static context. However, in an average IDE, you can create an autocomplete template for this. For example. logger + ctrl+space .

On the other hand, if you get a registrar using a factory, which, in turn, can cache already created log instances, then using a non-static form will not add too much overhead. For example, Log4j has a LogManager for this purpose.

 protected Log log = LogManager.getLogger(getClass()); 
+94
Oct 01 '10 at 20:28
source share

I used to think that all loggers should be static; however, this wiki.apache.org article raises some important memory issues regarding leaks from the class loader. Declaring the logger as static prevents garbage collection in the class (and its associated class loaders) in J2EE containers that use a common class loader. This will result in PermGen errors if you redeploy your application enough.

I really don't see a way around this classloader leak problem, other than declaring registrars as non-static.

+43
Mar 02 2018-11-11T00:
source share

The most important difference is how it affects your log files: in which category do the logs go?

  • In your first choice, the subclass logs fall into the superclass category. It seems very intuitive to me.
  • There is a variant of your first case:

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

    In this case, your log category indicates on which object the code that was logged is running.

  • In your second choice (private static), the log category is the class containing the logging code. So usually a class that does what is being registered.

I would highly recommend the latter option. It has these advantages over other solutions:

  • There is a direct link between the log and the code. It's easy to find back where the log message came from.
  • If someone needs to tune logging levels (which is done for each category), this is usually because they are interested (or not) in some specific posts written by a particular class. If the category is not a class that writes messages, it is more difficult to adjust the levels.
  • You can enter static methods
  • Journalists only need to initialize (or search) once per class, so when starting instead of each instance created.

It also has disadvantages:

  • It must be declared in each class where you register messages (reusing superclass logs).
  • When initializing the registrar, you need to put the correct class name. (But a good IDE will take care of this for you).
+15
Oct 02 '10 at 20:44
source share

Use control inversion and pass the logger to the constructor. If you create a logger inside a class, you will have a time devil with your unit tests. You write unit tests, right?

+3
Oct 01 2018-10-0120:
source share



All Articles