C++开源跨平台OJ系统判题核心FreeJudger(三)——log4cxx同时使用多个日志文件

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

主要维护人:

你可能感兴趣的:(C++开源跨平台OJ系统判题核心FreeJudger(三)——log4cxx同时使用多个日志文件)