log4cplus简单封装

#ifndef log_Logger4_H
#define log_Logger4_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//## class Logger4
class Logger4 {
public :

    enum Logger4Type {
        Cout = 0,
        File,
    };

    enum Logger4Level {
        None = -1,
        Off = 0,
        Crit = 1,
        Err = 2,
        Warning = 3,
        Info = 4,
        Debug = 5,
        Trace = 6,
    };


        Constructors and destructors    

    Logger4();

    ~Logger4();

        Operations    

    static Logger4 &Instance();

    //## operation logInit()
    void logInit(std::string fileName, Logger4Type type, Logger4Level level, const char *MaxFileSize,
                 const char *MaxBackupIndex);

    log4cplus::Logger _logger;

    log4cplus::Initializer _initializer;
};

#define S_LOG_TRACE(logEvent) LOG4CPLUS_TRACE(Logger4::Instance()._logger, logEvent)
#define S_LOG_DEBUG(logEvent) LOG4CPLUS_DEBUG(Logger4::Instance()._logger, logEvent)
#define S_LOG_INFO(logEvent)  LOG4CPLUS_INFO(Logger4::Instance()._logger, logEvent)
#define S_LOG_WARN(logEvent)  LOG4CPLUS_WARN(Logger4::Instance()._logger, logEvent)
#define S_LOG_ERROR(logEvent) LOG4CPLUS_ERROR(Logger4::Instance()._logger, logEvent)
#define S_LOG_FATAL(logEvent) LOG4CPLUS_FATAL(Logger4::Instance()._logger, logEvent)

#endif

#include "Logger4.h"

//## class Logger4

Logger4::Logger4() {
}

Logger4::~Logger4() {
    _logger.shutdown();
}

Logger4 &Logger4::Instance() {
    static Logger4 log;
    return log;
}

void Logger4::logInit(std::string fileName, Logger4Type type, Logger4Level level, const char *MaxFileSize,
                      const char *MaxBackupIndex) {
    //#[ operation logInit()
#if 0
    //    PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("logger4.properties"));
#else
    std::string pattern = "%D{%Y/%m/%d %H:%M:%S}- [%p] - %m [%l]%n";
    log4cplus::Layout *layout1 = 0;
    log4cplus::Layout *layout2 = 0;
    log4cplus::SharedAppenderPtr coutAppend(new log4cplus::ConsoleAppender());
    coutAppend->setName("Cout");
    coutAppend->setLayout(std::unique_ptr(
            layout1 = new log4cplus::PatternLayout(pattern)));

    log4cplus::helpers::Properties properties;
    properties.setProperty(LOG4CPLUS_TEXT("File"), LOG4CPLUS_TEXT(fileName));
    properties.setProperty(LOG4CPLUS_TEXT("CreateDirs"), LOG4CPLUS_TEXT("true"));
    properties.setProperty(LOG4CPLUS_TEXT("ImmediateFlush"), LOG4CPLUS_TEXT("true"));
    properties.setProperty(LOG4CPLUS_TEXT("MaxFileSize"), LOG4CPLUS_TEXT(MaxFileSize));
    properties.setProperty(LOG4CPLUS_TEXT("MaxBackupIndex"), LOG4CPLUS_TEXT(MaxBackupIndex));
    properties.setProperty(LOG4CPLUS_TEXT("Append"), LOG4CPLUS_TEXT("true"));
    properties.setProperty(LOG4CPLUS_TEXT("TextMode"), LOG4CPLUS_TEXT("Binary"));

    // SharedAppenderPtr  fileAppend(new  RollingFileAppender(fileName, 50*1024*1024, 5, true, true));
    log4cplus::SharedAppenderPtr fileAppend(new log4cplus::RollingFileAppender(properties));
    fileAppend->setName("File");
    fileAppend->setLayout(std::unique_ptr(
            layout2 = new log4cplus::PatternLayout(pattern)));

    log4cplus::SharedAppenderPtr asyncAppender(new log4cplus::AsyncAppender(fileAppend, 1000));
#endif
    switch (type) {
        case Cout:
            _logger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("Cout"));
            _logger.addAppender(coutAppend);
            break;
        case File:
            _logger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("File"));
            _logger.addAppender(asyncAppender);
            break;
        default:
            _logger = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("Cout"));
            _logger.addAppender(coutAppend);
            break;
    }

    switch (level) {
        case None:
            _logger.setLogLevel(log4cplus::NOT_SET_LOG_LEVEL);
            break;
        case Off:
            _logger.setLogLevel(log4cplus::OFF_LOG_LEVEL);
            break;
        case Crit:
            _logger.setLogLevel(log4cplus::FATAL_LOG_LEVEL);
            break;
        case Err:
            _logger.setLogLevel(log4cplus::ERROR_LOG_LEVEL);
            break;
        case Warning:
            _logger.setLogLevel(log4cplus::WARN_LOG_LEVEL);
            break;
        case Info:
            _logger.setLogLevel(log4cplus::INFO_LOG_LEVEL);
            break;
        case Debug:
            _logger.setLogLevel(log4cplus::DEBUG_LOG_LEVEL);
            break;
        case Trace:
            _logger.setLogLevel(log4cplus::TRACE_LOG_LEVEL);
            break;
        default:
            _logger.setLogLevel(log4cplus::NOT_SET_LOG_LEVEL);
            break;
    }
    S_LOG_INFO("Logger system start finish.");
    //#]
}

你可能感兴趣的:(log4cplus)