使用多个logger时,所有logger的配置写在一个配置文件里面
两个例子:
1 一个继承的例子(http://logging.apache.org/log4cxx/)
// file com/foo/bar.h
#include "log4cxx/logger.h" namespace com {
namespace foo {
class Bar {
static log4cxx::LoggerPtr logger; public:
void doIt();
}
}
}
// file bar.cpp
#include "com/foo/bar.h" using namespace com::foo;
using namespace log4cxx; LoggerPtr Bar::logger(Logger::getLogger("com.foo.bar")); void Bar::doIt() {
LOG4CXX_DEBUG(logger, "Did it again!")
}
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout # Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n # Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
// file MyApp2.cpp #include "com/foo/bar.h"
using namespace com::foo; // include log4cxx header files.
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/propertyconfigurator.h"
#include "log4cxx/helpers/exception.h" using namespace log4cxx;
using namespace log4cxx::helpers;
// Define a static logger variable so that it references the
// Logger instance named "MyApp".
LoggerPtr logger(Logger::getLogger("MyApp")); int main(int argc, char **argv)
{
int result = EXIT_SUCCESS;
try
{
if (argc > 1)
{
// BasicConfigurator replaced with PropertyConfigurator.
PropertyConfigurator::configure(argv[1]);
}
else
{
BasicConfigurator::configure();
} LOG4CXX_INFO(logger, "Entering application.")
Bar bar
bar.doIt();
LOG4CXX_INFO(logger, "Exiting application.")
}
catch(Exception&)
{
result = EXIT_FAILURE;
} return result;
}
The output of MyApp configured with this file is shown below.
2000-09-07 14:07:41,508 [12345] INFO MyApp - Entering application.
2000-09-07 14:07:41,529 [12345] INFO MyApp - Exiting application.
2 使用多个logger
前置知识
- log4cxx可以使用配置文件进行设置,并且其设置方式与log4j兼容;
- log4cxx库对日志流设置,只需更改配置文件中的appender属性;
- log4cxx支持Logger继承机制,默认会继承父Logger的appender,由于要将日志流输出到多个文件中,因此需要将子Logger的继承属性设置为false.
配置子Logger
为了最大程度的灵活性,log4cxx引入了子Logger这个概念,即拥有与父Logger不同的行为,和编程语言中的OOP概念一致。
下面是在配置文件中定义一个子Logger的代码,其中ap0是logger0使用的appender:
- log4j.logger.logger0 = TRACE, ap0
设置子Logger不继承父Logger的appender:
- 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