Why do we declare Loggers static final?

后端 未结 14 1904
陌清茗
陌清茗 2020-11-28 01:19

In Java, why is it best practice to declare a logger static final?

private static final Logger S_LOGGER
相关标签:
14条回答
  • 2020-11-28 01:37
    • 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.

    0 讨论(0)
  • 2020-11-28 01:40

    In addition to the reasons given in the other answers one thing I ran into was that if my logger was neither static nor final:

    ...
    public Logger logger = LoggerFactory.getLogger(DataSummary.class);
    
    public String toJson() {
      GsonBuilder gsonBuilder = new GsonBuilder();   
      return gsonBuilder.create().toJsonTree(this).toString();
    }
    ...
    

    in certain cases (when I was using the Gson library) I would get stackoverflow exception. My specific situation was to instantiate the class containing the non static non final logger. Then call the toJson method which invoked GsonBuilder:

    ...
    DataSummary ds = new DataSummary(data);    
    System.out.println(ds.toJson());
    ...
    
    0 讨论(0)
  • 2020-11-28 01:41

    To answer that question, you should have asked yourself what "static" and "final" are for.

    For a Logger, (I assume you talk about Log4J Logger class) you want a category per class. Which should lead to the fact that you assign it only once, and there is no need for more than one instance per class. And presumably there is no reason to expose the Logger object of one class to another, so why dont make it private and follow some OO-Principles.

    Also you should note, that the compiler is able to take benefits of that. So your code performs a bit better :)

    0 讨论(0)
  • 2020-11-28 01:41

    Actually static loggers can be "harmful" as they are supposed to work in a static context. When having a dynamic environment eg. OSGi it might help to use non-static loggers. As some logging implementations do a caching of loggers internally (AFAIK at least log4j) the performance impact might negligible.

    One drawback of static loggers is eg. garbage collection (when a class is used only once eg. during initialization the logger will be still kept around).

    For more details check:

    • http://slf4j.org/faq.html#declared_static
    • https://cwiki.apache.org/confluence/display/COMMONS/Logging+StaticLog

    See also:

    • Should logger be private static or not
    0 讨论(0)
  • 2020-11-28 01:42

    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.

    0 讨论(0)
  • 2020-11-28 01:43

    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.

    However, you can also see sometimes logger classes declared as singletons or even simply offering static functions to log your stuff.

    0 讨论(0)
提交回复
热议问题