Apache Commons-logging+log4j配置

1 Commons-Loggin 简介
Jakarta Commons Logging (JCL) 提供的是一个日志 (Log) 接口 (interface) ,同时兼顾轻量级和不依赖于具体的日志实现工具 。它提供给中间件 / 日志工具开发者一个简单的日志操作抽象,允许程序开发人员使用不同的具体日志实现工具。用户被假定已熟悉某种日志实现工具的更高级别的细节。 JCL 提供的接口,对其它一些日志工具,包括 Log4J, Avalon LogKit, and JDK 1.4 等,进行了简单的包装,此接口更接近于 Log4J LogKit 的实现。
2 .快速入门
JCL 有两个基本的抽象类: Log( 基本记录器 ) LogFactory( 负责创建 Log 实例 ) 。当 commons-logging.jar 被加入到 CLASSPATH 之后,它会合理地猜测你想用的日志工具,然后进行自我设置,用户根本不需要做任何设置。默认的 LogFactory 是按照下列的步骤去发现并决定那个日志工具将被使用的(按照顺序,寻找过程会在找到第一个工具时中止) :
 

1) 首先在classpath下寻找自己的配置文件commons-logging.properties,如果找到,则使用其中定义的Log实现类;

2) 如果找不到commons-logging.properties文件,则在查找是否已定义系统环境变量org.apache.commons.logging.Log,找到则使用其定义的Log实现类;

如果在Tomact中可以建立一个叫 :CATALINA_OPTS 的环境变量 
给他的值 : - Dorg.apache.commons.logging.Log = org.apache.commons.logging.impl.SimpleLog - Dorg.apache.commons.logging.simplelog.defaultlog = warn 

3) 否则,查看classpath中是否有Log4j的包,如果发现,则自动使用Log4j作为日志实现类;

4) 否则,使用JDK自身的日志实现类(JDK1.4以后才有日志实现类);

5) 否则,使用commons-logging自己提供的一个简单的日志实现类SimpleLog;

org.apache.commons.logging.Log 的具体实现有如下:
-org.apache.commons.logging.impl.Jdk14Logger  使用 JDK1.4
-org.apache.commons.logging.impl.Log4JLogger  使用 Log4J
-org.apache.commons.logging.impl.LogKitLogger  使用 avalon-Logkit
-org.apache.commons.logging.impl.SimpleLog   common-logging 自带日志实现类。它实现了 Log 接口,把日志消息都输出到系统错误流 System.err 中。  
-org.apache.commons.logging.impl.NoOpLog   common-logging 自带日志实现类。它实现了 Log 接口。 其输出日志的方法中不进行任何操作。
3 .使用JCL开发
因为 Log4j 的强大,同时开发者又不希望对 Log4j 的依赖性太强。所以目前比较流行的是 Commons-logging Log4j 结合使用
 
1. 部署日志器
   下载 commons-logging.jar log4j.jar 包,能后把它们放到工程的 lib 目录下,引入工程中。
2. 指定日志器
   在属性文件 common-logging.properties 设置实现接口的类 。如下 ( 这里设置 Log4j 为所使用的日志包 )
 
#commons-logging.properties 文件配置信息
 
# org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
# Must be one of ("trace", "debug", "info", "warn", "error", or "fatal").
 
# log4j
org.apache.commons.logging.Log= org.apache.commons.logging.impl.Log4JLogger
 
#JDK5 Logger
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
 
3 org.apache.commons.logging.Log 接口中定义的方法 , 按严重性由高到低的顺序有:
log.fatal(Object message);
log.fatal(Object message, Throwable t);
log.error(Object message);
log.error(Object message, Throwable t);
log.warn(Object message);
log.warn(Object message, Throwable t);
log.info(Object message);
log.info(Object message, Throwable t);
log.debug(Object message);
log.debug(Object message, Throwable t);
log.trace(Object message);
log.trace(Object message, Throwable t);
除此以外,还提供下列方法以便代码保护。
log.isFatalEnabled();
log.isErrorEnabled();
log.isWarnEnabled();
log.isInfoEnabled();
log.isDebugEnabled();
log.isTraceEnabled();
 
4 .信息级别
确保日志信息在内容上和反应问题的严重程度上的恰当,是非常重要的。
1 fatal 非常严重的错误,导致系统中止。期望这类信息能立即显示在状态控制台上。
 
