標籤:max 報錯資訊 tle pattern 產生 fat div dai index
Log4j官方的appender給出了一下幾種實現
org.apache.log4j.ConsoleAppender(控制台),
org.apache.log4j.FileAppender(檔案),
org.apache.log4j.DailyRollingFileAppender(每天產生一個記錄檔),
org.apache.log4j.RollingFileAppender(檔案大小到達指定尺寸的時候產生一個新的檔案),
org.apache.log4j.WriterAppender(將日誌資訊以流格式發送到任意指定的地方)
實際開發我們使用第1,第3和第4種實現;
假如日誌資料量不是很大,我們可以用DailyRollingFileAppender 每天產生一個日誌,方便查看;
假如日誌資料量很大,我們一般用RollingFileAppender,固定尺寸的日誌,假如超過了 就產生一個新的檔案;
我們這裡給出一些執行個體;
log4j.rootLogger=DEBUG, Console ,File ,DailyRollingFile ,RollingFile #Console log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n #Filelog4j.appender.File = org.apache.log4j.FileAppenderlog4j.appender.File.File = C://log2.loglog4j.appender.File.layout = org.apache.log4j.PatternLayoutlog4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n #DailyRollingFilelog4j.appender.DailyRollingFile = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.DailyRollingFile.File = C://log3.loglog4j.appender.DailyRollingFile.layout = org.apache.log4j.PatternLayoutlog4j.appender.DailyRollingFile.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n #RollingFilelog4j.appender.RollingFile = org.apache.log4j.RollingFileAppenderlog4j.appender.RollingFile.File = C://log4.loglog4j.appender.RollingFile.MaxFileSize=1KBlog4j.appender.RollingFile.MaxBackupIndex=3log4j.appender.RollingFile.layout = org.apache.log4j.PatternLayoutlog4j.appender.RollingFile.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
測試代碼:
package com.open1111; import org.apache.log4j.Logger; public class Test { private static Logger logger=Logger.getLogger(Test.class); // 擷取logger執行個體 public static void main(String[] args) { logger.info("普通Info資訊"); logger.debug("調試debug資訊"); logger.error("報錯error資訊"); logger.warn("警告warn資訊"); logger.fatal("嚴重錯誤fatal資訊"); logger.error("報錯資訊", new IllegalArgumentException("非法參數")); int i=0; while(i<10000){ logger.debug(" RollingFile 調試debug資訊"); logger.debug(" RollingFile 調試debug資訊"); logger.debug(" RollingFile 調試debug資訊"); logger.debug(" RollingFile 調試debug資訊"); logger.debug(" RollingFile 調試debug資訊"); i++; } } }
這裡有兩個新的配置項解釋下:
MaxFileSize 是記錄檔的最大尺寸;根據實際需求來定 10KB 100KB也行
MaxBackupIndex是記錄檔的個數,假如超過了,則覆蓋,主要考慮到的是硬碟的容量問題;根據實際需求來定 比如 100 500;
這裡給下記錄檔的效果:
DailyRollingFileAppender 效果:
RollingFileAppender 效果:
Log4j appender輸出類型配置