log4j使用整理材料(4)

1.1.1           Apache Commons-logging使用流程

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

2)导入所有需的commongs-logging

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

3)在需要使用logging的类中获取Log实例。

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

注意:这里定义的是static成员,以避免产生多个实例。 LogFactory.getLog()方法的参数使用的是当前类的class,这是目前被普通认为的最好的方式。为什么不写成LogFactory.getLog(this.getClass())?因为static类成员访问不到this指针!

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

log.debug("Debug info.");                    

注意:

每个需要写日志的java类都得创建一个static logger实例,如果java类很多的话,那创建这些static对象的开销将非常大,所以最后自己写一个log,有一个静态方法可以得到logger实例:

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class Logs {

         /**
       * Define a static Log variable,
       */  

private static Log log;

static{

log=LogFactory.getLog(Logs.class);

}

/**

* Get the log object
    * @return Log
    */

public static Log getLogger(){

    return log;

}

}

上面代码使用commons-logging生成一个静态log实例,以后的程序就可以这样来做:

Logs.getLogger().info("begin Action: UserBaseInfoAction.getSingleUserInfo()");
DBSession.begin();
String fname=userForm.getFname();
userForm=UserBaseInfoBusiness.getSingleUserInfo(DBSession.getSession(),fname);
DBSession.commit();
request.setAttribute("userInfo",userForm);
Logs.getLogger().info("end Action: UserBaseInfoAction.getSingleUserInfo()");

Log4j的实现方法类似,初始化类可以这样写:
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Logs {
   private static Logger log logger;
    /**
     * Define a static Log variable,
     */  
          static{
                   try{
                     logger=Logger.getLogger(LogInit.class);     
                    //DOMConfigurator.configure("E:/study/log4j/log4j.xml");//
加载.xml文件
                    //PropertyConfigurator.configure("log4j.properties");//
加载.properties文件                    }catch(Exception ex){
                            System.out.println("can't init the Logger, caused by: "+ex);
                     }
              }  
       /**
        * Get the log object
        * @return Log
        */
       public static Logger getLogger(){
              return logger;
       }
}
应用程序中的调用是完全相同的。

1.1.2           Apache Commons-logging使用示例

Test.java

package sample;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class Test {

    private static Log log = 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

<!--[if !supportLists]-->1 

<!--[endif]-->

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

<!--[if !supportLists]-->2 

<!--[endif]-->

log4j.jar common-logging.jar 两个包加入到环境变量或者classpath

3Test.java内容不变。

<!--[if !supportLists]-->3  <!--[endif]-->commons-logging.properties

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

4log4j.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

结果:

INFO [main] - Info info

WARN [main] - Warn info

ERROR [main] - Error info

FATAL [main] - Fatal info

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值