Log4cxx is a good library, using the configuration file can be very flexible to use it. To be specific, first of all, of course, in your C + + code, write a log of the various things
#include <log4cxx/logger.h>
#include <log4cxx/logstring.h>
#include <log4cxx/propertyconfigurator.h>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace log4cxx;
// 读取配置文件
PropertyConfigurator::configure("log4cxx.cfg");
// 建立两个logger
LoggerPtr logger1 = Logger::getLogger("TraceYourMama");
LoggerPtr logger2 = Logger::getLogger("Patch");
LOG4CXX_TRACE(logger1, "跟踪");
LOG4CXX_WARN(logger1, "警告");
LOG4CXX_DEBUG(logger1, "调试");
LOG4CXX_ASSERT(logger1, false, "断言");
LOG4CXX_FATAL(logger1, "致命");
LOG4CXX_TRACE(logger2, "跟踪");
LOG4CXX_ERROR(logger2, "错误");
return 0;
}
In fact, in practical applications, each class can get a logger, and then in the configuration file to do the following settings:
# Set root logger to debug level
#log4j. ROOTLOGGER=TRACE,CA
Log4j.logger.traceyourmama=error,fa,ha
Log4j.logger.patch=trace,ca
#设置spirit为TRACE级别
#log4j. Spirit=debug
#log4j. Additivity.spirit=false
#%m-message
#%n-Return
#%d-time
#%.16c-logger name
#%-5p-log level
#%t-thread_id
#对Appender CA settings:
#这是一个控制台类型的Appender
#输出格式 (layout) to Patternlayout
Log4j.appender.ca= Org.apache.log4j.ConsoleAppender
Log4j.appender.ca.layout=org.apache.log4j.patternlayout
log4j.appender.ca.layout.conversionpattern=%d%-5p%.16c-%m%n
#对Appender fa set:
# This is a file type of Appender ,
# Its output file (file) is./debug.log,
# Output mode (Append) is overridden,
# output format (layout) is Patternlayout
Log4j.appender.fa= Org.apache.log4j.FileAppender
Log4j.appender.fa.file=./debug.log
Log4j.appender.fa.append=true
Log4j.appender.fa.layout=org.apache.log4j.patternlayout
#log4j. appender.fa.layout.conversionpattern=%d[%t]%-5p%.16c%x-%m%n
log4j.appender.fa.layout.conversionpattern=%d%-5p%.16c-%m%n