commons.logging1.1.1源代码研究(3)-- 日志器Log接口,SimpleLog实现

本文详细介绍了Apache Commons Logging的Log接口,包括is<Level>Enabled()方法和不同层次的日志记录方法。同时,文章还探讨了SimpleLog的实现,包括其配置文件simplelog.properties的设置,如日志显示选项和日期格式。SimpleLog通过构造函数实现日志级别继承,并在log()方法中处理日志输出。最后,文章提到了getResourceAsStream()方法的权限处理和获取ClassLoader的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Log接口的定义如下:

 

其中主要的方法有:

is<Level>Enabled()  --  是否某个层次的日志开启

<Level>(Object message) -- 记录日志消息

<Level>(Object message, Throwable t)  -- 记录日志消息和异常

其中<Level>指trace,debug,info,wran,error,fatal

 

SimpleLog实现:

 

simplelog.properties必须放在根路径下,是配置文件

配置前缀:“org.apache.commons.logging.simplelog.”

showlogname  --  是否显示日志名(true/false

showShortLogname -- 是否显示短日志名(true /false)

showdatetime  --  是否显示时间(true/false

dateTimeFormat -- 日期格式(默认为yyyy/MM/dd HH:mm:ss:SSS zzz)

defaultlog -- 指定默认日志的层次(info,debug等)

log.<类的完全路径名> -- 指定某一类的日志的层次

比如:org.apache.commons.logging.simplelog.log.Test.TestClass=debug
         org.apache.commons.logging.simplelog.log.Test.TestA=info

         那么Test.TestClass类的日志层次为debug;而Test.TestA的日志层次为info

 

SimpleLog一些重要代码分析:

(1)构造函数

其中while循环处实现了日志器层次的继承性,即:

(1)如果没有设置,则黙认为info

(2)如果对某一类明确了日志层次,如:org.apache.commons.logging.simplelog.log.Test.TestA(Test.TestA类),则使用这个明确的日志层次;如果没明确,则向上寻找org.apache.commons.logging.simplelog.log.Test;如果还没找到,则找org.apache.commons.logging.simplelog.defaultlog

 

(2)日志函数

这段代码很简单,要注意的是:

showlogname 显示的是完全的类名(包括包名);showShortLogname只显示类名

注意:SimpleDateFormat不是线程安全的,在多线程中必须同步

Date formats are not synchronized.
 * It is recommended to create separate format instances for each thread.
 * If multiple threads access a format concurrently, it must be synchronized

 

(3)具体的日志方法

先判断是否这个层次的日志开启,然后调用写日志方法

 

(4)得到资源的方法

AccessController的doPrivileged方法给了其中的代码“特权”,从而不用checkPermissions.

以下得到ClassLoader

 

以下是Thread类的getContextClassLoader()方法,可能会报SecurityException

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值