现在的位置: 首页 > 综合 > 正文

Jakarta Commons Logging(JCL)开发手记

2012年02月23日 ⁄ 综合 ⁄ 共 6932字 ⁄ 字号 评论关闭

JCL(Jakarta Commons Logging)log4j不都是做log的吗,怎么在jcl的源码包中,还有个log4j的包?倒底怎么回事?看了jcl的用户指南,就明白了。

 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是按照下列的步骤去发现并决定那个日志工具将被使用的(按照顺序,寻找过程会在找到第一个工具时中止):

  1. 寻找当前factory中名叫org.apache.commons.logging.Log配置属性的值

  2. 寻找系统中属性中名叫org.apache.commons.logging.Log的值

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

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

  5. 使用简易日志包装类(SimpleLog)

3、开发使用logging

//在程序文件头部import相关的类
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......
//
在类中获取一个实例
public class Test{
private static Log log = LogFactory.getLog(Test.class);
//
下面是具体代码

...

//下面是调用方法

if (log.isDebugEnabled()) {

                     log.debug("hello,success ");

              }
 }


日志信息被送往记录器,如上例中的log。这个发送过程,是通过调用Log接口中定义的方法完成的,不同方法跟不同的级别联系在一起,日志信息通过哪个级别的方法发送,就标明了日志信息的级别。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();

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

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

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

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

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

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

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

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

  1. 生命周期
    JCL LogFactory
    必须实现建立/断开到日志工具的连接,实例化/初始化/解构一个日志工具.

  2. 异常处理
    JCL Log
    接口没有指定任何异常处理,对接口的实现必须捕获并处理异常。

  3.  

  4. 多线程
    JCL Log
    LogFactory 的实现,必须确保任何日志工具对并行的要求.

注意:
  JCL采用的记录器的不同其设置内容也不同。Log4J是默认首选记录器,对其设置可通过系统属性(system properties)或一个属性文件进行设置。

 

 

下面是官方文档:以备参考

Introduction

The Jakarta Commons Logging (JCL) provides a Log interface that is intended to be both light-weight and an independent abstraction of other logging toolkits. It provides the middleware/tooling developer with a simple logging abstraction, that allows the user (application developer) to plug in a specific logging implementation.

JCL provides thin-wrapper Log implementations for other logging tools, including Log4J , Avalon LogKit , the Avalon Framework's logging infrastructure, JDK 1.4, and an implementation of JDK 1.4 logging APIs (JSR-47) for pre-1.4 systems. The interface maps closely to Log4J and LogKit.

Familiarity with high-level details of the relevant Logging implementations is presumed.

Quick Start

As far as possible, JCL tries to be as unobtrusive as possible. In most cases, including the (full) commons-logging.jar in the classpath should result in JCL configuring itself in a reasonable manner. There's a good chance that it'll guess your preferred logging system and you won't need to do any configuration at all!

Configuration

There are two base abstractions used by JCL: Log (the basic logger) and LogFactory (which knows how to create Log instances). Using LogFactory implementations other than the default is a subject for advanced users only, so let's concentrate on configuring the default implementation.

The default LogFactory implementation uses the following discovery process to determine what type of Log implementation it should use (the process terminates when the first positive match - in order - is found):

1.       Look for a configuration attribute of this factory named org.apache.commons.logging.Log (for backwards compatibility to pre-1.0 versions of this API, an attribute org.apache.commons.logging.log is also consulted).

2.       Look for a system property named org.apache.commons.logging.Log (for backwards compatibility to pre-1.0 versions of this API, a system property org.apache.commons.logging.log is also consulted).

3.       If the Log4J logging system is available in the application class path, use the corresponding wrapper class ( Log4JLogger ).

4.       If the application is executing on a JDK 1.4 system, use the corresponding wrapper class ( Jdk14Logger ).

5.       Fall back to the default simple logging wrapper ( SimpleLog ).

Consult the JCL javadocs for details of the various Log implementations that ship with the component. (The discovery process is also covered in more detail there.)

Configuring The Underlying Logging System

The JCL SPI can be configured to use different logging toolkits (see above ). JCL provides only a bridge for writing log messages. It does not (and will not) support any sort of configuration API for the underlying logging system.

Configuration of the behavior of the JCL ultimately depends upon the logging toolkit being used. Please consult the documentation for the chosen logging system.

Configuring Log4J

Log4J is a very commonly used logging implementation (as well as being the JCL primary default), so a few details are presented herein to get the developer/integrator going. Please see the Log4J Home for more details on Log4J and it's configuration.

Configure Log4J using system properties and/or a properties file:

  • log4j.configuration=log4j.properties Use this system property to specify the name of a Log4J configuration file. If not specified, the default configuration file is log4j.properties.

  • log4j.rootCategory=priority [, appender]* Set the default (root) logger priority.

  • log4j.logger.logger.name=priority Set the priority for the named logger and all loggers hierarchically lower than, or below, the named logger. logger.name corresponds to the parameter of LogFactory.getLog(logger.name), used to create the logger instance. Priorities are: DEBUG, INFO, WARN, ERROR, or FATAL.

    Log4J understands hierarchical names, enabling control by package or high-level qualifiers: log4j.logger.org.apache.component=DEBUG will enable debug messages for all classes in both org.apache.component and org.apache.component.sub. Likewise, setting log4j.logger.org.apache.component=DEBUG will enable debug message for all 'component' classes, but not for other Jakarta projects.

  • log4j.appender.appender.Threshold=priority Log4J appenders correspond to different output devices: console, files, sockets, and others. If appender's threshold is less than or equal to the message priority then the message is written by that appender. This allows different levels of detail to be appear at different log destinations. For example: one can capture DEBUG (and higher) level information in a logfile, while limiting console output to INFO (and higher).

Developing With JCL

To use the JCL SPI from a Java class, include the following import statements:

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

Note that some components using JCL may either extend Log, or provide a component-specific LogFactory implementation. Review the component documentation for guidelines on how commons-logging should be used in such components.

For each class definition, declare and initialize a log attribute as follows:

public class CLASS

{

    private static Log log = LogFactory.getLog(CLASS.class);

    ...

    ;

       

Messages are logged to a logger, such as log by invoking a method corresponding to priority. The org.apache.commons.logging.Log interface defines the following m

抱歉!评论已关闭.