Why do we declare Loggers static final?

后端 未结 14 1906
陌清茗
陌清茗 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:46

    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.

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

    In most cases, you are not going to change the reference and final modifier marks it. You don't need separate instances for each class instance - so static. And first of all this is for performance - it can be nicely optimized (final) and saves memory (static).

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

    Ideally Logger should be as follows upto Java 7, for giving no Sonar and giving a Compliant Code: private: never be accessible outside of its parent class. If another class needs to log something, it should instantiate its own logger. static: not be dependent on an instance of a class (an object). When logging something, contextual information can of course be provided in the messages but the logger should be created at class level to prevent creating a logger along with each object and hence preventing High Memory footprint. final: be created once and only once per class.

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

    You still need static logger for inner static classes

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

    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.

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

    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.

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