首先是编译,安装log4cplus。
第二是对API进行封装,代码如下:
log.h
#ifndef _LOG_H_ #define _LOG_H_ #include <string> #define DEBUG(input) DEBUG_LOG((input), (__FILE__), (__LINE__)); #define ERROR(input) ERROR_LOG((input), (__FILE__), (__LINE__)); #define WARN(input) WARN_LOG((input), (__FILE__), (__LINE__)); void DEBUG_LOG(const std::string& strInfo, const std::string& strFile, int iLine); void ERROR_LOG(const std::string& strInfo, const std::string& strFile, int iLine); void WARN_LOG(const std::string& strInfo, const std::string& strFile, int iLine); class CLog { public: static CLog* Initialize(const std::string& strLogFile); private: CLog(const std::string& strLogFile); static CLog* pInstance; }; #endif
log.cpp
#include "log.h" #include <sstream> #include <memory> #include <log4cplus/logger.h> #include <log4cplus/loggingmacros.h> #include <log4cplus/fileappender.h> #include <log4cplus/configurator.h> using namespace std; using namespace log4cplus; static Logger global_pLogger; CLog* CLog::pInstance = NULL; CLog* CLog::Initialize(const string& strLogFile) { if (NULL == pInstance) { pInstance = new CLog(strLogFile); } return pInstance; } CLog::CLog(const string& strLogFile) { PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("./log4cplus.properties")); global_pLogger = Logger::getRoot(); } void DEBUG_LOG(const string& strInfo, const string& strFile, int iLine) { stringstream ssLogData; ssLogData << "[" << strFile << ":" << iLine << "] " << strInfo; LOG4CPLUS_DEBUG(global_pLogger, ssLogData.str()); } void ERROR_LOG(const string& strInfo, const string& strFile, int iLine) { stringstream ssLogData; ssLogData << "[" << strFile << ":" << iLine << "] " << strInfo; LOG4CPLUS_ERROR(global_pLogger, ssLogData.str()); } void WARN_LOG(const string& strInfo, const string& strFile, int iLine) { stringstream ssLogData; ssLogData << "[" << strFile << ":" << iLine << "] " << strInfo; LOG4CPLUS_WARN(global_pLogger, ssLogData.str()); }
配置文件
#配置文件(其它日志级别配置相同): log4cplus.rootLogger=WARN, DEBUG_MSGS, ERROR_MSGS, WARN_MSGS ################################DEBUG#################################### #设置日志追加到文件尾 log4cplus.appender.DEBUG_MSGS=log4cplus::RollingFileAppender #设置日志文件大小 log4cplus.appender.DEBUG_MSGS.MaxFileSize=10240MB #设置生成日志最大个数 log4cplus.appender.DEBUG_MSGS.MaxBackupIndex=5 #设置输出日志路径 log4cplus.appender.DEBUG_MSGS.File=/home/boris/log4cplus/test/debug.log log4cplus.appender.DEBUG_MSGS.layout=log4cplus::PatternLayout #设置日志打印格式 log4cplus.appender.DEBUG_MSGS.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S}]%p%m%n log4cplus.appender.DEBUG_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter #匹配相同日志级别,只有debug日志才输入到该文件中 log4cplus.appender.DEBUG_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter log4cplus.appender.DEBUG_MSGS.filters.1.LogLevelToMatch=DEBUG log4cplus.appender.DEBUG_MSGS.filters.1.AcceptOnMatch=true log4cplus.appender.DEBUG_MSGS.filters.2=log4cplus::spi::DenyAllFilter ################################ERROR#################################### #设置日志追加到文件尾 log4cplus.appender.ERROR_MSGS=log4cplus::RollingFileAppender #设置日志文件大小 log4cplus.appender.ERROR_MSGS.MaxFileSize=10240MB #设置生成日志最大个数 log4cplus.appender.ERROR_MSGS.MaxBackupIndex=5 #设置输出日志路径 log4cplus.appender.ERROR_MSGS.File=/home/boris/log4cplus/test/error.log log4cplus.appender.ERROR_MSGS.layout=log4cplus::PatternLayout #设置日志打印格式 log4cplus.appender.ERROR_MSGS.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S}]%p%m%n log4cplus.appender.ERROR_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter #匹配相同日志级别,只有debug日志才输入到该文件中 log4cplus.appender.ERROR_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter log4cplus.appender.ERROR_MSGS.filters.1.LogLevelToMatch=ERROR log4cplus.appender.ERROR_MSGS.filters.1.AcceptOnMatch=true log4cplus.appender.ERROR_MSGS.filters.2=log4cplus::spi::DenyAllFilter ################################WARN#################################### #设置日志追加到文件尾 log4cplus.appender.WARN_MSGS=log4cplus::RollingFileAppender #设置日志文件大小 log4cplus.appender.WARN_MSGS.MaxFileSize=10240MB #设置生成日志最大个数 log4cplus.appender.WARN_MSGS.MaxBackupIndex=5 #设置输出日志路径 log4cplus.appender.WARN_MSGS.File=/home/boris/log4cplus/test/warn.log log4cplus.appender.WARN_MSGS.layout=log4cplus::PatternLayout #设置日志打印格式 log4cplus.appender.WARN_MSGS.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S}]%p%m%n log4cplus.appender.WARN_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter #匹配相同日志级别,只有debug日志才输入到该文件中 log4cplus.appender.WARN_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter log4cplus.appender.WARN_MSGS.filters.1.LogLevelToMatch=WARN log4cplus.appender.WARN_MSGS.filters.1.AcceptOnMatch=true log4cplus.appender.WARN_MSGS.filters.2=log4cplus::spi::DenyAllFilter
main.cpp
使用起来就是这么的简单!
#include "log.h" int main() { CLog::Initialize(""); DEBUG("Hello, World!"); WARN("Hello, World!"); ERROR("Hello, World!"); return 0; }
原文地址:http://blog.csdn.net/nyist327/article/details/43707043