JiveJdon Community Forums
在线156人   首页   主题表   培训咨询   标签   精华   查搜   注册    登陆 RSS
首页 » 论坛 » 项目工程开发经验谈
???en_US.forumThreadPrev.name??? 上一主题
google yahoo 365Key网摘 CSDN网摘 添加到百度搜藏 POCO网摘 新浪ViVi 天极网摘
???en_US.forumThreadNext.name??? 下一主题
Go 共有 3 回复 / 1
 发表新帖子   回复该主题贴
mindfloating

悄悄话
发表文章: 23
注册时间: 2005年08月30日 16:06
为什么日志器(logger)要声明为static final的? 2007年10月10日 11:19 到本帖网址 加入本帖到收藏夹 回复该主题
标签
我用PMD的代码分析工具来分析一个项目源代码,提示logger 没有被声明为static final的,很疑惑~
mindfloating

悄悄话
发表文章: 23
注册时间: 2005年08月30日 16:06
re:为什么日志器(logger)要声明为static final的? 2007年10月11日 10:44 到本帖网址 加入本帖到收藏夹 回复该主题
找到一篇blog如是说:
原文链接
[url]http://www.nofluffjuststuff.com/blog/scott_leberknight/2007/03/loggerisnotstaticfinal.html[/url]

For anyone who uses PMD, the title of this blog appears in their list of PMD errors if they don't declare their loggers static and final. Specifically, the LoggerIsNotStaticFinal rule simply says that a log should be declared static and final. I also like to make sure they are private as well. For example:

// 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.

banq

悄悄话
发表文章: 9613
注册时间: 2002年08月03日 17:08
回复:re:为什么日志器(logger)要声明为static final的? 2007年10月11日 12:23 到本帖网址 加入本帖到收藏夹 回复该主题
使用AOP是一个方向,但是性能问题是个麻烦。<BR><BR>使用Anonation可能是一个方向
ghostv1

悄悄话
发表文章: 34
注册时间: 2007年08月29日 16:06
re:为什么日志器(logger)要声明为static final的? 2007年10月19日 16:27 到本帖网址 加入本帖到收藏夹 回复该主题
(1):出于资源利用的考虑,LOGGER的构造方法参数是Class,决定了LOGGER是根据
类的结构来进行区分日志,所以一个类只要一个LOGGER就可以了,故static
(2):final表示一种编程习惯,表示该类的LOGGER只是记录该类的信息,否则日志
会无法提供可以令人信服的记录
标签
共有 3 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG:
正在读取,请等待...
查询本论坛内 回复超过的热门帖子
标题
 
粗体: [b]文本[/b] 斜体: [i]文本[/i] 下划线 [u]文本[/u] 插入图片 [img]http://wwww.xxxx.com/img.ext[/img] 插入代码 [code]程序代码[/code]  插入url链接 [url]http://url[/url] / [url=http://url]URL加下滑线[/url] 插入附件 插入word文档 Txt等文件
内容
  提交时自动拷贝以上内容到剪贴板 Ctrl-V可取出;提问题前先查询标签列表

RSS 手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区 推荐Chrome快速浏览本站
OpenSource JIVEJDON v3.5 Powered by JdonFramework Code © 2002-09 jdon.com

anti spam