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