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());
BalusC Oct 01 '10 at 20:28 2010-10-01 20:28
source share