log4j配置详解与使用方法说明
1、首先去apache.org网站下载最新版本的log4j组件,推荐w使用1.2.x版本,下载地址:;
2、下载下来之后,找到其它相关联的Jar包,主要有如下几个:log4j.jar,common-logging.jar,common-io.jar,common-lang.jar,common-logging-api.jar,将它们添加到我们工程的classpath路径下;
3、在我们自己的工程中src根目录下创建log4j.properties配置文件,配置文件里的内容如下:
Java代码
log4j.rootLogger=DEBUG, CONSOLE
log4j.additivity.org.apache=true
#Console Appender 1
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[%t]%d %c %l-%m%n
#Console Appender 2
#log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
#log4j.appender.Threshold=DEBUG
#log4j.appender.CONSOLE.Target=System.out
#log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
#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
#File Appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=file.log
log4j.appender.FILE.Append=false
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#Rolling File
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold=ERROR
log4j.appender.ROLLING_FILE.File=rolling.log
log4j.appender.ROLLING_FILE.Append=true
log4j.appender.ROLLING_FILE.MaxFileSize=10KB
log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#Socket Appender
log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender
log4j.appender.SOCKET.RemoteHost=localhost
log4j.appender.SOCKET.Port=5001
log4j.appender.SOCKET.LocationInfo=true
# Set up for Log Facter 5
log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout
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
# Log Factor 5 Appender
log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender
log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
#Send the logs to mailbox
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold=FATAL
log4j.appender.MAIL.BufferSize=10
log4j.appender.MAIL.SMTPHost=
log4j.appender.MAIL.Subject=Log4J Message
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#Database Appender
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver
log4j.appender.DATABASE.user=root
log4j.appender.DATABASE.password=root
log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=SampleMessages.log4j
log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j'
log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout
#Custom Appender
log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender
log4j.appender.im.host = mail.cybercorlin.net
log4j.appender.im.username = username
log4j.appender.im.password = password
log4j.appender.im.recipient =
log4j.appender.im.layout=org.apache.log4j.PatternLayout
log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
将文件保存后,在工程的src目录下随便创建一个类,如LoggerExample.java,其源代码如下:
Java代码
package com.hanmiao.log;
import org.apache.log4j.Logger;
public class LoggerExample {
static Logger log = Logger.getLogger(com.hanmiao.log.LoggerExample.class.getName());
public static void main(String[] args) {
log.info("This is info");
log.debug("This is debug");
log.warn("This is warn");
log.error("This is error");
log.fatal("This is fatal");
}
}
编译运行之后,我们可以看到日志信息被输出到控制台了,内容如下:
Java代码
[main]2011-08-16 20:34:08,910 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:10)-This is info
[main]2011-08-16 20:34:08,913 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:11)-This is debug
[main]2011-08-16 20:34:08,913 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:12)-This is warn
[main]2011-08-16 20:34:08,913 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:13)-This is error
[main]2011-08-16 20:34:08,913 com.hanmiao.log.LoggerExample com.hanmiao.log.LoggerExample.main(LoggerExample.java:14)-This is fatal
我自己的样例代码附在本文后面,以方便日后参考。
另外,在写这篇文章时,我也重点参考了两篇文章如下:
2、log4j.xml配置方法详解:
阅读(833) | 评论(0) | 转发(0) |