2 error 其它运行期错误或不是预期的条件。期望这类信息能立即显示在状态控制台上。
 
3 warn 使用了不赞成使用的 API 、非常拙劣使用 API, ' 几乎就是 ' 错误 , 其它运行时不合需要和不合预期的状态但还没必要称为 " 错误 " 。期望这类信息能立即显示在状态控制台上。
 
4 info 运行时产生的有意义的事件。期望这类信息能立即显示在状态控制台上。
 
5 debug 系统流程中的细节信息。期望这类信息仅被写入 log 文件中。
 
6 trace 更加细节的信息。期望这类信息仅被写入 log 文件中。
  通常情况下,记录器的级别不应低于 info. 也就是说,通常情况下 debug 的信息不应被写入 log 文件中。
4 Apache Commons-logging 使用流程
1 )将 common-logging.jar 包加入到环境变量或者 classpath
 
2 )在需要记录日志的类中导入 org.apache.commons.logging.Log ; org.apache.commons.logging.LogFactory ;
 
3 )在需要使用 logging 的地方获取 Log 实例。
private static Log log = LogFactory .getLog (Test. class );
 
4 )使用 Logger 对象的 debug,info,fatal... 方法。
log .debug( "Debug info." );
5.Apache Commons-logging 使用示例
Java代码   收藏代码
  1. import org.apache.commons.logging.Log;  
  2. import org.apache.commons.logging.LogFactory;  
  3.   
  4. public class TestLog {  
  5.     private static Log log = LogFactory.getLog(TestLog.class);  
  6.   
  7.     public void log() {  
  8.         log.debug("Debug info.");  
  9.         log.info("Info info");  
  10.         log.warn("Warn info");  
  11.         log.error("Error info");  
  12.         log.fatal("Fatal info");  
  13.     }  
  14.   
  15.     public static void main(String[] args) {  
  16.         System.out.println("log obj = " + log);  
  17.         TestLog test = new TestLog();  
  18.         test.log();  
  19.     }  
  20. }  
 
结果:

log obj = org.apache.commons.logging.impl.Jdk14Logger @173a10f
2009-11-19 22:48:48 TestLog log
信息: Info info
2009-11-19 22:48:49 TestLog log
警告: Warn info
2009-11-19 22:48:49 TestLog log
严重: Error info
2009-11-19 22:48:49 TestLog log
严重: Fatal info

 
当没有任何配置文件( .properties )时,就如同上的结果。此时,它使用的是使用jdk1.4中的日志工具 ( Jdk14Logger ) ,注,我是在jdk1.5下运行的,当然,如果没jdk版本低于1.4时,会使用 common-logging中的 SimpleLog。
 
6.Apache Commons-logging + log4j 使用
 
1、加入配置文件 commons-logging.properties log4j.properties
 
commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
 
log4j.properties
log4j.rootLogger=info, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
 
2、将 log4j.jar common-logging.jar 两个包加入到环境变量或者 classpath
 
3TestLog .java 内容不变。
 
结果:
log obj = org.apache.commons.logging.impl.Log4JLogger @1ffb8dc
 INFO [main] - Info info
  WARN [main] - Warn info
 ERROR [main] - Error info
 FATAL [main] - Fatal info
 
7.使用Apache Commons-logging 中的 SimpleLog

如果把 commons-logging.properties配置成如下:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog 

则输出:

log obj = org.apache.commons.logging.impl.SimpleLog @69b332
[INFO] TestLog - Info info
[WARN] TestLog - Warn info
[ERROR] TestLog - Error info
[FATAL] TestLog - Fatal info

扫描二维码关注公众号,回复: 469640 查看本文章

为了简化配置,我们可以不使用commons-logging的配置文件,也不设置commons-logging相关的环境变量,只需log4j的包放入classpath就可以了,这样就可以完成commons- logging与Log4j的结合。如果以后不想使用log4j,只需将log4j的包从classpath中移除就可以了。 当然使用时Log4j会根据指定的日志器名(LogFactory.getLog(TestLog.class) )自动搜索classpath下log4j.properties log4j.xml 配置文件中对应的日志器,如果没有找到,可能会显示以下错误信息:
log4j:WARN No appenders could be found for logger (TestLog).
log4j:WARN Please initialize the log4j system properly.

猜你喜欢

转载自z724130632.iteye.com/blog/2254672
今日推荐