C++開源跨平臺OJ系統判題核心FreeJudger(三)——log4cxx同時使用多個日誌檔案

凝霜發表於2013-02-08

C++開源跨平臺OJ系統判題核心FreeJudger(三)——log4cxx同時使用多個日誌檔案

By 馬冬亮(凝霜  Loki)

一個人的戰爭(http://blog.csdn.net/MDL13412)

前置知識

  • log4cxx可以使用配置檔案進行設定,並且其設定方式與log4j相容
  • log4cxx庫對日誌流設定,只需更改配置檔案中的appender屬性;
  • log4cxx支援Logger繼承機制,預設會繼承父Loggerappender,由於要將日誌流輸出到多個檔案中,因此需要將子Logger的繼承屬性設定為false.

配置子Logger

為了最大程度的靈活性,log4cxx引入了子Logger這個概念,即擁有與父Logger不同的行為,和程式語言中的OOP概念一致。

下面是在配置檔案中定義一個子Logger的程式碼,其中ap0logger0使用的appender

log4j.logger.logger0 = TRACE, ap0

設定子Logger不繼承父Loggerappender

log4j.additivity.logger0=false
設定子Logger所使用的日誌檔案,下述程式碼將logger0的日誌檔案設定為當前程式目錄下的0.log

log4j.appender.ap0.File=./0.log

在程式中獲取子Logger例項

首先是載入配置檔案:

log4cxx::PropertyConfigurator::configure(配置檔名稱);
獲取子Logger例項:

log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(子Logger名[例如上文中的"logger0"]);

FreeJudger專案中使用的完整配置檔案

下述程式碼配置了8個子Logger,用於FreeJudger專案中判題執行緒的日誌:

log4j.additivity.gather = false
log4j.rootLogger=TRACE

log4j.logger.logger0 = TRACE, ap0
log4j.logger.logger1 = TRACE, ap1
log4j.logger.logger2 = TRACE, ap2
log4j.logger.logger3 = TRACE, ap3
log4j.logger.logger4 = TRACE, ap4
log4j.logger.logger5 = TRACE, ap5
log4j.logger.logger6 = TRACE, ap6
log4j.logger.logger7 = TRACE, ap7
log4j.logger.logger8 = TRACE, ap8

log4j.additivity.logger0=false
log4j.additivity.logger1=false
log4j.additivity.logger2=false
log4j.additivity.logger3=false
log4j.additivity.logger4=false
log4j.additivity.logger5=false
log4j.additivity.logger6=false
log4j.additivity.logger7=false
log4j.additivity.logger8=false

log4j.appender.logfile.encoding=UTF-8

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R.MaxFileSize=100KB

log4j.appender.ap0=org.apache.log4j.RollingFileAppender
log4j.appender.ap0.ImmediateFlush=true 
log4j.appender.ap0.File=./0.log
log4j.appender.ap0.MaxBackupIndex=10
log4j.appender.ap0.layout=org.apache.log4j.PatternLayout
log4j.appender.ap0.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n

log4j.appender.ap1=org.apache.log4j.RollingFileAppender
log4j.appender.ap1.ImmediateFlush=true 
log4j.appender.ap1.File=./1.log
log4j.appender.ap1.MaxBackupIndex=10
log4j.appender.ap1.layout=org.apache.log4j.PatternLayout
log4j.appender.ap1.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n

log4j.appender.ap2=org.apache.log4j.RollingFileAppender
log4j.appender.ap2.ImmediateFlush=true 
log4j.appender.ap2.File=./2.log
log4j.appender.ap2.MaxBackupIndex=10
log4j.appender.ap2.layout=org.apache.log4j.PatternLayout
log4j.appender.ap2.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n

log4j.appender.ap3=org.apache.log4j.RollingFileAppender
log4j.appender.ap3.ImmediateFlush=true 
log4j.appender.ap3.File=./3.log
log4j.appender.ap3.MaxBackupIndex=10
log4j.appender.ap3.layout=org.apache.log4j.PatternLayout
log4j.appender.ap3.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n

log4j.appender.ap4=org.apache.log4j.RollingFileAppender
log4j.appender.ap4.ImmediateFlush=true 
log4j.appender.ap4.File=./4.log
log4j.appender.ap4.MaxBackupIndex=10
log4j.appender.ap4.layout=org.apache.log4j.PatternLayout
log4j.appender.ap4.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n

log4j.appender.ap5=org.apache.log4j.RollingFileAppender
log4j.appender.ap5.ImmediateFlush=true 
log4j.appender.ap5.File=./5.log
log4j.appender.ap5.MaxBackupIndex=10
log4j.appender.ap5.layout=org.apache.log4j.PatternLayout
log4j.appender.ap5.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n

log4j.appender.ap6=org.apache.log4j.RollingFileAppender
log4j.appender.ap6.ImmediateFlush=true 
log4j.appender.ap6.File=./6.log
log4j.appender.ap6.MaxBackupIndex=10
log4j.appender.ap6.layout=org.apache.log4j.PatternLayout
log4j.appender.ap6.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n

log4j.appender.ap7=org.apache.log4j.RollingFileAppender
log4j.appender.ap7.ImmediateFlush=true 
log4j.appender.ap7.File=./7.log
log4j.appender.ap7.MaxBackupIndex=10
log4j.appender.ap7.layout=org.apache.log4j.PatternLayout
log4j.appender.ap7.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n

log4j.appender.ap8=org.apache.log4j.RollingFileAppender
log4j.appender.ap8.ImmediateFlush=true 
log4j.appender.ap8.File=./8.log
log4j.appender.ap8.MaxBackupIndex=10
log4j.appender.ap8.layout=org.apache.log4j.PatternLayout
log4j.appender.ap8.layout.ConversionPattern=%5p %d{HH:mm:ss} -- %m%n

歡迎加入

群117975329,驗證資訊CSDN

主要維護人:

  • 周寶      you_lan_hai@foxmail.com
  • 馬冬亮  mdl2009@vip.qq.com

相關文章