log4cplus使用

依赖包安装

  1. 使用神器vcpkg
  2. vcpkg install log4cplus[core, unicode]:x86-windows, 安装匹配项目的包

Show Me the Code

#ifndef LOGGER_HEADER
#define LOGGER_HEADER
#include <iostream>

#include <log4cplus/loggingmacros.h>
#include <log4cplus/logger.h>
#include <log4cplus/consoleappender.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/layout.h>
#include <log4cplus/configurator.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/helpers/stringhelper.h>

#include <boost/filesystem.hpp>
#include <boost/shared_ptr.hpp>

using namespace boost::filesystem;

/*
* Configuration:
	log4cplus.rootLogger=ALL, MR, CA
	log4cplus.appender.CA=log4cplus::ConsoleAppender
	log4cplus.appender.CA.Encoding=utf-8
	log4cplus.appender.CA.layout=log4cplus::PatternLayout
	log4cplus.appender.CA.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S.%Q}] %-5p %m%n

	log4cplus.appender.MR=log4cplus::TimeBasedRollingFileAppender
	log4cplus.appender.MR.FilenamePattern=Log/%d{yyyy-MM-dd}.log
	log4cplus.appender.MR.Schedule=DAILY
	log4cplus.appender.MR.Append=true
	log4cplus.appender.MR.MaxHistory=365
	log4cplus.appender.MR.RollOnClose=false
	log4cplus.appender.MR.CreateDirs=true
	log4cplus.appender.MR.MaxFileSize=10MB
	log4cplus.appender.MR.MaxBackupIndex=10
	log4cplus.appender.MR.Encoding=utf-8
	log4cplus.appender.MR.layout=log4cplus::PatternLayout
	log4cplus.appender.MR.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S.%Q}] %-5p %m%n
*/

class Logger {
public:
	//static Logger& getInstance();
	//static std::shared_ptr<Logger> getInstance();
	static boost::shared_ptr<Logger> getInstance();
	log4cplus::Logger getLogger();

	~Logger();

private:
	Logger();
	
	// 防止外部调用拷贝和赋值构造
	Logger(const Logger&);
	const Logger& operator=(const Logger&);

	log4cplus::Logger _logger;
};

//static Logger* instance = nullptr;
//static std::shared_ptr<Logger> instance = nullptr;
static boost::shared_ptr<Logger> instance = nullptr;
static std::once_flag of;

Logger::Logger()
{
#pragma region 文件载入
	//if (!exists("./Log/"))
	//{
	//	create_directory("./Log/");
	//}

	//log4cplus::PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("log4cplus.cfg"));
	//_logger = log4cplus::Logger::getRoot();
#pragma endregion

#pragma region 代码载入
	log4cplus::initialize();
	log4cplus::helpers::Properties consoleProperties;
	consoleProperties.setProperty(LOG4CPLUS_TEXT("Encoding"), LOG4CPLUS_TEXT("utf-8"));
	log4cplus::SharedAppenderPtr CA(new log4cplus::ConsoleAppender(consoleProperties));
	CA->setName(LOG4CPLUS_TEXT("CA"));
	log4cplus::tstring pattern = LOG4CPLUS_TEXT("[%D{%Y-%m-%d %H:%M:%S.%Q}] %-5p %m%n");
	CA->setLayout(std::unique_ptr<log4cplus::Layout>(new log4cplus::PatternLayout(pattern)));

	log4cplus::helpers::Properties timeBasedProperties;
	timeBasedProperties.setProperty(LOG4CPLUS_TEXT("FilenamePattern"), LOG4CPLUS_TEXT("Log/%d{yyyy-MM-dd}.log"));
	timeBasedProperties.setProperty(LOG4CPLUS_TEXT("Schedule"), LOG4CPLUS_TEXT("DAILY"));
	timeBasedProperties.setProperty(LOG4CPLUS_TEXT("Append"), LOG4CPLUS_TEXT("true"));
	timeBasedProperties.setProperty(LOG4CPLUS_TEXT("MaxHistory"), LOG4CPLUS_TEXT("365"));
	timeBasedProperties.setProperty(LOG4CPLUS_TEXT("RollOnClose"), LOG4CPLUS_TEXT("false"));
	timeBasedProperties.setProperty(LOG4CPLUS_TEXT("CreateDirs"), LOG4CPLUS_TEXT("true"));
	timeBasedProperties.setProperty(LOG4CPLUS_TEXT("MaxFileSize"), LOG4CPLUS_TEXT("10MB"));
	timeBasedProperties.setProperty(LOG4CPLUS_TEXT("MaxBackupIndex"), LOG4CPLUS_TEXT("10"));
	timeBasedProperties.setProperty(LOG4CPLUS_TEXT("Encoding"), LOG4CPLUS_TEXT("utf-8"));
	log4cplus::SharedAppenderPtr MR(new log4cplus::TimeBasedRollingFileAppender(timeBasedProperties));
	pattern = LOG4CPLUS_TEXT("[%D{%Y-%m-%d %H:%M:%S.%Q}] %-5p %m%n");
	MR->setLayout(std::unique_ptr<log4cplus::Layout>(new log4cplus::PatternLayout(pattern)));
#pragma endregion

	_logger = log4cplus::Logger::getRoot();
	_logger.setLogLevel(log4cplus::ALL_LOG_LEVEL);
	_logger.addAppender(CA);
	_logger.addAppender(MR);
}

Logger::~Logger() {
	log4cplus::Logger::shutdown();
}

//Logger& Logger::getInstance() {
//	static Logger instance;
//	return instance;
//}
 
//std::shared_ptr<Logger> Logger::getInstance() {
//	std::call_once(instance_once_flag, [&]() {
//		instance = std::shared_ptr<Logger>(new Logger());
//	});
//
//	return instance;
//}

boost::shared_ptr<Logger> Logger::getInstance() {
	std::call_once(of, [&]() {
		instance = boost::shared_ptr<Logger>(new Logger());
	});

	return instance;
}

//Logger& Logger::getInstance() {
//	std::call_once(instance_once_flag, [&]() {
//		instance = new Logger();
//	});
//
//	return *instance;
//}

log4cplus::Logger Logger::getLogger() {
	return _logger;
}

#define LOG_TRACE(log) LOG4CPLUS_TRACE(Logger::getInstance()->getLogger(), log)
#define LOG_DEBUG(log) LOG4CPLUS_DEBUG(Logger::getInstance()->getLogger(), log)
#define LOG_INFO(log) LOG4CPLUS_INFO(Logger::getInstance()->getLogger(), log)
#define LOG_WARN(log) LOG4CPLUS_WARN(Logger::getInstance()->getLogger(), log)
#define LOG_ERROR(log) LOG4CPLUS_ERROR(Logger::getInstance()->getLogger(), log)
#define LOG_FATAL(log) LOG4CPLUS_FATAL(Logger::getInstance()->getLogger(), log)
#endif
posted @ 2022-08-01 16:16  非法关键字  阅读(313)  评论(0编辑  收藏  举报