1. 獲得log4j.jar
- 下載log4j.zip的地址: http://labs.mop.com/apache-mirror/logging/log4j/1.2.17/log4j-1.2.17.zip
- 解壓log4j.zip,獲得 log4j-1.2.17.jar
2. 在Eclipse中依賴log4j-1.2.17.jar
- 右擊項目--Build Path-- Configure Build Path--Libraries -- Add External JARs..--選中log4j-1.2.17.jar--確定...
3. 配置 log4j.properties
1. 在項目中src目錄下建立檔案log4j.properties 步驟如下 : 右擊src--new--file--在File Name:輸入log4j.properties---OK...2. 在log4j.properties中新增內容:
log4j.rootLogger=info,
stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= %5p [%t] (%F :%L) - %m%n
OK,接下來可以使用了,具體為舍這樣配置,之後再分析
4. 建立測試檔案test.java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Test
{
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");
}
/**
* @param args
*/
public static void main(String[]
args) {
Test test = new Test();
test.log();
}
}
Run一下,查看結果...OK,你已經知道怎麼用了下面我們來看看為什麼這樣用。。
—— —— —— EOF —— —— ——
作者: 畢小朋| http://blog.csdn.net/wirelessqa郵箱: wirelessqa.me@gmail.com微博: http://weibo.com/wirelessqa 轉載請註明來源: 畢小朋 — http://blog.csdn.net/wirelessqa