Why it is good practice to declare loggers private, static, and final?

// Jakarta Commons Logging
private static final Log log = LogFactory.getLog(MyClass.class);
The above code also shows another good practice, which is to pass the Class object to the getLog() method, instead of a string.
Why the java.util.logging.Logger class doesn‘t even provide a method accepting a Class object is simply beyond me.
Why did the people who developed the java.util.logging package base their API on Log4j yet omit some of the most useful parts of it?
Oh well.Now to the point.
Why it is good practice to declare loggers private, static, and final?
A logger is an internal implementation detail, so it should be private.
You only need one logger for all instances of a class, hence static.
And a logger should not be able to be replaced, thus final.
So if this is good, what‘s not so good (at least in my opinion)?
Simple - any logger that is not private, static, final, and which doesn‘t pass in a Class object to getLog()!
For example, consider this common bit of code, declared in some base class:

// Not so good logger declaration
protected final Log log = LogFactory.getLog(getClass());
Why is this bad? Well, it isn‘t static for one thing.
For another, it uses getClass() to obtain the log.
At first this seems efficient since now all subclasses automatically inherit a ready-made log of the correct runtime type.
So what‘s the issue here?
The biggest problem with loggers declared in this manner is that you now get all the logging from the superclass mixed in with the logging from the subclass,
and it is impossible in the log output to discern which messages came from which class unless you look at the source.
This is really annoying if the superclass has a lot of logging that you don‘t want to see, since you cannot filter it out.
Another problem is that your ability to set log levels differently goes away,
for example if a subclass resides in a different package than the superclass.
In that case, if you try to filter out logging from the superclass, you can‘t because the actual runtime class was used to obtain the logger.
Last, having a protected logger just seems to violate basic object-oriented principles.
Why in the world should subclasses know about an internal implementation detail from a superclass that is a cross-cutting concern, no less?
Anyway, though this is a silly little rant it really is annoying when you extend a superclass that declares a protected logger like this.

Class<? extends ValueOfNull> java.lang.Object.getClass()

Returns the runtime class of this Object.
The returned Class object is the object that is locked by static synchronized methods of the represented class.
The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called.
For example, no cast is required in this code fragment:

Number n = 0;
Class<? extends Number> c = n.getClass();

The Class object that represents the runtime class of this object.

Why it is good practice to declare loggers private, static, and final?

时间: 2024-10-09 04:20:59

Why it is good practice to declare loggers private, static, and final?的相关文章

[RK_2014_1020]Cannot declare member function ‘static int Foo::bar()’ to have static linkage

1. if you declare a method to be static in your .cc file. The reason is that static means something different inside .cc files than in class declarations It is really stupid, but the keyword static has three different meanings. In the .cc file, the s

Java性能提示(全)

http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLists and ArrayLists (and Vectors) (Page last updated May 2001, Added 2001-06-18, Author Jack Shirazi, Publisher OnJava). Tips: ArrayList is faster than

YARN源码分析(四)-----Journalnode

前言 最近在排查公司Hadoop集群性能问题时,发现Hadoop集群整体处理速度非常缓慢,平时只需要跑几十分钟的任务时间一下子上张到了个把小时,起初怀疑是网络原因,后来证明的确是有一部分这块的原因,但是过了没几天,问题又重现了,这次就比较难定位问题了,后来分析hdfs请求日志和Ganglia的各项监控指标,发现namenode的挤压请求数持续比较大,说明namenode处理速度异常,然后进而分析出是因为写journalnode的editlog速度慢问题导致的,后来发现的确是journalnode

Apache Commons IO简介

虽然Google的guava对Java的IO操作进行了一定封装,但是它更偏向于集合.并发和缓存,在实际项目中,我非常喜欢guava,同时我也非常喜欢Apache的一个工具包org.apache.commons.io,这两个工具包提供非常强大的工具能力,能够简化代码逻辑,提高开发效率和质量,是每个Java程序员都应该掌握的工具包.此文简单介绍一下org.apache.commons.io,详细的可参考其API注,此文绝大部分内容翻译自http://www.javacodegeeks.com/201

让你的spring-boot应用日志随心所欲--spring boot日志深入分析

1.spring boot日志概述 spring boot使用Commons Logging作为内部的日志系统,并且给Java Util Logging,Log4J2以及Logback都提供了默认的配置.如果使用了spring boot的Starters,那么默认会使用Logback用于记录日志. 2.spring boot日志默认配置 我们启动一个空的spring-boot项目看一下控制台的日志 控制台的默认配置 logging.pattern.console=%clr(%d{${LOG_DA

实战Arch Unit

在以前的文章中介绍了通过 [<实战PMD>](https://zhuanlan.zhihu.com/p/105585075).[<实战Checkstyle>](https://zhuanlan.zhihu.com/p/105583516)在代码级守护我们的代码,比通过[<实战Jacoco>](https://zhuanlan.zhihu.com/p/105581725)来了解当前项目的测试覆盖情况.通过得到数据了解我们的项目质量,进行定向的改进. 使用这些简单方面的自动

The serializable class does not declare a static final serialVersionUID field of type long

在编译以下Java程序时,出现The serializable class  does not declare a static final serialVersionUID field of type long警告 1 package learn; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 7 public class SimpleGui3C implements ActionList

Java Concurrency in Practice 4.1-4.2相关问题及理解

今天终于又重新拿起了Java Concurrency in Practice,之前被虐的体无完肤,在看这本书之前,有一部分自己写的代码我根本没意识到是线程不安全的,还真的是要恶补这方面的知识. 1.Java监视器模式 监视器模式其实很简单,就是用私有对象的锁或者内置锁来保证所属对象的线程安全性.这里引入一个例子:车辆追踪 public class MonitorVehicleTracker { private final Map<String, MutablePoint> locations;

Eclipse下关于The serializable class UsersServlet does not declare a static final serialVersionUID field of type的警告

The serializable class XXX does not declare a static final serialVersionUID field of type long serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性. 你可以随便写一个,在Eclipse中它替你生成一个,有两种生成方式:一个是默认的1L,比如:private static final long serialVersionUID = 1L;一个是根