Posted on 2009-02-16 20:54
啥都寫點(diǎn) 閱讀(2450)
評(píng)論(0) 編輯 收藏 所屬分類:
J2EE
Log4J 是如何進(jìn)行工作的?
這是在 Log4J 項(xiàng)目中對(duì) 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.
對(duì)于 Log4J 的具體如何進(jìn)行工作的原理,在 javaeye 上 buaawhl 給出了一個(gè)解釋:
http://buaawhl.javaeye.com/blog/6450
總結(jié)一下就是利用 JDK 提供的 new Throwable().getStackTrace() 方法返回當(dāng)前運(yùn)行棧的結(jié)構(gòu)層次,然后對(duì)返回的調(diào)用棧的信息進(jìn)行分析,找到調(diào)用該類的類的名稱。(也就是Log4J自己說(shuō)的Runtime期間實(shí)現(xiàn)Log)
由于在 Log4J 中只存在一個(gè)根 Logger ,并且 Logger 之間存在繼承的關(guān)系,并且我們可以在配置文件( log4J.proerties 或者 XML )中進(jìn)行配置,可以初始化指定的 Logger ,并且對(duì)指定的 Logger 進(jìn)行一些相關(guān)的配置,如設(shè)置如何進(jìn)行輸出,以及如何將輸出進(jìn)行布局。并且由于 Logger 之間會(huì)存在繼承的關(guān)系(會(huì)找一個(gè)最近的 Logger 進(jìn)行繼承,如果沒(méi)有就以根 Logger 作為父類,共享父類的配置信息),以及父子類之間初始化沒(méi)有先后的順序,所以我們可以在配置文件中,對(duì)各個(gè)程序以及不同的包中使用到 Log4j 的 Log 紀(jì)錄方式進(jìn)行配置。
如果在同一個(gè)
APPLICATION 對(duì)相同名稱的 Logger 進(jìn)行調(diào)用,那么只會(huì)返回一個(gè) Logger 實(shí)例。
Log4J 會(huì)不會(huì)對(duì)系統(tǒng)的性能造成大的影響?
這是對(duì) Log4J 性能的一些描述,看來(lái)還是不錯(cuò)的,呵呵,在調(diào)整一下 Log 的等級(jí)就可以了。
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.
注:對(duì)于 Common
Logging 和 Log4J 的關(guān)系可以參見(jiàn) (其實(shí) JCL
是一個(gè)規(guī)范,對(duì)一些 Log 工具進(jìn)行了包裝,我們常會(huì)使用到的具體實(shí)現(xiàn)還是 Log4j ,當(dāng)然也可以使用其他的實(shí)現(xiàn))
http://hedong.3322.org/archives/000316.html
參考資料:
http://www-128.ibm.com/developerworks/cn/java/l-log4j/index.html
使用 Log4J 進(jìn)行日志操作
http://zooo.51.net/heavyz_cs/notebook/log4j.html
Log4J 學(xué)習(xí)筆記(推薦)
http://www.dingl.com/view.shtml?xh=485
log4j 配置
http://hedong.3322.org/archives/000193.html
竹筍炒肉的 Log4J 學(xué)習(xí)筆記
http://forum.springside.org.cn/redirect.php?fid=3&tid=382&goto=nextoldset
SpringSide 對(duì) Log4j 的使用要點(diǎn)
--
學(xué)海無(wú)涯