1. 程式人生 > >Log4j深入研究 Log4j為單個類寫單個日誌檔案 Log4j為包寫檔案

Log4j深入研究 Log4j為單個類寫單個日誌檔案 Log4j為包寫檔案

 log4j配置檔案:

log4j.rootLogger=DEBUG,CONSOLE,FILE
#優先順序從高到低分別是ERROR、WARN、INFO、DEBUG
#這裡推薦選擇二種,分別為ERROR:僅僅只記錄錯誤名和位置;DEBUG:記錄所有錯誤的詳細資訊
#在生產配置下,請關閉應用於控制檯CONSOLE選項,僅啟用FILE及可
log4j.addivity.org.apache=true

# 應用於控制檯
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Encoding=GBK
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d(line-%L)- %m%n

#應用於檔案
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=D:\webGame\game.log
log4j.appender.FILE.MaxFileSize=1MB
log4j.appender.FILE.MaxBackupIndex=100
log4j.appender.FILE.Encoding=GBK
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d [%t] %-5p %c(line-%L) %-4r %x - %m%n

#這裡為子日誌檔案配置

log4j.logger.com.webgame.msg.manage.SendMsgStartup=DEBUG,CONSOLE1,FILE1

log4j.addivity.org.apache=false

#
log4j.additivity.com.webgame.msg.manage.SendMsgStartup=false 這裡如果設定為true,剛會把相關的日誌檔案,也寫入到主日誌檔案裡. log4j.additivity.com.webgame.msg.manage.SendMsgStartup=false # 應用於控制檯 log4j.appender.CONSOLE1=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE1.Threshold=DEBUG log4j.appender.CONSOLE1.Target=System.out log4j.appender.CONSOLE1.Encoding=GBK log4j.appender.CONSOLE1.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE1.layout.ConversionPattern=%d(line-%L)- %m%n #應用於檔案 log4j.appender.FILE1=org.apache.log4j.RollingFileAppender log4j.appender.FILE1.Threshold=DEBUG log4j.appender.FILE1.File=D:\webGame\blcs.msg.log log4j.appender.FILE1.MaxFileSize=10MB log4j.appender.FILE1.MaxBackupIndex=100 log4j.appender.FILE1.Encoding=GBK log4j.appender.FILE1.layout=org.apache.log4j.PatternLayout log4j.appender.FILE1.layout.ConversionPattern=%d [%t] %-5p %c(line-%L) %-4r %x - %m%n

 在紅色的地方,就是配置說明,相關的配置會單獨的寫到blcs.msg.log 裡.而不會寫入到主日誌檔案裡. 經過以上配置,SendMsgStartup 類裡的所有日誌資訊都會寫到一個檔案裡.

+++++++第一種++++++++++++++第一種++++++++++++++第一種++++++++++++++第一種+++++++

針對於第一種檔案的配置,很鬱悶的是,這個類裡所有的日誌都會寫到blcs.msg.log 裡.而實際上,可能一個些類我不想寫到一個檔案裡.於是我改了一下配置. +++++++第二種++++++++++++第二種++++++++++++第二種++++++++++++第二種+++++

package com.webgame.msg.manage;

import java.net.URL;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.webgame.test.TestLog4j;

public class SendMsgStartup extends HttpServlet {
private final Logger logger = Logger.getLogger(SendMsgStartup.class);

public void init(ServletConfig servletConfig) throws ServletException {
try {
super.init(servletConfig);
logger.info("第一個info測試,測試載入的哪個檔案");
String BLCX_XML_CONFIG_FILEPATH = TestLog4j.class.getResource("/")
+"xml/games/blcx.log4j.properties";//與第一種不同的地方
URL url = new URL(BLCX_XML_CONFIG_FILEPATH);
PropertyConfigurator.configure(url);
Logger logger1 = Logger.getLogger(SendMsgStartup.class);
for (int i = 0; i < 10; i++) {
logger1
.debug("debugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebug");
logger1
.error("errorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerror");
logger1
.info("infoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfo");
}
logger.info("第一個info測試結束,測試載入的哪個檔案");

} catch (Exception e) {
e.printStackTrace();
}
}

}

獨立出配置檔案:blcx.log4j.properties

#log4j.rootLogger=DEBUG,CONSOLE,FILE
#優先順序從高到低分別是ERROR、WARN、INFO、DEBUG
#這裡推薦選擇二種,分別為ERROR:僅僅只記錄錯誤名和位置;DEBUG:記錄所有錯誤的詳細資訊
#在生產配置下,請關閉應用於控制檯CONSOLE選項,僅啟用FILE及可
log4j.logger.com.webgame.msg.manage.SendMsgStartup=DEBUG,CONSOLE1,FILE1
log4j.addivity.org.apache=false
log4j.additivity.com.webgame.msg.manage.SendMsgStartup=false
# 應用於控制檯
log4j.appender.CONSOLE1=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE1.Threshold=DEBUG
log4j.appender.CONSOLE1.Target=System.out
log4j.appender.CONSOLE1.Encoding=GBK
log4j.appender.CONSOLE1.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE1.layout.ConversionPattern=%d(line-%L)- %m%n

#應用於檔案
log4j.appender.FILE1=org.apache.log4j.RollingFileAppender
log4j.appender.FILE1.Threshold=DEBUG
log4j.appender.FILE1.File=D:\webGame\blcs.msg.log
log4j.appender.FILE1.MaxFileSize=10MB
log4j.appender.FILE1.MaxBackupIndex=100
log4j.appender.FILE1.Encoding=GBK
log4j.appender.FILE1.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE1.layout.ConversionPattern=%d [%t] %-5p %c(line-%L) %-4r %x - %m%n

 +++++++第二種++++++++++++第二種++++++++++++第二種++++++++++++第二種+++++ 類檔案改成以下:

package com.webgame.msg.manage;

import java.net.URL;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.webgame.test.TestLog4j;

public class SendMsgStartup extends HttpServlet {
private final Logger logger = Logger.getLogger(SendMsgStartup.class);

public void init(ServletConfig servletConfig) throws ServletException {
try {
super.init(servletConfig);
logger.info("第一個info測試,測試載入的哪個檔案");
String BLCX_XML_CONFIG_FILEPATH = TestLog4j.class.getResource("/")
+"xml/games/blcx.log4j.properties";
URL url = new URL(BLCX_XML_CONFIG_FILEPATH);
PropertyConfigurator.configure(url);
Logger logger1 = Logger.getLogger(SendMsgStartup.class);
for (int i = 0; i < 10; i++) {
logger1
.debug("debugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebugdebug");
logger1
.error("errorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerrorerror");
logger1
.info("infoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfoinfo");
}
logger.info("第一個info測試結束,測試載入的哪個檔案");

} catch (Exception e) {
e.printStackTrace();
}
}

}

#給指定類設定等級和目的地

log4j.logger.包名.類名=debug,A1

#每天建立一個日誌檔案,格式為‘檔名-yyyy-MM-dd.log’

log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.file=c:/ccsLog
log4j.appender.A2.DatePattern='-'yyyy-MM-dd'.log'

#設定輸出格式

log4j.appender.A2.layout=org.apache.log4j.PatternLayout   
log4j.appender.A2.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss:SSS}][%C-%M] %m%n

private static Logger logger = Logger.getLogger(類名.class);//在指定類下建立logger