//1.導入jar包,log4j-1.2.17.jar//2.src下創建log4j.properties文件//3.配置properties文件/*log4j.rootLogger(預設是對整個工程生效)=DEBUG,stdout(控制台),a, b, …日誌級別(ALL<DEBUG<INFO<W ...
//1.導入jar包,log4j-1.2.17.jar
//2.src下創建log4j.properties文件
//3.配置properties文件
/*
log4j.rootLogger(預設是對整個工程生效)=DEBUG,stdout(控制台),a, b, …
日誌級別(ALL<DEBUG<INFO<WARN<ERROR<FATAL<OFF)
log4j.logger.com.hutu=info, stdout,表示該日誌只對package com.hutu生效
log4j.appender.a=org.apache.log4j.日誌輸出到哪兒
ConsoleAppender(控制台)
FileAppender(文件)
DailyRollingFileAppender(每天產生一個日誌文件)
RollingFileAppender(文件大小到達指定尺寸時產生一個新的文件)
WriteAppender(將日誌信息以流格式發送到任意指定的地方)
JDBCAppender(將日誌信息保存到資料庫中)
log4j.appender.a.File=文件目錄及文件路徑${user.home}/logs/...
log4j.appender.a.MaxFileSize=最大文件大小100m
log4j.appender.a.MaxBackupIndex=備份文件個數10
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd 日誌尾碼格式
log4j.appender.a.layout=org.apache.log4j.日誌佈局格式
HTMLLayout(以HTML表格形式佈局)
SimpleLayout(包含日誌信息的級別和信息字元串)
TTCCLayout(包含日誌產生的時間,執行緒,類別等信息)
PatternLayout(可以靈活的指定佈局格式,常用)
log4j.appender.a.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c] %m%n日誌輸出格式
log4j.appender.ServerDailyRollingFile.Append=true
#Log4j配置文件實現了輸出到控制台、文件、回滾文件、發送日誌郵件、輸出到資料庫日誌表、自定義標簽等功能直接拷貝。
log4j.rootLogger=DEBUG,console,logFile,rollingFile,dailyFile,socket.......
log4j.additivity.org.apache=true
# 控制台(console)
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.ImmediateFlush=true
log4j.appender.console.Target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
# 日誌文件(logFile)
log4j.appender.logFile=org.apache.log4j.FileAppender
log4j.appender.logFile.Threshold=DEBUG
log4j.appender.logFile.ImmediateFlush=true
log4j.appender.logFile.Append=true
log4j.appender.logFile.File=D:/logs/log.log4j
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
# 回滾文件(rollingFile)
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.Threshold=DEBUG
log4j.appender.rollingFile.ImmediateFlush=true
log4j.appender.rollingFile.Append=true
log4j.appender.rollingFile.File=D:/logs/log.log4j
log4j.appender.rollingFile.MaxFileSize=200KB
log4j.appender.rollingFile.MaxBackupIndex=50
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
# 定期回滾日誌文件(dailyFile)
log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyFile.Threshold=DEBUG
log4j.appender.dailyFile.ImmediateFlush=true
log4j.appender.dailyFile.Append=true
log4j.appender.dailyFile.File=D:/logs/log.log4j
log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd
log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyFile.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
# 應用於socket
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 Factor 5
log4j.appender.socket.layout=org.apache.log4j.PatternLayout
log4j.appender.socket.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
# Log Factor 5 Appender
log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender
log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
# 發送日誌到指定郵件
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.Threshold=FATAL
log4j.appender.mail.BufferSize=10
log4j.appender.mail.From = [email protected]
log4j.appender.mail.SMTPHost=mail.com
log4j.appender.mail.Subject=Log4J Message
log4j.appender.mail.To= [email protected]
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
# 應用於資料庫
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=
log4j.appender.database.sql=INSERT INTO LOG4J (Message) VALUES('=[%-5p] %d(%r) --> [%t] %l: %m %x %n')
log4j.appender.database.layout=org.apache.log4j.PatternLayout
log4j.appender.database.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
# 自定義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 = [email protected]
log4j.appender.im.layout=org.apache.log4j.PatternLayout
log4j.appender.im.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
//4.java類中
private static Logger logger = Logger.getLogger(Log.class.getName());
PropertyConfigurator.configure("src//log4j.properties");//src下可以省略