Log4cpp的应用-----封装成单例模式

Log4cpp的应用-----封装成单例模式

MyLogger.h

#ifndef __MyLogger_H__
#define __MyLogger_H__

#include 
using namespace log4cpp;

class MyLogger
{
public:
    static MyLogger* getInstance();
    static void destroy();

    void warn(const char* msg);
    void error(const char* msg);
    void debug(const char* msg);
    void info(const char* msg);

    private:
    MyLogger();
    ~MyLogger();
    private:
    static MyLogger* _pInstance;
    Category& _mycat;
};

#define prefix(msg) (string(__FILE__) + string(":")\
                     +string(__FUNCTION__) + string(":")\
                     +string(std::to_string(__LINE__))\
                     +string(":") + msg).c_str()


#define logError(msg) MyLogger::getInstance()->error(prefix(msg))
#define logInfo(msg) MyLogger::getInstance()->info(prefix(msg))
#define logWarn(msg) MyLogger::getInstance()->warn(prefix(msg))
#define logDebug(msg) MyLogger::getInstance()->debug(prefix(msg))


#endif

MyLogger.cc

#include "MyLogger.h"
#include 
#include 
#include 
#include 
#include 
#include 

using std::cout;
using std::endl;

MyLogger* MyLogger::_pInstance = nullptr;

MyLogger* MyLogger::getInstance()
{
    if(_pInstance == nullptr)
    {
        _pInstance = new MyLogger();
    }
    return _pInstance;
}

void MyLogger::destroy()
{
    if(_pInstance)
    {
        delete _pInstance;
        _pInstance = nullptr;
    }
}

void MyLogger::warn(const char* msg)
{
    _mycat.warn(msg);
}

void MyLogger::error(const char* msg)
{
    _mycat.error(msg);
}

void MyLogger::debug(const char* msg)
{
    _mycat.debug(msg);
}

void MyLogger::info(const char* msg)
{
    _mycat.info(msg);
}
MyLogger::MyLogger()
:_mycat(Category::getRoot().getInstance("_mycat"))
{
    cout << "MyLogger()" << endl;
    //日志的格式
    PatternLayout* ptn1 = new PatternLayout();
    ptn1->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout* ptn2 = new PatternLayout();
    ptn2->setConversionPattern("%d %c [%p] %m%n");
    
    
    PatternLayout* ptn3 = new PatternLayout();
    ptn3->setConversionPattern("%d %c [%p] %m%n");

    //日志的目的地->目的地和日志布局绑定
    OstreamAppender* pos = new OstreamAppender("console",&cout);
    pos->setLayout(ptn1);

    FileAppender* fileApp = new FileAppender("fileApp","wd.txt");
    fileApp->setLayout(ptn2);
    
    RollingFileAppender* rollApp = new RollingFileAppender("rollApp","rolling.wd.log",10*1024,9);
    rollApp->setLayout(ptn3);

    //日志记录器和目的地对象绑定
    _mycat.addAppender(pos);
    _mycat.addAppender(fileApp);
    _mycat.addAppender(rollApp);

    //设置日志系统优先级
    _mycat.setPriority(Priority::DEBUG);
}

MyLogger::~MyLogger()
{
    cout << "~MyLogger()" << endl;
    Category::shutdown();
}

测试:testMylogger.cc

#include "MyLogger.h"
#include 
#include 

using std::cout;
using std::endl;
using std::string;

string func(const char* msg)
{
    string s1 = string(__FILE__) + string(":")
                +string(__FUNCTION__) + string(":")
                +string(std::to_string(__LINE__))
                +string(":") + msg;
    return s1;
}

void test()
{
    logInfo("this is a info");
    logError("this is a Error");
    logDebug("this is a degub");
    logWarn("this is a Warn");
}

int main()
{
    test();
    return 0;
}

运行结果:

2023-07-10 08:55:32,971 _mycat [INFO] testMyLogger.cc:test:20:this is a info
2023-07-10 08:55:32,973 _mycat [ERROR] testMyLogger.cc:test:21:this is a Error
2023-07-10 08:55:32,973 _mycat [DEBUG] testMyLogger.cc:test:22:this is a degub
2023-07-10 08:55:32,973 _mycat [WARN] testMyLogger.cc:test:23:this is a Warn

你可能感兴趣的:(c++,单例模式,c++)