log4cplus的使用方法

我们需要1至N个记录器——即log4cplus::Logger对象。写日志时,需要把指定Logger对象作为参数传入。我写了一个类,用来创建和配置一些log4cplus::Logger对象。有一个配置文件,可以在运行时修改日志的配置。如果缺少这个配置文件,程序会自动生成。

移植性:这个类使用了windows C运行时的API。这个类能适配MBCS或UNICODE字符集。

头文件:

extern log4cplus::Logger logMain;
extern log4cplus::Logger logTraceMsg;
extern log4cplus::Logger logTraceListen;

class CustomLog
{
public:
	static CustomLog* Instance();
	~CustomLog();
public:
	void InitLog();
	void CleanThreadData();
private:
	CustomLog();
	const static log4cplus_char_t* cfgFileName;
	const static char* strConfig;
	static CustomLog m_Instance;
};

源文件:

log4cplus::Logger logMain;
log4cplus::Logger logTraceMsg;
log4cplus::Logger logTraceListen;

CustomLog CustomLog::m_Instance;
#ifdef _DEBUG
const log4cplus_char_t* CustomLog::cfgFileName = LOG4CPLUS_TEXT("LogDebug.cfg");
const char* CustomLog::strConfig = \
	"log4cplus.logger.logMain     = Debug , file\n"
	"log4cplus.appender.file=log4cplus::RollingFileAppender\n"
	"log4cplus.appender.file.File=Server.log\n"
	"log4cplus.appender.file.MaxFileSize=10MB\n"
	"log4cplus.appender.file.MaxBackupIndex=5\n"
	"log4cplus.appender.file.layout=log4cplus::PatternLayout\n"
	"log4cplus.appender.file.layout.ConversionPattern=%D{%Y %m %d %H:%M:%S:%q}:[%-5t]:%-5p:[%-8c]: %m :%l:%M%n\n"
	"log4cplus.appender.file.Append=true\n"
	"log4cplus.appender.file.CreateDirs=true\n"
	"log4cplus.logger.logTraceMsg     = INFO , file1\n"
	"log4cplus.logger.logTraceListen  = INFO , file1\n"
	"log4cplus.appender.file1=log4cplus::RollingFileAppender\n"
	"log4cplus.appender.file1.File=ServerTrace.log\n"
	"log4cplus.appender.file1.MaxFileSize=10MB\n"
	"log4cplus.appender.file1.MaxBackupIndex=5\n"
	"log4cplus.appender.file1.layout=log4cplus::PatternLayout\n"
	"log4cplus.appender.file1.layout.ConversionPattern=%D{%Y %m %d %H:%M:%S:%q}:[%-5t]:%-5p:[%-8c]: %m%n\n"
	"log4cplus.appender.file1.Append=false\n"
	"log4cplus.appender.file1.CreateDirs=true\n";
#else
const log4cplus_char_t* CustomLog::cfgFileName = LOG4CPLUS_TEXT("LogRelease.cfg");
const char* CustomLog::strConfig = \
	"log4cplus.logger.logMain     = INFO , file\n"
	"log4cplus.appender.file=log4cplus::RollingFileAppender\n"
	"log4cplus.appender.file.File=Server.log\n"
	"log4cplus.appender.file.MaxFileSize=10MB\n"
	"log4cplus.appender.file.MaxBackupIndex=5\n"
	"log4cplus.appender.file.layout=log4cplus::PatternLayout\n"
	"log4cplus.appender.file.layout.ConversionPattern=%D{%Y %m %d %H:%M:%S:%q}:[%-5t]:%-5p:[%-8c]: %m :%l:%M%n\n"
	"log4cplus.appender.file.Append=true\n"
	"log4cplus.appender.file.CreateDirs=true\n"
	"log4cplus.logger.logTraceMsg     = INFO , file1\n"
	"log4cplus.logger.logTraceListen  = INFO , file1\n"
	"log4cplus.appender.file1=log4cplus::RollingFileAppender\n"
	"log4cplus.appender.file1.File=ServerTrace.log\n"
	"log4cplus.appender.file1.MaxFileSize=10MB\n"
	"log4cplus.appender.file1.MaxBackupIndex=5\n"
	"log4cplus.appender.file1.layout=log4cplus::PatternLayout\n"
	"log4cplus.appender.file1.layout.ConversionPattern=%D{%Y %m %d %H:%M:%S:%q}:[%-5t]:%-5p:[%-8c]: %m%n\n"
	"log4cplus.appender.file1.Append=false\n"  
	"log4cplus.appender.file1.CreateDirs=true\n";
#endif


CustomLog::CustomLog()
{

}

CustomLog* CustomLog::Instance()
{
	return &m_Instance;
}

CustomLog::~CustomLog()
{

}
void CustomLog::InitLog()
{
	static bool bInitialized = false;
	if (bInitialized)
	{
		return;
	}else
	{
		bInitialized = true;
	}
	
	//	初始化日志模块
	log4cplus::initialize ();
	log4cplus::helpers::LogLog::getLogLog()->setInternalDebugging(true);
	log4cplus::tstring file(cfgFileName);
	log4cplus::helpers::FileInfo fi;
	log4cplus_char_t sExePathName[MAX_PATH]={0};
	::GetModuleFileName(NULL,sExePathName,MAX_PATH);
	log4cplus_char_t sDrive[10]={0};
	log4cplus_char_t sDir[MAX_PATH]={0};
	log4cplus_char_t sName[50]={0};
	log4cplus_char_t sExt[10]={0};
	_tsplitpath_s (sExePathName,sDrive,sDir,sName,sExt);
	log4cplus::tstring strCfgFile(sDrive);
	strCfgFile += sDir;
	strCfgFile += cfgFileName;
	_tprintf_s(LOG4CPLUS_TEXT("cfg file:%s\n"),strCfgFile.c_str());
	if (getFileInfo (&fi, strCfgFile) == 0)
	{
		static log4cplus::ConfigureAndWatchThread configureThread(strCfgFile,5*1000);
	}else
	{
		std::ofstream in;
		in.open(strCfgFile,std::ios::trunc);
		in << strConfig;
		in.close();
		if (getFileInfo (&fi, strCfgFile) == 0)
		{
			static log4cplus::ConfigureAndWatchThread configureThread(strCfgFile,5*1000);
		}
	}
	
	logMain        =  log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("logMain"));
	logTraceMsg    =  log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("logTraceMsg"));
	logTraceListen =  log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("logTraceListen"));
}

void CustomLog::CleanThreadData()
{
	 log4cplus::threadCleanup();
}

 

你可能感兴趣的:(C++11)