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?的更多相关文章
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- 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 ...
- Log4J2用法
一. 关于Log4J 2015年5月,Apache宣布Log4J 1.x 停止更新.最新版为1.2.17. 如今,Log4J 2.x已更新至2.7. 官方网址:http://logging.ap ...
- 单例模式多线程安全写法(double-lock-check)
原始版本 public static Object getInstance() { if (instance != null) { return instance; } instance = new ...
- 读取properties文件的信息
1.properties配置文件的信息 fcsimage_path=C://FCSImage 2.Java代码 public final class Config { private static f ...
- 让你的spring-boot应用日志随心所欲--spring boot日志深入分析
1.spring boot日志概述 spring boot使用Commons Logging作为内部的日志系统,并且给Java Util Logging,Log4J2以及Logback都提供了默认的配 ...
- java中的双重锁定检查(Double Check Lock)
原文:http://www.infoq.com/cn/articles/double-checked-locking-with-delay-initialization#theCommentsSect ...
- 实战Arch Unit
在以前的文章中介绍了通过 [<实战PMD>](https://zhuanlan.zhihu.com/p/105585075).[<实战Checkstyle>](https:// ...
- 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 ...
随机推荐
- Qt自绘窗体
也许大部分情况下我们不需要自己手动绘制一个窗体,大部分可以通过图片来实现,本篇仅以学习的态度来初略的理解Qt界面的自定义绘制功能. 本篇将实现以下功能: 1.绘制一个椭圆形 2.支持界面的移动操作 ...
- Poj 3117 World Cup
1.Link: http://poj.org/problem?id=3117 2.Content: World Cup Time Limit: 1000MS Memory Limit: 65536 ...
- js定义参数默认值
javascript可以用arguments定义参数组. 一.简单的定义参数默认值 function test1(a,b){ //如果有参数一,则返回参数一,如果没有返回默认值"这是参数 ...
- C#委托的异步调用1
本文将主要通过“同步调用”.“异步调用”.“异步回调”三个示例来讲解在用委托执行同一个“加法类”的时候的的区别和利弊. 首先,通过代码定义一个委托和下面三个示例将要调用的方法: /*添加的命名空间 u ...
- preventDefault()、stopPropagation()、return false 之间的区别
“return false”之所以被误用的如此厉害,是因为它看起来像是完成了我们交给它的工作,浏览器不会再将我们重定向到href中的链接,表单也不会被继续提交,但这么做到底有什么不对呢? 可能在你刚开 ...
- spring-cloud-feign案例
主要依赖 <dependencyManagement> <dependencies> <dependency> <groupId>org.springf ...
- 类似FirePhp的Chrome.php 调试php
之前一直用firephp来调试php,主要受限Firefox启动太慢,研究了下chromephp; 写了个简单的判断模版: <?php /** * @Author: Klaus * @Date: ...
- win8安装python环境和pip & easy_install工具
最近在学python,2.7.6的版本 首先安装python2.7 官网下载地址https://www.python.org/downloads/ 下载相应版本即可,应该是一个msi的文件,默认安装到 ...
- Web服务器集群搭建关键步骤纪要
前言:本文记述了搭建一个小型web服务器集群的过程,由于篇幅所限,系统.软件的安装和基本配置我这里就省略了,只记叙关键配置和脚本内容.假如各位朋友想了解各软件详细配置建议查阅官方文档. 一 需求分析: ...
- java 两个日期之间的标准工作日(原创)
package control; import java.text.ParseException; import java.text.SimpleDateFormat; import java.uti ...