commons.logging1.1.1源代码研究(2)-- 基本使用及配置文件

一、Code Guard

为了避免运行时过多的负载,请使用log.is<Priority >()来判断当前是否这个日志级别可以记录

 

二、优先级层次

1.fatal  记录严重及致命的错误

2.error  运行时错误及不希望出现的条件

3.warn  警告(比如:使用了不建议的API,运行时一些不一定是错误的情况)

4.info  一般的运行时提示信息

5.debug  调试信息

6.trace   最详细的细节信息

默认的消息优先级为info

 

三、记录日志的建议

1.检查异常和非检查异常(外部边界)

   如果是由API内部代码引起,则记录信息以debug层次;如果是由调用API的代码引起,则记录堆栈信息以info层次

2.内部边界

   如果异常由内部引起,并且由内部解决;记录以info或debug层次

3.重要的内部边界(比如:跨网络)

   以info层次记录日志

 

四、配置文件

commons-logging.properties  配置文件名称(必须放到项目根目录下)

配置:(分为系统属性和文件属性,文件属性覆盖系统属性,但有些只能通过系统属性或文件属性明确)

1.只从系统属性中读

org.apache.commons.logging.diagnostics.dest -- 指定诊断信息的输出(STDERR、STDOUT或文件名)

org.apache.commons.logging.LogFactory.HashtableImpl  -- 指定<ClassLoader,LogFactory>的缓存哈希表的类名

2.只从文件属性中读

priority  -- 配置文件的优先级,值越大优先级越高(数值型)

在非Webapp中无用;在Webapp中,可以通过不同的ClassLoader读入不同的commons-logging.properties文件

The purpose of the priority field is to allow a webserver administrator to override logging settings in all webapps by placing a commons-logging.properties file in a shared classpath location with a priority > 0; this overrides any commons-logging.properties files without priorities which are in the webapps. Webapps can also use explicit priorities to override a configuration file in the shared classpath if needed.

use_tccl  --  是否通过thread context classloader读入日志类

3.从两者(文件属性覆盖系统属性)

org.apache.commons.logging.Log  -- 指定日志器实现类

org.apache.commons.logging.log  -- 指定日志器实现类,同旧版的logging兼容

搜索顺序:

1.查询配置文件属性org.apache.commons.logging.Log
2.如果没找到,得到系统上述属性
3.如果没找到,按照Log4J、JDK14Logger、Jdk13LumberjackLogger、SimpleLog方式产生日志器


org.apache.commons.logging.LogFactory  --  指定日志器工厂的实现类(默认为 org.apache.commons.logging.impl.LogFactoryImpl)

org.apache.commons.logging.Log.allowFlawedContext -- if tolerates bad context classloaders (true /false)

org.apache.commons.logging.Log.allowFlawedDiscovery  -- if tolerates bad logging adapters(true /false)

org.apache.commons.logging.Log.allowFlawedHierarchy  -- if tolerates bad Log class hierarchy(true /false)

 

 

 样例配置:使用SimpleLog 

 commons-logging.properties #################### org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog simplelog.properties #################### org.apache.commons.logging.simplelog.showlogname=true org.apache.commons.logging.simplelog.showShortLogname=false org.apache.commons.logging.simplelog.showdatetime=true org.apache.commons.logging.simplelog.dateTimeFormat=yyyy-MM-dd hh:mm:ss org.apache.commons.logging.simplelog.log.Test.TestClass=debug org.apache.commons.logging.simplelog.log.Test.TestA=info

 

样例代码:

package Test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class TestA { public Log logger = LogFactory.getLog(TestA.class); public void log() { Throwable th = new Throwable(); StackTraceElement locs[] = th.getStackTrace(); for (int i = 0; i < locs.length; i++) { System.out.println(locs[i].getClassName() + " " + locs[i].getMethodName()); } } } package Test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class TestClass { public Log logger = LogFactory.getLog(TestClass.class); public static void main(String[] args) { System.setProperty("org.apache.commons.logging.diagnostics.dest", "STDOUT"); TestClass test = new TestClass(); test.logger.info("test"); test.logger.debug("haha"); test.logger.trace("trace"); TestA ta = new TestA(); ta.logger.info("test"); ta.logger.debug("debug"); try { String str = null; str.toString(); } catch (Exception e) { ta.log(); } } }

 

  

你可能感兴趣的:(exception,ClassLoader,Class,logging,hierarchy,webapp)