log4cplus日志代码示例

log4cplus c++的一个开源的日志工具。

1 日志配置:设置日志级别,名称,目录,更新方式,输出格式

log.properties

log4cplus.rootLogger=DEBUG, R
log4cplus.appender.R=log4cplus::RollingFileAppender
log4cplus.appender.R.File=../log/Server.log
log4cplus.appender.R.Append=true
log4cplus.appender.R.layout=log4cplus::PatternLayout
log4cplus.appender.R.layout.ConversionPattern=%D{%y-%m-%d %H:%M:%S} %l |%p| %m%n

2 代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace log4cplus;
using namespace log4cplus::helpers;

int main()
{

        PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("log.properties"));
        Logger logger = Logger::getRoot();

        LOG4CPLUS_DEBUG(logger, "this is debug log: " << "random integer: "<< random());
        LOG4CPLUS_INFO(logger, "the information centry...." << "[ 1 + 1 = " << 1 + 1 << "]");
        LOG4CPLUS_WARN(logger, "Writing warning messages to log....");
        LOG4CPLUS_ERROR(logger, "ooooooh, there is an error....");
        return 0;
}
编译 :g++ -llog4cplus tlog.cpp -o a      
执行./a
more Server.log  结果:
18-02-28 17:49:47 tlog.cpp:19 |DEBUG| this is debug log: random integer: 1804289383
18-02-28 17:49:47 tlog.cpp:20 |INFO| the information centry....[ 1 + 1 = 2]
18-02-28 17:49:47 tlog.cpp:21 |WARN| Writing warning messages to log....
18-02-28 17:49:47 tlog.cpp:22 |ERROR| ooooooh, there is an error....

 
  


你可能感兴趣的:(开源)