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

發佈了30 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章