1、新建log4j.propperties,放在工程的src目錄下。html
#fileAppender log4j.rootCategory = DEBUG,file,consoleAppender log4j.appender.file = org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File = D:/logs/TestLog/testlog log4j.appender.file.DatePattern = ' _ ' yyyy - MM - dd ' .log ' log4j.appender.file.layout = org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %m%n log4j.appender.file.Threshold = INFO log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender log4j.appender.consoleAppender.Threshold = INFO log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout log4j.appender.consoleAppender.layout.ConversionPattern =%d %-5p %m %n log4j.appender.consoleAppender.ImmediateFlush = true
package test; import org.apache.log4j.Logger; public class testLog4j { private static Logger logger = Logger.getLogger(testLog4j.class); public static void main(String[] args) { logger.info("123"); } }
3、關於配置信息的解釋java
log4j 格式詳解數據庫
log4j.rootLogger=日誌級別,appender1, appender2, ….apache
log4j.appender.appender1=org.apache.log4j.日誌輸出到哪兒app
log4j.appender.appender1.File=文件目錄及文件佈局
${user.home}/logs/...性能
log4j.appender.appender1.MaxFileSize=最大文件大小測試
log4j.appender.appender1.MaxBackupIndex=備份文件個數spa
log4j.appender.ServerDailyRollingFile.DatePattern=日誌後綴格式線程
log4j.appender.appender1.layout=org.apache.log4j.日誌佈局格式
log4j.appender.appender1.layout.ConversionPattern=日誌輸出格式
log4j.appender.ServerDailyRollingFile.Append=true
總結一下: