`
shiwuyisheng
  • 浏览: 64839 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Apache Commons-logging使用实例(转)

阅读更多

 Apache Commons-logging使用实例(转)

原文地址:http://zhangjunhd.blog.51cto.com/113473/25135

1Commons-Loggin简介

Jakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具体的日志实现工具。它提供给中间件/日志工具开发者一个简单的日志操作抽象,允许程序开发人员使用不同的具体日志实现工具。用户被假定已熟悉某种日志实现工具的更高级别的细节。JCL提供的接口,对其它一些日志工具,包括Log4J, Avalon LogKit, and JDK 1.4等,进行了简单的包装,此接口更接近于Log4JLogKit的实现。

2.快速入门 

JCL有两个基本的抽象类:Log(基本记录器)LogFactory(负责创建Log实例)。当commons-logging.jar被加入到 CLASSPATH之后,它会合理地猜测你想用的日志工具,然后进行自我设置,用户根本不需要做任何设置。默认的LogFactory是按照下列的步骤去发现并决定那个日志工具将被使用的(按照顺序,寻找过程会在找到第一个工具时中止)

Ø 寻找当前factory中名叫org.apache.commons.logging.Log配置属性的值【注:我觉得应该是org.apache.commons.logging.LogFactory

Ø 寻找系统中属性中名叫org.apache.commons.logging.Log的值【注:我觉得应该是org.apache.commons.logging.LogFactory

Ø 如果应用程序的classpath中有log4j,则使用相关的包装(wrapper)(Log4JLogger)

Ø 如果应用程序运行在jdk1.4的系统中,使用相关的包装类(Jdk14Logger)

Ø 使用简易日志包装类(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-loggingLog4j结合使用。 

1. 部署日志器 

下载commons-logging.jarlog4j.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.Log4JCategoryLog 

#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.信息级别

确保日志信息在内容上和反应问题的严重程度上的恰当,是非常重要的。

Ø fatal非常严重的错误,导致系统中止。期望这类信息能立即显示在状态控制台上。

Ø error其它运行期错误或不是预期的条件。期望这类信息能立即显示在状态控制台上。

Ø warn使用了不赞成使用的API、非常拙劣使用API, '几乎就是'错误,其它运行时不合需要和不合预期的状态但还没必要称为 "错误"。期望这类信息能立即显示在状态控制台上。 

Ø info运行时产生的有意义的事件。期望这类信息能立即显示在状态控制台上。 

Ø debug系统流程中的细节信息。期望这类信息仅被写入log文件中。 

Ø trace更加细节的信息。期望这类信息仅被写入log文件中。

  通常情况下,记录器的级别不应低于info.也就是说,通常情况下debug的信息不应被写入log文件中。

4Apache Commons-logging使用流程

1)将common-logging.jar 包加入到环境变量或者classpath

2)导入org.apache.commons.logging.Log; org.apache.commons.logging.LogFactory;及相关包

3)在需要使用logging的地方获取Log实例。

4)使用Logger对象的debug,info,fatal...方法。

private static Loglog = LogFactory.getLog(Test.class);

5.Apache Commons-logging使用示例

使用SimpleLog

Test.java

log.debug("Debug info.");

package sample;

 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

public class Test {

   private static Loglog = LogFactory.getLog(Test.class);

   public void log(){

      log.debug("Debug info.");

      log.info("Info info");

      log.warn("Warn info");

      log.error("Error info");

      log.fatal("Fatal info");

   }

   public static void main(String[] args) {

      Test test =new Test();

      test.log();

   }

}

结果:

DEBUG sample.Test.log(Test.java:13) Debug info.

INFO  sample.Test.log(Test.java:14) Info info

WARN  sample.Test.log(Test.java:15) Warn info

ERROR sample.Test.log(Test.java:16) Error info

FATAL sample.Test.log(Test.java:17) Fatal info

当没有任何配置文件(.properties)时,就如同上的结果。此时,它使用的是使用简易日志包装类(SimpleLog)

下面加入包与配置文件,使其使用log4j

(1)加入配置文件commons-logging.propertieslog4j.properties

(2)将 log4j.jar 和 common-logging.jar 两个包加入到环境变量或者classpath 

(3)Test.java内容不变。

(4)commons-logging.properties

log4j.properties

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

结果:

INFO [main] - Info info

WARN [main] - Warn info

ERROR [main] - Error info

FATAL [main] - Fatal info 

6.Apache Log介绍集

[1] Apache Log4j配置说明

[2] Apache Log4j使用实例

[3] Apache Commons-logging使用实例

[4] 如何自建appender扩展Log4j框架

7.参考资料

[1]官方文档

[2] Jakarta Commons Logging(JCL)开发手记

[url]http://blog.csdn.net/fasttalk/archive/[/url]2005/06/29/406681.aspx

<!--EndFragment-->
分享到:
评论

相关推荐

    commons-logging.jar

    commons-logging-1.0-javadoc.jar, commons-logging-1.0.1-javadoc.jar, commons-logging-1.0.1.jar, commons-logging-1.0.2-javadoc.jar, commons-logging-1.0.2.jar, commons-logging-1.0.3-javadoc.jar, commons-...

    Apache Commons-logging使用实例

    本文将介绍如何在程序中使用Apache Commons-logging

    spring-framework & commons-logging

    spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & commons-logging spring-framework & ...

    commons-logging-1.2-API文档-中英对照版.zip

    赠送jar包:commons-logging-1.2.jar; 赠送原API文档:commons-logging-1.2-javadoc.jar; 赠送源代码:commons-logging-1.2-sources.jar; 包含翻译后的API文档:commons-logging-1.2-javadoc-API文档-中文...

    commons-logging-1.2-API文档-中文版.zip

    赠送jar包:commons-logging-1.2.jar; 赠送原API文档:commons-logging-1.2-javadoc.jar; 赠送源代码:commons-logging-1.2-sources.jar; 包含翻译后的API文档:commons-logging-1.2-javadoc-API文档-中文...

    commons-logging-1.1.3-API文档-中英对照版 (2).zip

    赠送jar包:commons-logging-1.1.3.jar; 赠送原API文档:commons-logging-1.1.3-javadoc.jar; 赠送源代码:commons-logging-1.1.3-sources.jar; 赠送Maven依赖信息文件:commons-logging-1.1.3.pom; 包含翻译后...

    commons-logging-1.1.3.jar

    common-logging是apache提供的一个通用的日志接口。用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的logging, common-logging会通过动态查找的机制,在程序运行时自动找出真正使用的日志库。...

    commons-logging-1.1.3-API文档-中文版.zip

    赠送jar包:commons-logging-1.1.3.jar; 赠送原API文档:commons-logging-1.1.3-javadoc.jar; 赠送源代码:commons-logging-1.1.3-sources.jar; 赠送Maven依赖信息文件:commons-logging-1.1.3.pom; 包含翻译后...

    commons-logging-1.1.1-API文档-中文版.zip

    赠送jar包:commons-logging-1.1.1.jar 赠送原API文档:commons-logging-1.1.1-javadoc.jar 赠送源代码:commons-logging-1.1.1-sources.jar 包含翻译后的API文档:commons-logging-1.1.1-javadoc-API文档-中文...

    commons-logging-1.2-sources.jar

    commons-logging-1.2-sources.jar资源

    commons-logging-1.2-bin.zip下载

    这是commons-logging-1.2-bin.zip,有需要的朋友下载使用

    apache-commons-logging.jar.zip

    标签:apache-commons-logging.jar.zip,apache,commons,logging,jar.zip包下载,依赖包

    开发工具 commons-logging-1.2

    开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发工具 commons-logging-1.2开发...

    commons-beanutils.jar+commons-logging.jar

    Apache提供的这个beanutils包极大方便了javabean的 操作。包含了最新的commons-beanutils-1.9.3.jar和api文档,以及其依赖的commons-logging-1.2.jar包

    commons-codec-1.3.jar,commons-httpclient-3.1.jar,commons-logging-1.1.jar)

    图片上传需要的三个jar包(commons-codec-1.3.jar,commons-httpclient-3.1.jar,commons-logging-1.1.jar)

    commons-logging-1.0.4.jar 免费下载

    java spring 中打印信息的jar包,api发送信息的包,commons-logging-1.0.4-bin.zip,放在lib中,亲测可用。

    commons-logging-1.2.jar

    spring所依赖的Apache common logging包。搭建Spring开发环境,必须用到commons-logging.jar。

    commons-beanutils.jar、commons-logging.jar两个包

    beanUtils 方便访问javaBean 附带支持框架 logging jar包,Apache提供的这个beanutils包极大方便了javabean的 操作。包含了最新的commons-beanutils-1.9.3.jar,以及其依赖的commons-logging-1.2.jar包

    JavaEE源代码 commons-logging-1.0.4

    JavaEE源代码 commons-logging-1.0.4JavaEE源代码 commons-logging-1.0.4JavaEE源代码 commons-logging-1.0.4JavaEE源代码 commons-logging-1.0.4JavaEE源代码 commons-logging-1.0.4JavaEE源代码 commons-logging-...

Global site tag (gtag.js) - Google Analytics