LOG4CPLUS单例封装及配置项

 

log4cplus.logger.YxLogObject= TRACE,DEBUG_YxLogObject,YxLogObject_INFO, ERR_YxLogObject

log4cplus.appender.DEBUG_YxLogObject=log4cplus::RollingFileAppender
log4cplus.appender.DEBUG_YxLogObject.File=./log/yxclient/debug.log
log4cplus.appender.DEBUG_YxLogObject.Appender=log4cplus::RollingFileAppender
log4cplus.appender.DEBUG_YxLogObject.QueueLimit=999
log4cplus.appender.DEBUG_YxLogObject.MaxFileSize=50MB
log4cplus.appender.DEBUG_YxLogObject.MaxBackupIndex=10
log4cplus.appender.DEBUG_YxLogObject.Encoding=utf-8
log4cplus.appender.DEBUG_YxLogObject.layout=log4cplus::PatternLayout
log4cplus.appender.DEBUG_YxLogObject.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S.%q} [%-5p] %m %n
log4cplus.appender.DEBUG_YxLogObject.filters.1=log4cplus::spi::LogLevelMatchFilter
log4cplus.appender.DEBUG_YxLogObject.filters.1.LogLevelToMatch=DEBUG
log4cplus.appender.DEBUG_YxLogObject.filters.1.AcceptOnMatch=true
log4cplus.appender.DEBUG_YxLogObject.CreateDirs=true 
log4cplus.appender.DEBUG_YxLogObject.filters.2=log4cplus::spi::DenyAllFilter

log4cplus.appender.YxLogObject_INFO=log4cplus::RollingFileAppender
log4cplus.appender.YxLogObject_INFO.File=./log/yxclient/run.log
log4cplus.appender.YxLogObject_INFO.Appender=log4cplus::RollingFileAppender
log4cplus.appender.YxLogObject_INFO.QueueLimit=100000
log4cplus.appender.YxLogObject_INFO.MaxFileSize=50MB
log4cplus.appender.YxLogObject_INFO.MaxBackupIndex=10
log4cplus.appender.YxLogObject_INFO.Encoding=utf-8
log4cplus.appender.YxLogObject_INFO.CreateDirs=true 
log4cplus.appender.YxLogObject_INFO.filters.1=log4cplus::spi::LogLevelMatchFilter
log4cplus.appender.YxLogObject_INFO.filters.1.AcceptOnMatch=true
log4cplus.appender.YxLogObject_INFO.filters.1.LogLevelToMatch=INFO
log4cplus.appender.YxLogObject_INFO.layout=log4cplus::PatternLayout
log4cplus.appender.YxLogObject_INFO.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S.%q} [%-5p] %m %n
log4cplus.appender.YxLogObject_INFO.filters.2=log4cplus::spi::DenyAllFilter


log4cplus.appender.ERR_YxLogObject=log4cplus::RollingFileAppender
log4cplus.appender.ERR_YxLogObject.File=./log/yxclient/error.log
log4cplus.appender.ERR_YxLogObject.Appender=log4cplus::RollingFileAppender
log4cplus.appender.ERR_YxLogObject.QueueLimit=100000
log4cplus.appender.ERR_YxLogObject.MaxFileSize=50MB
log4cplus.appender.ERR_YxLogObject.MaxBackupIndex=10
log4cplus.appender.ERR_YxLogObject.Encoding=utf-8
log4cplus.appender.ERR_YxLogObject.layout=log4cplus::PatternLayout
log4cplus.appender.ERR_YxLogObject.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S.%q} [%-5p] %m %n
log4cplus.appender.ERR_YxLogObject.filters.1=log4cplus::spi::LogLevelMatchFilter
log4cplus.appender.ERR_YxLogObject.filters.1.LogLevelToMatch=ERROR
log4cplus.appender.ERR_YxLogObject.filters.1.AcceptOnMatch=true
log4cplus.appender.ERR_YxLogObject.CreateDirs=true 
log4cplus.appender.ERR_YxLogObject.filters.2=log4cplus::spi::DenyAllFilter

 

#pragma  once
#include 
#include "YxSingleton.h"
#include 
#include 
#include 
#include 
using namespace std;
using namespace log4cplus;

template 
inline std::string format_str(const char* pformat, Args... args)
{
	// 计算字符串长度
	int len_str = std::snprintf(nullptr, 0, pformat, args...);

	if (0 >= len_str)
		return std::string("");

	len_str++;
	char* pstr_out = nullptr;
	pstr_out = new(std::nothrow) char[len_str];
	// 申请失败
	if (NULL == pstr_out || nullptr == pstr_out)
		return std::string("");

	// 构造字符串
	std::snprintf(pstr_out, len_str, pformat, args...);

	// 保存构造结果
	std::string str(pstr_out);

	// 释放空间
	delete pstr_out;
	pstr_out = nullptr;

	return str;
}

enum LogLevelEnum
{
	_DEBUG_LOG_LEVEL_ =1,
	_RUNINFO_LOG_LEVEL_,
	_ERROR_LOG_LEVEL_ 

};

class YxLog4cppObject 
{
public:
	SINGLETON(YxLog4cppObject)	
public:
	void WriteDebugLog(string _strLog);
	void WriteRunInfoLog(string _strLog);
	void WriteErrorLog(string _strLog);
	void SetLogLevel(const LogLevelEnum& _enLevel);
private:
	Logger m_logger;
	log4cplus::Hierarchy m_hierarchy;
};


#include "YxLog4cppObject.hpp"
#include 
#include 
#include 
#include 

using namespace std;
using namespace log4cplus::helpers;

#define LOG4CPLUS_CONF_FILE "./YxStreamClient.properties"


YxLog4cppObject::YxLog4cppObject(){
	
	PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT(LOG4CPLUS_CONF_FILE), m_hierarchy);
	m_logger = m_hierarchy.getInstance("YxLogObject");
}
YxLog4cppObject::~YxLog4cppObject() {}

void YxLog4cppObject::WriteDebugLog( string  _strLog)
{
	qDebug() << _strLog.c_str();
	LOG4CPLUS_DEBUG(m_logger, _strLog.c_str());
}

void YxLog4cppObject::WriteRunInfoLog( string  _strLog)
{
	qDebug() << _strLog.c_str();
 	LOG4CPLUS_INFO(m_logger, _strLog.c_str());
}

void YxLog4cppObject::WriteErrorLog( string  _strLog)
{
	qDebug() << _strLog.c_str();
	LOG4CPLUS_ERROR(m_logger, _strLog.c_str());
}


void YxLog4cppObject::SetLogLevel(const LogLevelEnum& _enLevel)
{
	switch (_enLevel)
	{ 
	case _DEBUG_LOG_LEVEL_:
		m_logger.setLogLevel(DEBUG_LOG_LEVEL);
		break;
	case _RUNINFO_LOG_LEVEL_:
		m_logger.setLogLevel(INFO_LOG_LEVEL);
		break;
	case _ERROR_LOG_LEVEL_:
		m_logger.setLogLevel(ERROR_LOG_LEVEL);
		break;
	default:
		m_logger.setLogLevel(DEBUG_LOG_LEVEL);
		break;
	}
}

 

你可能感兴趣的:(LOG4CPLUS单例封装及配置项)