Advantages for declaring loggers as static Disadvantages for declaring loggers as static
- common and well-established idiom
- less CPU overhead: loggers are retrieved and assigned only once, at hosting class initialization
- less memory overhead: logger declaration will consume one reference per class
- For libraries shared between applications, not possible to take advantage of repository selectors. It should be noted that if the SLF4J binding and the underlying API ships with each application (not shared between applications), then each application will still have its own logging environment.
- not IOC-friendly
Advantages for declaring loggers as instance variables Disadvantages for declaring loggers as instance variables
- Possible to take advantage of repository selectors even for libraries shared between applications. However, repository selectors only work if the underlying logging system is logback-classic. Repository selectors do not work for the SLF4J+log4j combination.
- Less common idiom than declaring loggers as static variables
- higher CPU overhead: loggers are retrieved and assigned for each instance of the hosting class
- higher memory overhead: logger declaration will consume one reference per instance of the hosting class
ExplanationStatic logger members cost a single variable reference for all instances of the class whereas an instance logger member will cost a variable reference for every instance of the class. For simple classes instantiated thousands of times there might be a noticeable difference.
However, more recent logging systems, e.g log4j or logback, support a distinct logger context for each application running in the application server. Thus, even if a single copy of log4j.jar or logback-classic.jar is deployed in the server, the logging system will be able to differentiate between applications and offer a distinct logging environment for each application.
More specifically, each time a logger is retrieved by invoking
LoggerFactory.getLogger()method, the underlying logging system will return an instance appropriate for the current application. Please note that within the same application retrieving a logger by a given name will always return the same logger. For a given name, a different logger will be returned only for different applications.
If the logger is static, then it will only be retrieved once when the hosting class is loaded into memory. If the hosting class is used in only in one application, there is not much to be concerned about. However, if the hosting class is shared between several applications, then all instances of the shared class will log into the context of the application which happened to first load the shared class into memory - hardly the behavior expected by the user.
Unfortunately, for non-native implementations of the SLF4J API, namely with slf4j-log4j12, log4j's repository selector will not be able to do its job properly because slf4j-log4j12, a non-native SLF4J binding, will store logger instances in a map, short-circuiting context-dependent logger retrieval. For native SLF4J implementations, such as logback-classic, repository selectors will work as expected.
In summary, declaring logger members as static variables requires less CPU time and have a slightly smaller memory footprint. On the other hand, declaring logger members as instance variables requires more CPU time and have a slighlty higher memory overhead. However, instance variables make it possible to create a distinct logger environment for each application, even for loggers declared in shared libraries. Perhaps more important than previously mentioned considerations, instance variables are IOC-friendly whereas static variables are not.
Check link for more info.
Thursday, September 13, 2012
Should Logger members of a class be declared as static?
Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...
Prerequisite: Basic knowledge of Spring boot application I am working on a series of implementing frameworks with Spring boot application b...
In my previous blog, I have already discussed on Lambda Expression and its Syntax as well as Functional Interface. So, if you are not awa...
The most common approach of generating a self-signed certificate is using the java keytool. There may be a situation when you want to cr...