log4cpp输出为html文件,log4cpp:简单输出日志示例

一、log4cpp概念

Category:  写日志。

Appender: 指定日志的目的地。

Layout:      设定日志的格式。

Priority: 指定Category的优先级和日志的优先级。

NDC:    嵌套的上下文诊断,可用于多线程、多场景的跟踪调试。

优先级:NOTSET < DEBUG < INFO < NOTICE < WARN < ERROR < CRIT < ALERT < FATAL = EMERG

当日志优先级高于Category的优先级时,该日志被记录,否则被忽略。

二、示例代码(Windows/Linux)

2.1 代码

#include

using namespace std;

#include

#include

#include

#include

#include

#include

int main()

{

//Step 1 实例化Layout对象、Appender对象、Category对象

log4cpp::Layout *layout = new log4cpp::BasicLayout;

log4cpp::Appender *appender = new log4cpp::OstreamAppender("default", &std::cout);

//log4cpp::Appender *appender = new log4cpp::FileAppender("File", "test.txt");

log4cpp::Category &log = log4cpp::Category::getInstance("test");

//Step 2 把Layout对象附着到Appender对象上,把Appender对象附着到Category对象上

appender->setLayout(layout);

log.addAppender(appender);

//Step 3 设置Category的优先级

log.setPriority(log4cpp::Priority::DEBUG);

log.alert("DEBUG emerg.");

log.log(log4cpp::Priority::ALERT, "DEBUG alert.");

log.error("DEBUG error.");

log.info("DEBUG warn.");

log.log(log4cpp::Priority::INFO, "DEBUG info.");

log.debug("DEBUG debug.");

//

log.setPriority(log4cpp::Priority::ERROR);

log.alert("ERROR emerg.");

log.log(log4cpp::Priority::ALERT, "ERROR alert.");

log.error("ERROR error.");

log.info("ERROR warn.");

log.log(log4cpp::Priority::INFO, "ERROR info.");

log.debug("ERROR debug.");

log4cpp::Category::shutdown();

return 0;

}

2.2 输出结果

windows下:

0818b9ca8b590ca3270a3433284dd417.png

Linux下:

0818b9ca8b590ca3270a3433284dd417.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值