此演示程序在VS2010上运行通过,此演示程序参考http://www.cnblogs.com/luhouxiang/archive/2012/03/03/2378519.html而写成。
先贴出代码如下
这个是头文件Serverlog.h
/******************************************************************************
author : xxd
purpose : 日志系统,以宏log_XXX开头,封装log4cxx日志。
*******************************************************************************/
#pragma once
#include <log4cxx/logstring.h>
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <locale.h>
using namespace log4cxx;
using namespace log4cxx::helpers;
//#include "log4cxx/logger.h"
//#include "log4cxx/propertyconfigurator.h"
/*
日志类的封装
*/
#include <string>
using namespace std;
class CServerlog;
extern CServerlog* g_serverlog;
#define log_debug(pszFormat,...) g_serverlog->WriteFormatDebugLog(pszFormat, __VA_ARGS__)
#define log_info(pszFormat,...) g_serverlog->WriteFormatInfoLog(pszFormat, __VA_ARGS__)
#define log_warn(pszFormat,...) g_serverlog->WriteFormatWarnLog(pszFormat, __VA_ARGS__)
#define log_error(pszFormat,...) g_serverlog->WriteFormatErrorLog(pszFormat, __VA_ARGS__)
class CServerlog
{
public:
CServerlog();
~CServerlog();
void WriteFormatDebugLog(char* lpszFormat,...);
void WriteFormatInfoLog(char* lpszFormat,...);
void WriteFormatWarnLog(char* lpszFormat,...);
void WriteFormatErrorLog(char* lpszFormat,...);
protected:
log4cxx::LoggerPtr infologger;
};
这个是Serverlog.cpp文件
#include "Serverlog.h"
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include "Serverlog.h"
CServerlog* g_serverlog=new CServerlog();
CServerlog::CServerlog()
{
log4cxx::PropertyConfigurator::configure("log4cxx.properties");
infologger = (log4cxx::Logger::getLogger("server"));
}
CServerlog::~CServerlog()
{
}
void CServerlog::WriteFormatDebugLog(char* lpszFormat,...)
{
va_list args;
va_start(args,lpszFormat);
char szBuffer[1024]={0};
vsprintf(szBuffer,lpszFormat,args);
va_end(args);
infologger->debug(szBuffer);
}
void CServerlog::WriteFormatInfoLog(char* lpszFormat,...)
{
va_list args;
va_start(args,lpszFormat);
char szBuffer[1024]={0};
vsprintf(szBuffer,lpszFormat,args);
va_end(args);
infologger->info(szBuffer);
}
void CServerlog::WriteFormatWarnLog(char* lpszFormat,...)
{
va_list args;
va_start(args,lpszFormat);
char szBuffer[1024]={0};
vsprintf(szBuffer,lpszFormat,args);
va_end(args);
infologger->warn(szBuffer);
}
void CServerlog::WriteFormatErrorLog(char* lpszFormat,...)
{
va_list args;
va_start(args,lpszFormat);
char szBuffer[1024]={0};
vsprintf(szBuffer,lpszFormat,args);
va_end(args);
infologger->error(szBuffer);
}
下面是写出的主程序代码main.cpp
#include "stdafx.h"
#include "Serverlog.h"
int _tmain(int argc, _TCHAR* argv[])
{
log_debug("mmmmmmmmmm");
log_info("hhhhhhhhhhh");
log_warn("yyyyyyyyyyyyy");
log_error("errrror");
return 0;
}
另外贴出日志文件所需的配置文件log4cxx.properties
log4j.rootLogger=
log4j.logger.server=DEBUG,Daily,xml
#Daily
log4j.appender.Daily=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Daily.File=./log/server.log
log4j.appender.Daily.DatePattern = '.'yyyy-MM-dd
# Keep 10 days backup files
log4j.appender.Daily.MaxBackupDays=10
log4j.appender.Daily.layout=org.apache.log4j.PatternLayout
log4j.appender.Daily.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%5p] [%c] - %m%n
#xml
log4j.appender.xml=org.apache.log4j.net.XMLSocketAppender
log4j.appender.xml.RemoteHost=localhost
log4j.appender.xml.Port=4448
log4j.appender.xml.LocationInfo=true
最后此程序代码可以从http://download.csdn.net/detail/xxd654321/8256745下载