// 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?的更多相关文章

  1. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  2. Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses

    5.1. Classes, Superclasses, and SubclassesLet's return to the Employee class that we discussed in th ...

  3. Log4J2用法

    一.    关于Log4J 2015年5月,Apache宣布Log4J 1.x 停止更新.最新版为1.2.17. 如今,Log4J 2.x已更新至2.7. 官方网址:http://logging.ap ...

  4. 单例模式多线程安全写法(double-lock-check)

    原始版本 public static Object getInstance() { if (instance != null) { return instance; } instance = new ...

  5. 读取properties文件的信息

    1.properties配置文件的信息 fcsimage_path=C://FCSImage 2.Java代码 public final class Config { private static f ...

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

    1.spring boot日志概述 spring boot使用Commons Logging作为内部的日志系统,并且给Java Util Logging,Log4J2以及Logback都提供了默认的配 ...

  7. java中的双重锁定检查(Double Check Lock)

    原文:http://www.infoq.com/cn/articles/double-checked-locking-with-delay-initialization#theCommentsSect ...

  8. 实战Arch Unit

    在以前的文章中介绍了通过 [<实战PMD>](https://zhuanlan.zhihu.com/p/105585075).[<实战Checkstyle>](https:// ...

  9. Pramp mock interview (4th practice): Matrix Spiral Print

    March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...

随机推荐

  1. 【Linux】rsync同步文件 & 程序自启动

    rsync使用 1. 为什么使用rsync? rsync解决linux系统下文件同步时, 增量同步问题. 使用场景: 线上需要定时备份数据文件(视频资源), 使用rsync完成每天的增量备份. 参见: ...

  2. C#使用反射工厂模式遇到System.TypeLoadException(类名错误导致)

    项目中,在运行另一个反射类时出现问题,未能从程序集中加载类 class PopUpActionFactory { public static InterfacePopUpAction getActio ...

  3. wamp Server2.5 配置 自定义目录

    煎熬了两天终于找到了方法!!! 前提先改成中文 右键"W"图表-> Language -> chinese; 成功改为中文. 自定义目录步骤: 一.添加一个Alias ...

  4. 定时备份服务器数据库(借助windows任务计划以及mysqldump)

    最近社区多了,考虑到数据的安全性,要每天备份一次数据库,以防万一: linux目前还不是很了解,先用windows的计划任务吧: 大体思路就是 借用windows的计划任务来执行备份远程数据库到本地: ...

  5. php curl抓取远程页面内容的代码

    使用php curl抓取远程页面内容的例子. 代码如下: <?php /** * php curl抓取远程网页内容 * edit by www.jbxue.com */ $curlPost = ...

  6. C语言控制语句总结(if else for switch while break continue)

    一.if语句 1表达式 if(条件表达式) 语句 注: (1)条件表达式,一般为逻辑表达式或关系表达式,但也可以是任何数值类型,如整型.实型.字符型.指针型数据等. (2)语句,由于是C语言的语句,而 ...

  7. Only the original thread that created a view hierarchy can touch its views

    在调试软件的时候出现如下的错误: 01-05 20:53:36.492: E/ZZShip(2043): android.view.ViewRootImpl$CalledFromWrongThread ...

  8. linux标准输入输出重定向

    command > filename 把标准输出重定向到一个文件,如果文件不存在则新建,如果存在则覆盖其内容.command >> filename 把标准输出重定向到一个文件中,如 ...

  9. 一步步学习NHibernate(10)——连接查询和子查询(2)

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,老魏讲述了HQL的链接查询,本章呢,老魏开始讲述HQL的子查询.子查询在SQL中也是占据着非常重要的作用,如果没有 ...

  10. Mac OS X 软件推荐

    ​1. 前言 每个操作系统都有自己的一套软件系统,但是不同的用户却会有不同的需求,系统虽会为用户提供一些基础软件,不过为了能无碍的进入自己的学习和工作状态,总有一些软件是必须安装的,同时这些软件也可以 ...