1、首先去apache.org网站下载最新版本的log4j组件,推荐w使用1.2.x版本,下载地址:http://logging.apache.org/log4j/1.2/download.html;
2、下载下来之后,找到其它相关联的Jar包,主要有如下几个:log4j.jar,common-logging.jar,common-io.jar,common-lang.jar,common-logging-api.jar,将它们添加到我们工程的classpath路径下;
3、在我们自己的工程中src根目录下创建log4j.properties配置文件,配置文件里的内容如下:
Java代码
1.log4j.rootLogger=DEBUG, CONSOLE
2.log4j.additivity.org.apache=true
3.
4.#Console Appender 1
5.log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
6.log4j.appender.CONSOLE.Threshold=DEBUG
7.log4j.appender.CONSOLE.Target=System.out
8.log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
9.log4j.appender.CONSOLE.layout.ConversionPattern=[%t]%d %c %l-%m%n
10.
11.#Console Appender 2
12.#log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
13.#log4j.appender.Threshold=DEBUG
14.#log4j.appender.CONSOLE.Target=System.out
15.#log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
16.#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
17.
18.#File Appender
19.log4j.appender.FILE=org.apache.log4j.FileAppender
20.log4j.appender.FILE.File=file.log
21.log4j.appender.FILE.Append=false
22.log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
23.log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
24.
25.#Rolling File
26.log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
27.log4j.appender.ROLLING_FILE.Threshold=ERROR
28.log4j.appender.ROLLING_FILE.File=rolling.log
29.log4j.appender.ROLLING_FILE.Append=true
30.log4j.appender.ROLLING_FILE.MaxFileSize=10KB
31.log4j.appender.ROLLING_FILE.MaxBackupIndex=1
32.log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
33.log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
34.
35.#Socket Appender
36.log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender
37.log4j.appender.SOCKET.RemoteHost=localhost
38.log4j.appender.SOCKET.Port=5001
39.log4j.appender.SOCKET.LocationInfo=true
40.
41.# Set up for Log Facter 5
42.log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout
43.log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n
44.
45.# Log Factor 5 Appender
46.log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender
47.log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
48.
49.#Send the logs to mailbox
50.log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
51.log4j.appender.MAIL.Threshold=FATAL
52.log4j.appender.MAIL.BufferSize=10
53.log4j.appender.MAIL.From=web@www.wuset.com
54.log4j.appender.MAIL.SMTPHost=www.wusetu.com
55.log4j.appender.MAIL.Subject=Log4J Message
56.log4j.appender.MAIL.To=web@www.wusetu.com
57.log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
58.log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
59.
60.#Database Appender
61.log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
62.log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test
63.log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
64.log4j.appender.DATABASE.user=root
65.log4j.appender.DATABASE.password=root
66.log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')
67.log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
68.log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
69.log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
70.log4j.appender.A1.File=SampleMessages.log4j
71.log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j'
72.log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout
73.
74.#Custom Appender
75.log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender
76.log4j.appender.im.host = mail.cybercorlin.net
77.log4j.appender.im.username = username
78.log4j.appender.im.password = password
79.log4j.appender.im.recipient = corlin@cybercorlin.net
80.log4j.appender.im.layout=org.apache.log4j.PatternLayout
81.log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
将文件保存后,在工程的src目录下随便创建一个类,如LoggerExample.java,其源代码如下:
Java代码
1.package com.hanmiao.log;
2.
3.import org.apache.log4j.Logger;
4.
5.public class LoggerExample {
6.
7. static Logger log = Logger.getLogger(com.hanmiao.log.LoggerExample.class.getName());
8.
9. public static void main(String[] args) {
10. log.info("This is info");
11. log.debug("This is debug");
12. log.warn("This is warn");
13. log.error("This is error");
14. log.fatal("This is fatal");
15. }
16.}
编译运行之后,我们可以看到日志信息被输出到控制台了,内容如下:
Java代码
1.[main]2011-08-16 20:34:08,910 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:10)-This is info
2.[main]2011-08-16 20:34:08,913 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:11)-This is debug
3.[main]2011-08-16 20:34:08,913 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:12)-This is warn
4.[main]2011-08-16 20:34:08,913 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:13)-This is error
5.[main]2011-08-16 20:34:08,913 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:14)-This is fatal
我自己的样例代码附在本文后面,以方便日后参考。
另外,在写这篇文章时,我也重点参考了两篇文章如下:
1、log4j使用方法收藏:http://www.blogjava.net/sy1214520/archive/2008/11/10/239764.html
2、log4j.xml配置方法详解:http://ajava.org/course/open/15990.html