其他分享
首页 > 其他分享> > 基于log4cxx的日志封装

基于log4cxx的日志封装

作者:互联网

//头文件
#pragma once
//添加log4cxx头文件
#pragma once
//添加log4cxx头文件
#include <iostream>
#include <log4cxx/logstring.h>
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <locale.h>
#include <log4cxx/basicconfigurator.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <Windows.h> 
#include <tchar.h> 
#include <cstdarg>
#include <crtdbg.h>
#include <stdarg.h>//可变参函数列表宏 包含头文件
using namespace std;
using namespace log4cxx;
using namespace log4cxx::helpers;
//按照不同的级别定义宏

/*
						  优点:利用宏可以调用CServerlog类的函数
*/
char str[1024] = {0};
//#define LINE  ((strcpy(str,atoi(__LINE__))))
#define BUFFSIZE 111111
#define log_debug(Character,...)    g_Mylog->Write_DebugLog(Character,__VA_ARGS__)
#define log_info(Character, ...)     g_Mylog->Write_InfoLog(Character, __VA_ARGS__)
#define log_warn(Character, ...)     g_Mylog->Write_WarnLog(Character, __VA_ARGS__)
#define log_error(Character, ...)     g_Mylog->Write_ErrorLog(Character, __VA_ARGS__)
char* MyLogInfo(char* lpszFormat, va_list);
class	MyLog;//声明一个日志类
extern MyLog* g_Mylog;
class MyLog
{
public:
	MyLog();
	~MyLog();

	void Write_DebugLog(char* Character, ...);
	void Write_InfoLog(char* Character, ...);
	void Write_WarnLog(char* Character, ...);
	void Write_ErrorLog(char* Character, ...);

protected:
	//实例化4个指针代表错误级别
	log4cxx::LoggerPtr Infologger;
	log4cxx::LoggerPtr Errorlogger;
	log4cxx::LoggerPtr Warnlogger;
	log4cxx::LoggerPtr Debuglogger;
};

#include "TestLog4cxx.h"
MyLog*  g_Mylog = new MyLog();
MyLog::MyLog()
{
	log4cxx::PropertyConfigurator::configure("log4cxx1.cfg");//配置文件
	Infologger = (log4cxx::Logger::getLogger("info"));//获取配置文件中info对应的句柄
	Errorlogger = (log4cxx::Logger::getLogger("error"));//获取配置文件中error对应的句柄
	Debuglogger = (log4cxx::Logger::getLogger("debug"));//获取配置文件中debug对应的句柄
	Warnlogger = (log4cxx::Logger::getLogger("warn"));//获取配置文件中warn对应的句柄
}
MyLog::~MyLog()
{
}
 char* MyLogInfo(char* lpszFormat, va_list args)
{
	//va_list args;
	//va_start(args, lpszFormat);//初始化指针  第一个可选参数 args  lpszFormat最后一个固定的可选参数 用于确定可变参数的个数
	 static char szBuffer[BUFFSIZE];
	memset(szBuffer, 0, sizeof(szBuffer));
	char *p = __FILE__;
	char *ptr = __FUNCTION__;
	strcpy(szBuffer, p);
	strcat(szBuffer, "-");
	strcat(szBuffer, ptr);
	strcat(szBuffer, "-");
	vsprintf(szBuffer + strlen(szBuffer), lpszFormat, args);
	cout << szBuffer << endl;
	va_end(args);//清空可变参数列表
	return szBuffer;

}
void MyLog::Write_DebugLog(char* Character, ...)//写debug日志
{
	va_list args;
	va_start(args, Character);
	Debuglogger->debug(MyLogInfo(Character, args));
}


void MyLog::Write_InfoLog(char* Character, ...)//写info日志
{
	va_list args;
	va_start(args, Character);
	Infologger->info(MyLogInfo(Character, args));
}

void MyLog::Write_WarnLog(char* Character, ...)//写warning日志
{
	va_list args;
	va_start(args, Character);
	Warnlogger->warn(MyLogInfo(Character, args));
}

void MyLog::Write_ErrorLog(char* Character, ...)//写warning日志
{
	va_list args;
	va_start(args, Character);
	Errorlogger->error(MyLogInfo(Character, args));
}

int main()
{
	log_debug("%d 11111",__LINE__);
	log_info("这是myinfo.log");
	log_warn("这是mywarn.log");
	log_error("这是myerror.log");
	system("pause");
	return EXIT_SUCCESS;
}

配置文件

#不向默认对象(rootLogger)输出,因此信息只能输出到文件
log4j.additivity.gather = false
#设置日志对象
log4j.logger.debug=DEBUG,debug
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.DatePattern=’…/Debug/logs/‘yyyy-MM-dd’/MyDebug.log’
log4j.appender.debug.ImmediateFlush=true
log4j.appender.debug.Append=true
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p [%t] %m%n

log4j.logger.info=INFO,info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.DatePattern=’…/Debug/logs/‘yyyy-MM-dd’/MyInfo.log’
log4j.appender.info.ImmediateFlush=true
log4j.appender.info.Append=true
log4j.appender.info.LocationInfo=true
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p [%t] - %m%n

log4j.logger.warn=WARN,warn
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.warn.DatePattern=’…/Debug/logs/‘yyyy-MM-dd’/MyWarn.log’
log4j.appender.warn.ImmediateFlush=true
log4j.appender.warn.Append=true
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p [%t] - %m%n

log4j.logger.error=ERROR,error
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.DatePattern=’…/Debug/logs/‘yyyy-MM-dd’/MyError.log’
log4j.appender.error.ImmediateFlush=true
log4j.appender.error.Append=true
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p [%t] - %m%n

运行效果如图:

今天好饿啊 发布了11 篇原创文章 · 获赞 0 · 访问量 184 私信 关注

标签:__,封装,args,Character,appender,日志,include,log4j,log4cxx
来源: https://blog.csdn.net/weixin_43512524/article/details/104014018