log4详细配

    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 
[email protected] 
54.log4j.appender.mail.smtphost=www.wusetu.com 
55.log4j.appender.mail.subject=log4j message 
[email protected] 
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 = [email protected] 
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 

你可能感兴趣的:(java,工作)