log4cplus日志代码示例

本文介绍了一个用于C++的开源日志工具log4cplus的基本配置方法及示例代码,展示了如何通过log4cplus进行不同级别的日志记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <cstdlib>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/helpers/stringhelper.h>
#include <log4cplus/loggingmacros.h>
#include <iomanip>

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....



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值