C++ Log4Cpp 使用

log4cpp当前提供以下appender:

log4cpp::IdsaAppender // 发送到IDS或者logger, 详细见 http://jade.cs.uct.ac.za/idsa/

log4cpp::FileAppender // 输出到文件

log4cpp::RollingFileAppender // 输出到回卷文件,即当文件到达某个大小后回卷

log4cpp::OstreamAppender // 输出到一个ostream类

log4cpp::RemoteSyslogAppender // 输出到远程syslog服务器

log4cpp::StringQueueAppender // 内存队列

log4cpp::SyslogAppender // 本地syslog

log4cpp::Win32DebugAppender // 发送到缺省系统调试器

log4cpp::NTEventLogAppender // 发送到win 事件日志

NOTSET < DEBUG < INFO < NOTICE < WARN < ERROR < CRIT < ALERT < FATAL = EMERG

 

2. 手动使用步骤

#include "log4cpp/Category.hh"

#include "log4cpp/FileAppender.hh"

#include "log4cpp/BasicLayout.hh"

int main(int argc, char* argv[])

{

    // 1实例化一个layout 对象

    log4cpp::Layout* layout = 

    new log4cpp::BasicLayout();

    // 2. 初始化一个appender 对象

    log4cpp::Appender* appender = new log4cpp::FileAppender("FileAppender", "./test_log4cpp1.log");

    // 3. 把layout对象附着在appender对象上

    appender->setLayout(layout);

    // 4. 实例化一个category对象

    log4cpp::Category& warn_log = log4cpp::Category::getInstance("mywarn");

    // 5. 设置additivity为false,替换已有的appender

    warn_log.setAdditivity(false);

    // 5. 把appender对象附到category上

    warn_log.setAppender(appender);

    // 6. 设置category的优先级,低于此优先级的日志不被记录

    warn_log.setPriority(log4cpp::Priority::WARN);

    // 记录一些日志

    warn_log.info("Program info which cannot be wirten");

    warn_log.debug("This debug message will fail to write");

    warn_log.alert("Alert info");

    // 其他记录日志方式

    warn_log.log(log4cpp::Priority::WARN, "This will be a logged warning");

    log4cpp::Priority::PriorityLevel priority;

    bool this_is_critical = true;

    if(this_is_critical)

        priority = log4cpp::Priority::CRIT;

    else

        priority = log4cpp::Priority::DEBUG;

    warn_log.log(priority,"Importance depends on context");

     

    warn_log.critStream() << "This will show up << as " 

    << 1 << " critical message" 

    << log4cpp::CategoryStream::ENDLINE;

    // clean up and flush all appenders

    log4cpp::Category::shutdown();

    return 0;

}

3. 配置文件驱动方式使用步骤

Log4cpp主要提供了 log4cpp::PropertyConfigurator 和log4cpp::SimpleConfigurator两种机制(文件格式)

# a simple test config

#定义了3个category sub1, sub2, sub1.sub2

log4j.rootCategory=DEBUG, rootAppender

log4j.category.sub1=,A1

log4j.category.sub2=INFO

log4j.category.sub1.sub2=ERROR, A2

# 设置sub1.sub2 的additivity属性

log4j.additivity.sub1.sub2=false

#定义rootAppender类型和layout属性

log4j.appender.rootAppender=org.apache.log4j.ConsoleAppender

log4j.appender.rootAppender.layout=org.apache.log4j.BasicLayout

#定义A1的属性

log4j.appender.A1=org.apache.log4j.FileAppender

log4j.appender.A1.fileName=A1.log

log4j.appender.A1.layout=org.apache.log4j.SimpleLayout

#定义A2的属性

log4j.appender.A2=org.apache.log4j.ConsoleAppender

log4j.appender.A2.layout=org.apache.log4j.PatternLayout

log4j.appender.A2.layout.ConversionPattern=The message '%m' at time %d%n


 


#include "log4cpp/Category.hh"

#include "log4cpp/PropertyConfigurator.hh"

int main(int argc, char* argv[])

{

    // 1 读取解析配置文件

    // 读取出错, 完全可以忽略,可以定义一个缺省策略或者使用系统缺省策略

    // BasicLayout输出所有优先级日志到ConsoleAppender

    try { 

        log4cpp::PropertyConfigurator::configure("./log4cpp.conf");

    } catch(log4cpp::ConfigureFailure& f) {

        std::cout << "Configure Problem " << f.what() << std::endl;

        return -1;

    }

     

    // 2 实例化category对象

    // 这些对象即使配置文件没有定义也可以使用,不过其属性继承其父category

    // 通常使用引用可能不太方便,可以使用指针,以后做指针使用

    // log4cpp::Category* root = &log4cpp::Category::getRoot();

    log4cpp::Category& root = log4cpp::Category::getRoot();

     

    log4cpp::Category& sub1 = 

        log4cpp::Category::getInstance(std::string("sub1"));

    log4cpp::Category& sub3 = 

        log4cpp::Category::getInstance(std::string("sub1.sub2"));

    // 3 正常使用这些category对象进行日志处理。

    // sub1 has appender A1 and rootappender.

    sub1.info("This is some info");

    sub1.alert("A warning");

     

    // sub3 only have A2 appender.

    sub3.debug("This debug message will fail to write");

    sub3.alert("All hands abandon ship");

    sub3.critStream() << "This will show up << as " << 1 << " critical message" 

    << log4cpp::CategoryStream::ENDLINE;

    sub3 << log4cpp::Priority::ERROR 

              << "And this will be an error"  

              << log4cpp::CategoryStream::ENDLINE;

    sub3.log(log4cpp::Priority::WARN, "This will be a logged warning");

     

    return 0;

}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星点点-

请我喝杯咖啡呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值