create log by common-logging and log4j in java project

      Serval days ago, I had to write a log for an project, and the first option is common-logging and log4j,common-logging provides the generic definition of  log operations.log4j is a detail implementation,all of its functions can be controled by a configuration file which must be named log4j.properties,and the propertty file must locate at the root directory of your project.And a configuration file for common-logging is also necessary,its name must be common-logging.properties,its location just same as the log4j.properties which is root directory of your project.

Let us begin to talk about how to use log4j and common-logging together step by step:

Step 1:Add the libraries (common-logging-1.1.1.jar and log4j-1.2.15.jar)to your project and write the configuration file common-logging.properties and log4j.properties.

the following two files are examples:

file:common-logging.properties

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

file:log4j.properties

##LOGGERS##
#define a logger
log4j.rootLogger=ERROR,console,file
#log4j.rootLogger=DEBUG,console,file
##APPENDERS##
#define an appender named console,which is set to be a ConsoleAppender
log4j.appender.console=org.apache.log4j.ConsoleAppender
#define an appender named file,which is set to be a RollingFileAppender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log.txt
##LAYOUTS##
#assign a SimpleLayout to console appender
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
#assign a PatternLayout to file appender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern= %-5p ,%d{yyyy-MM-dd HH:mm}, [%t], %m%n

Step2:Use log function in your java application

the following is an example java source file

file:Main.java

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * this class is an log example,it shows how to use commons-logging and log4j
 * together.
 *
 * @author ***
 * @version 0.1
 */
public class Main {

    /**
     * get a Log object from LogFactory,first it check if there is a properties
     * file named commons-logging,this file specifies which kind of Log
     * implementation will be used,in this example we specify Log4J as the Log
     * implementation by <code>org.apache.commons.logging.Log =
     *      org.apache.commons.logging.impl.Log4JLogger </code>
     * so this Log object log will automatically use Log4j.
     */
    private Log log = LogFactory.getLog(Main.class);

    /**
     * main method,the entrance of the program
     *
     * @param args  arguements
     */
    public static void main(String[] args) {
        new Main().display();
    }

    /**
     * display method will output all levels of log
     */
    public void display() {
        log.debug("this is debug");
        log.fatal("this is fatal");
        log.info("this is info");
        log.error("this is error");
        log.warn("this is warn");
    }

}

    The preceding example is for java application,it is similar with the web application,you just locate the two configuration file to /WEB-INF/classes,and the else is absolutely the same as the preceding example.

 

complete time:Sep 19th 2008

place: Neusoft Dalian

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值