日志记录工具:log4cpp,使用流程

本文介绍了log4cpp的主要工具类,包括Category、Appender、Layout和Priority,详细阐述了日志记录的使用流程,并提出了通过单例模式进行简易封装的方法。文章还指出了改进方向,如线程安全、参数防呆处理和函数返回值判断等。

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

一、主要工具类

  • 日志系统,就是将用户指定的信息,按照一定的格式,存储到一个指定的区域。
  • 根据面向对象的思想,可以抽象出一个执行日志记录的主体Category,该主体用于执行日志的开始,结束以及记录。
  • 而如何执行日志记录、这些功能也可以单独抽象出一个类,作为Category的一种属性。

log4cpp的开发主要使用以下四个工具类

  1. Category类。 执行日志记录任务的主体,其构造函数是protect类型,默认有一个root的Category实例,其他的Category实例均从root衍生出来。使用log4cpp必须有一个Category实例。

  2. Appender类。Appender类用于描述日志如何输出,例如使用什么格式输出以及日志输出到哪里。其中使用什么格式输出,是通过Layout类来进行实现的。Appender是一个基类,常用的Appender有FileAppender、OstreamAppender以及RollingFileAppender。

  3. Layout类。用于描述日志输出的格式。

  4. Priority。用于过滤日志信息。Priority类根据设置的优先级的不同,会过滤掉相应的优先级低于(数值越低优先级越高)当前Category实例设置成的优先级的记录。
    在这里插入图片描述

二、使用流程

  1. 由于log4cpp是以Category类为基础进行日志操作。所以可以先定义一个Category类的变量。

  2. 如果需要将Category实例的属性补充完整,需要使用Appender类,所以第二步可以定义一个Appender类。

  3. Appender类需要使用描述输出的格式,因此需要定义一个Layout类来设置输出格式

  4. Layout类设定完毕后->完善Appender类->完善Category类

  5. 设定Category实例的优先级

  6. 利用Category实例进行日志记录、

  7. 使用Category::shutdown()来结束日志记录,销毁各个对象。

三、使用单例模式进行简易封装

  • 1.将日志消息记录到标准输出cout 以及回滚文件中
  • 2.实例创建成功后只需要使用相应的函数进行打印结果即可,
  • 3.默认格式会打印所在文件,行号,函数,以及消息信息
//使用的头文件
#include <iostream>
#include <string>
#include <sstream>

#include <log4cpp/Category.hh>

#include <log4cpp/Appender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/RollingFileAppender.hh>

#include <log4cpp/Layout.hh>
#include <log4cpp/PatternLayout.hh>

using namespace log4cpp;
using namespace std;

//类结构的声明
class MLog{
public:
    static MLog* logCntl(const bool flag,const string& FileName,
                         const size_t& fileSize = 10*1024*1024,const int& RollingFileNum = 10,
                         const int& PRIORITY = Priority::DEBUG);

    int getPriority()const;
    void setpriority(int PRIORITY);

    void logInfo(const string& msg)const;
    void logError(const string& msg)const;
    void logWarn(const string& msg)const;
    void logDebug(const string& msg)const;

private:
    Category& root = Category::getRoot();
    MLog(const string& FileName,const int& RollingFileNum,const size_t& fileSize,const int& PRIORITY);
    ~MLog();

};

//成员函数和构造函数的实现
MLog::MLog(const string& FileName,const int& RollingFileNum,const size_t& fileSize,const int& PRIORITY){
    //1.设置输出器
    RollingFileAppender* term1 = new RollingFileAppender("term1",FileName,fileSize,
                                                         RollingFileNum,true,00644);
    OstreamAppender* term2 =  new OstreamAppender("term2",&cout);

    //2.设置布局器
    PatternLayout* layout1 = new PatternLayout();
    PatternLayout* layout2 = new PatternLayout();

    layout1->setConversionPattern("%d [%p] %m %n");
    layout2->setConversionPattern("%d [%p] %m %n");

    //3.添加布局
    term1->setLayout(layout1);
    term2->setLayout(layout2);

    //4.将输出器添加到记录器中
    root.addAppender(term1);
    root.addAppender(term2);

    //5.设置优先级
    root.setPriority(PRIORITY);

    printf("done setting root \n");

}

MLog:: ~MLog(){
    Category::shutdown();
    printf("destructor\n");
}

MLog*  MLog:: logCntl(const bool flag,const string& FileName,
                      const size_t& fileSize ,const int& RollingFileNum ,
                      const int& PRIORITY){

    static MLog* MLogInstance = nullptr;
    if(1 == flag){
        if(nullptr == MLogInstance){
            MLogInstance = new MLog(FileName,RollingFileNum,fileSize,PRIORITY);
            printf("done creating new Mlog\n");
        }else{
            printf("instance Already exist\n");
        }    

    }else{
        if(MLogInstance==nullptr){
            printf("intance already deleted\n");
        }else{
            delete MLogInstance;
        }

    }

    return MLogInstance;
}



void MLog:: logInfo(const string& msg)const{
    ostringstream strstream;
    strstream << __FILE__ <<" : "<<__LINE__<<" : "<<__FUNCTION__<<" : "<<msg<<endl;
    root.info(strstream.str());    
    /* cout << strstream.str()<<endl; */
}
void MLog:: logError(const string& msg)const{
    ostringstream strstream;
    strstream << __FILE__ <<" : "<<__LINE__<<" : "<<__FUNCTION__<<" : "<<msg<<endl;
    root.error(strstream.str());
    /* cout << strstream.str()<<endl; */

}
void MLog:: logWarn(const string& msg)const{
    ostringstream strstream;
    strstream << __FILE__ <<" : "<<__LINE__<<" : "<<__FUNCTION__<<" : "<<msg<<endl;
    root.warn(strstream.str());
    /* cout << strstream.str()<<endl; */
}
void MLog:: logDebug(const string& msg)const{
    ostringstream strstream;
    strstream << __FILE__ <<" : "<<__LINE__<<" : "<<__FUNCTION__<<" : "<<msg<<endl;
    root.debug(strstream.str());
    /* cout << strstream.str()<<endl; */
}

int  MLog::getPriority()const{
    return root.getPriority();
}
void MLog::setpriority(int PRIORITY){
    root.setPriority(PRIORITY);
}


//测试函数
int main()
{
    //1.创建日志实例
    MLog* mlog = MLog::logCntl(1,"mLog_log",20*1024,5,Priority::DEBUG);

    //2.循环打印日志
    for(int index = 0;index<300;++index){
        mlog->logDebug("this is a Debug msg"); 
        mlog->logInfo("this is an Info msg"); 
        mlog->logError("this is an Error msg"); 
        mlog->logWarn("this is a Warn msg"); 
    }

    //3.关闭日志实例
    MLog::logCntl(0," ");


    return 0;
}

四、改进方向

  1. 单例模式未修改为线程安全。
  2. 构造函数的输入参数没有做防呆处理。
  3. 调用log4cpp的函数,其返回值未做处理判断。
  4. 其他优先级日志记录函数的添加。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值