LOG4J原理以及配置的研究

在进行Log4J的使用过程中,我对下面的两个问题比较敏感:

1、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实例。

2、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.htmlLog4J学习笔记(推荐)

http://www.dingl.com/view.shtml?xh=485log4j配置

http://hedong.3322.org/archives/000193.html竹笋炒肉的Log4J学习笔记

http://forum.springside.org.cn/redirect.php?fid=3&tid=382&goto=nextoldsetSpringSide对Log4j的使用要点

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值