Posted on 2009-02-16 20:54
啥都寫點 閱讀(2450)
評論(0) 編輯 收藏 所屬分類:
J2EE
Log4J 是如何進行工作的?
這是在 Log4J 項目中對 Log4J 的介紹中的一段話
With log4j it is
possible to enable logging at runtime without modifying the application binary.
The log4j package is designed so that these statements can remain in shipped
code without incurring a heavy performance cost. Logging behavior can be
controlled by editing a configuration file, without touching the application
binary.
對于 Log4J 的具體如何進行工作的原理,在 javaeye 上 buaawhl 給出了一個解釋:
http://buaawhl.javaeye.com/blog/6450
總結一下就是利用 JDK 提供的 new Throwable().getStackTrace() 方法返回當前運行棧的結構層次,然后對返回的調用棧的信息進行分析,找到調用該類的類的名稱。(也就是Log4J自己說的Runtime期間實現Log)
由于在 Log4J 中只存在一個根 Logger ,并且 Logger 之間存在繼承的關系,并且我們可以在配置文件( log4J.proerties 或者 XML )中進行配置,可以初始化指定的 Logger ,并且對指定的 Logger 進行一些相關的配置,如設置如何進行輸出,以及如何將輸出進行布局。并且由于 Logger 之間會存在繼承的關系(會找一個最近的 Logger 進行繼承,如果沒有就以根 Logger 作為父類,共享父類的配置信息),以及父子類之間初始化沒有先后的順序,所以我們可以在配置文件中,對各個程序以及不同的包中使用到 Log4j 的 Log 紀錄方式進行配置。
如果在同一個
APPLICATION 對相同名稱的 Logger 進行調用,那么只會返回一個 Logger 實例。
Log4J 會不會對系統的性能造成大的影響?
這是對 Log4J 性能的一些描述,看來還是不錯的,呵呵,在調整一下 Log 的等級就可以了。
On an AMD Duron
clocked at 800Mhz running JDK 1.3.1, it costs
about 5 nanoseconds to determine if a logging statement should be logged or
not. Actual logging is also quite fast, ranging from 21 microseconds using the
SimpleLayout, 37 microseconds using the TTCCLayout. The performance of the
PatternLayout is almost as good as the dedicated layouts, except that it is
much more flexible.
注:對于 Common
Logging 和 Log4J 的關系可以參見 (其實 JCL
是一個規范,對一些 Log 工具進行了包裝,我們常會使用到的具體實現還是 Log4j ,當然也可以使用其他的實現)
http://hedong.3322.org/archives/000316.html
參考資料:
http://www-128.ibm.com/developerworks/cn/java/l-log4j/index.html
使用 Log4J 進行日志操作
http://zooo.51.net/heavyz_cs/notebook/log4j.html
Log4J 學習筆記(推薦)
http://www.dingl.com/view.shtml?xh=485
log4j 配置
http://hedong.3322.org/archives/000193.html
竹筍炒肉的 Log4J 學習筆記
http://forum.springside.org.cn/redirect.php?fid=3&tid=382&goto=nextoldset
SpringSide 對 Log4j 的使用要點
--
學海無涯