1. Common Logger definition methods
Here are three common ways to define Loggers:
1.1 Use private static final and specify the class name
private static final Logger LOGGER = ();
- Use class names as the context of the logger, which is usually recommended.
- Defined as static means that this is a class-level logger and does not change with instantiation.
- final ensures that the reference is not reassigned.
advantage:
Best performance. The logger is only initialized once when the class is loaded, reducing runtime overhead.
Clear and intuitive, the name of the logger is bound to the current class.
It is the best practice for most Java development teams and complies with common coding specifications.
shortcoming:
It can only be used in the current class. If the subclass requires logging functionality, it needs to redefine its own logger.
1.2 Use private final and specify the class name
private final Logger LOGGER = ();
- Non-static variables, meaning that the logger is initialized every time a class instance is created.
- Defined as final, but the life cycle is bound to the class instance.
advantage:
If the log needs to be bound to an instance of the class, not the class itself (very rare), consider this way.
Suitable for a very small number of specific needs, such as dynamic proxy or dependency injection.
shortcoming:
The logger is recreated every time the instantiation is very expensive and is usually not recommended.
It does not conform to the convention of logger binding to class, and most teams will not adopt it.
1.3 Dynamically obtain class names using ()
private static final Logger LOGGER = (());
- Dynamically get the name of the current class as the context of the logger.
- It is usually defined in the base class so that the subclass can reuse the same code logic to obtain its own class name.
advantage:
When the base class defines a logger, subclasses do not need to be repeatedly declared, reducing code redundancy.
Dynamically adapts to the name of the subclass to facilitate unified management of log output.
shortcoming:()
It is runtime operation, with slightly poor performance.
If the subclass is strongly coupled with the base class log content, it may cause debugging difficulties.
2. Other considerations in the Logger definition
2.1 Logger name: uppercase or lowercase?
- It is recommended to use full capital (such as
LOGGER
),conform tostatic final
Naming specifications for constants. - Lower case (such as
logger
) Although it can be used, it can cause inconsistent code styles in teamwork.
2.2 Logger access modifier: private or protected?
1、private:
Loggers are internal implementation details of the class and usually do not need to be exposed to subclasses. Private is recommended.
Each class has its own logger to facilitate distinction between log sources.
2、 protected: In scenarios where a shared logger is required (such as the base class and subclass are highly correlated), you can consider using protected.
Be careful when using it to avoid subclasses abuse the parent logger and causing confusion in log content.
3. Recommended best practices
- From the perspective of performance, readability and maintenance, the following writing method is recommended:
private static final Logger LOGGER = ();
- Comply with the habit of binding loggers to classes.
- Best performance, the logger is initialized only once.
- Clarify the source of the log to facilitate logging and maintenance.
- Special scenario: If there is a requirement for the parent class to share log logic with the child class, you can use
()
Dynamically adapt to subclass names, but requires trade-offs on performance and complexity.
4. Summary
- First choice:
private static final
Combined with the category name, it is both efficient and clear. - Use less: Non-static loggers have poor performance and do not comply with logger design practices.
- Use caution: A logger that dynamically obtains class names, suitable for special scenarios such as parent class general log logic.
In actual development, follow team specifications and choose based on specific needs, and always prioritize readability, performance and maintainability.
This is the article about the three ways and best practices of Logger definition in Java. For more related content on Logger definition in Java, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!