log4j日誌檔案的運用
匯入log4j-1.2.7.jar和commons-logging-api-1.1.jar(版本沒有規定)
新建log4j.properties配置檔案,內容如下:
log4j.rootLogger=ALL, CA ,file
# ConsoleAppender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %d{yyyy-MM-dd HH:mm:ss} %5p %c %t: - %m%n
# FileAppender
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=${catalina.home}/logs/ddoMsg.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern= %d{yyyy-MM-dd HH:mm:ss} %5p %c %t: - %m%n
log4j.properties檔案放在專案名/src檔案下。
java程式碼如下:
package com;
import org.apache.log4j.Logger;
public class test01 {
private static Logger logger = Logger.getLogger(test01.class);
public static void main(String[] args) {
//輸出等級由小到大
logger.debug("****** debug start ****** ");
logger.info("****** info start ****** ");
logger.warn("****** warn start ****** ");
logger.error("****** error start ****** ");
logger.fatal("****** fatal start ****** ");
}
}