什么是营销型的网站推广,平台网站很难做,响应式模板网站模板,深圳网站维护页面设计设计目标: 1、每个Logging source对应一个目录#xff0c;可以设置日志文件数#xff0c;日志大小#xff0c;目录名#xff0c;文件名等 2、所有logging source日志目录都在一个根目录下。 3、可以动态创建和删除logging source 4、打印出日期时间和日志严重等级 示例代码…设计目标: 1、每个Logging source对应一个目录可以设置日志文件数日志大小目录名文件名等 2、所有logging source日志目录都在一个根目录下。 3、可以动态创建和删除logging source 4、打印出日期时间和日志严重等级 示例代码:
#include libs/Core/LogMananger.hint main(){LogManager::instance().setRootPath(logs);LogManager::instance().addLogger(network, network, 1024*100, 10);LogManager::instance().addLogger(db, db, 1024*100, 10);for(int i 0; i 10000; i){LOG(network, info) hello world aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;LOG(network, trace) hello world aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;LOG(network, warning) hello world aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;LOG(network, fatal) hello world ;LOG(network, debug) hello world;LOG(db, info) hello world aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;LOG(db, trace) hello world aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;LOG(db, warning) hello world aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;LOG(db, fatal) hello world;LOG(db, debug) hello world;}return 0;
}
生成的日志目录结构: 日志格式: LogManager.h
#pragma once
#include boost/log/trivial.hpp
#include boost/log/sources/severity_logger.hpp
#include boost/log/sources/logger.hpp
#include boost/log/sources/basic_logger.hpp
#include boost/log/sinks.hpp
#include boost/log/core.hpp
#include boost/log/utility/setup/file.hpp
#include boost/log/sinks.hpp
#include boost/core/null_deleter.hpp
#include boost/log/attributes.hpp
#include boost/log/utility/setup/console.hpp
#include boost/log/attributes/constant.hpp
#include boost/log/exceptions.hpp
#include boost/log/keywords/format.hpp
#include boost/log/expressions.hpp
#include boost/log/expressions/formatters/date_time.hpp
#include boost/log/support/date_time.hpp
#include boost/log/utility/setup/common_attributes.hpp#include memory
#include map
#include fstream
#include iostream
#include string
#include mutex
using namespace std;
namespace sources boost::log::sources;
namespace sinks boost::log::sinks;
namespace expressions boost::log::expressions;
namespace trivial boost::log::trivial;
namespace keywords boost::log::keywords;
namespace attributes boost::log::attributes;class LogManager
{struct LoggerInfo{string m_strName;string m_strPath;int m_nMaxSize;int m_nMaxCount;boost::shared_ptrsources::severity_logger_mttrivial::severity_level m_pLogger;boost::shared_ptrsinks::synchronous_sinksinks::text_file_backend m_pSink;};
public:~LogManager();static LogManager instance();void setRootPath(const string strRootPath );void addLogger(const string strName, const string strPath, int nMaxSize, int nMaxCount);void removeLogger(const string strName);boost::log::sources::severity_logger_mttrivial::severity_level getLogger(const string strName);
private:LogManager();
private:string m_strRootPath;mapstring, LoggerInfo m_mapLogger;mutex m_oLock;
};#define LOG(name, level) BOOST_LOG_SEV(LogManager::instance().getLogger(#name), boost::log::trivial::severity_level::level)
LogManager.cpp
#include LogMananger.hLogManager::LogManager()
{}LogManager::~LogManager()
{}LogManager LogManager::instance()
{static LogManager t;return t;
}void LogManager::setRootPath(const string strRootPath)
{m_strRootPath strRootPath;
}void LogManager::addLogger(const string strName, const string strPath, int nMaxSize, int nMaxCount)
{lock_guardmutex lk(m_oLock);if(m_mapLogger.contains(strName))return;m_mapLogger[strName] LoggerInfo();m_mapLogger[strName].m_strName strName;m_mapLogger[strName].m_strPath strPath;m_mapLogger[strName].m_nMaxSize nMaxSize;m_mapLogger[strName].m_nMaxCount nMaxCount;m_mapLogger[strName].m_pLogger boost::make_sharedsources::severity_logger_mttrivial::severity_level();m_mapLogger[strName].m_pLogger-add_attribute(LoggerName, attributes::constantstring(strName));m_mapLogger[strName].m_pLogger-add_attribute(DateTime, attributes::local_clock());boost::shared_ptrsinks::text_file_backend pTxtFileBackend boost::make_sharedsinks::text_file_backend( keywords::file_name m_strRootPath / strPath / strName .log, //是日志文件大小没达到最大值时,日志写的文件keywords::target_file_name strName _%2N.log, //当日志文件达到最大值时日志文件名会被改写这里的%2N表示用两位数字表示keywords::rotation_size nMaxSize, //单个日志文件最大值这里是5Mkeywords::open_mode std::ios_base::out | std::ios_base::app, //添加模式keywords::time_based_rotation sinks::file::rotation_at_time_point(12, 0, 0)); //每天12点会重新生成一个日志文件不管日志文件是否到了最大值m_mapLogger[strName].m_pSink boost::make_sharedsinks::synchronous_sinksinks::text_file_backend(pTxtFileBackend);m_mapLogger[strName].m_pSink-locked_backend()-set_file_collector(sinks::file::make_collector(keywords::max_files nMaxCount, //最多N个日志文件keywords::max_size nMaxSize*(1nMaxCount), //正目录最大占用多少磁盘空间keywords::target m_strRootPath / strPath //日志保存的目录前面target_file_name所在的目录));m_mapLogger[strName].m_pSink-locked_backend()-auto_flush(true);m_mapLogger[strName].m_pSink-locked_backend()-scan_for_files();m_mapLogger[strName].m_pSink-set_formatter(expressions::stream expressions::if_(expressions::has_attrboost::posix_time::ptime(DateTime))[expressions::stream [ expressions::format_date_time boost::posix_time::ptime (DateTime, %Y-%m-%d %H:%M:%S.%f) ] ] [ trivial::severity ] expressions::smessage);m_mapLogger[strName].m_pSink-set_filter(expressions::has_attr(LoggerName) expressions::attrstring(LoggerName) strName);boost::log::core::get()-add_sink(m_mapLogger[strName].m_pSink);
}void LogManager::removeLogger(const string strName)
{lock_guardmutex lk(m_oLock);if(m_mapLogger.contains(strName))boost::log::core::get()-remove_sink(m_mapLogger[strName].m_pSink);
}sources::severity_logger_mttrivial::severity_level LogManager::getLogger(const string strName)
{lock_guardmutex lk(m_oLock);return *m_mapLogger[strName].m_pLogger;
}