=================================版权声明=================================
版权声明:本文为博主原创文章 未经许可不得转载
请通过右侧公告中的“联系邮箱(wlsandwho@foxmail.com)”联系我
未经作者授权勿用于学术性引用。
未经作者授权勿用于商业出版、商业印刷、商业引用以及其他商业用途。
本文不定期修正完善,为保证内容正确,建议移步原文处阅读。 <--------总有一天我要自己做一个模板干掉这只土豆
本文链接:http://www.cnblogs.com/wlsandwho/p/4666418.html
耻辱墙:http://www.cnblogs.com/wlsandwho/p/4206472.html
=======================================================================
也不知道写的对不对,目前能用就好。
=======================================================================
自动生成文件名、设置过滤等级、设置日志格式、输出到完备日志和重要日志
(部分功能没用到就不删除了,也不碍事。)
=======================================================================
日志格式:
00000001 2015-07-22_09:14:35.771886 <fatal> 王林森原创 未经许可请勿转载 侵权必究
( 行号 ) ( 时间戳 ) (等级) ( 内容 )
=======================================================================
头文件
#include <iostream>
#include <boost/locale/generator.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/common.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <cstddef>
#include <string>
#include <fstream>
#include <iomanip>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/log/core.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sources/basic_logger.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sources/global_logger_storage.hpp> namespace logging = boost::log;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords; enum severity_level
{
normal,
message,
alarm,
error,
fatal
}; template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
static const char* const str[] =
{
"normal",
"message",
"alarm",
"error",
"fatal"
};
if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
strm << str[lvl];
else
strm << static_cast< int >(lvl);
return strm;
} BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(timeline, "Timeline", attrs::timer::value_type) void init_logging();
实现文件
void CHTCollectorDlg::init_logging()
{
CString strFileName=m_strLogLocation+TEXT("\\WLS_%Y-%m-%d_FULL.log"); std::locale::global(std::locale("chs")); typedef sinks::synchronous_sink< sinks::text_file_backend > text_sink; boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >( keywords::open_mode=std::ios::app,
keywords::file_name =strFileName,
keywords::rotation_size = * * * ,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(, , )
);
sink->set_formatter(expr::stream
<< std::dec << std::setw() << std::setfill('') << line_id << std::dec << std::setfill(' ')
<<"\t"<<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f")
<< "\t<"<< severity.or_default(normal)
<< ">\t\t"<<expr::if_(expr::has_attr(tag_attr))
[
expr::stream << "[" << tag_attr << "]"
]
<< expr::if_(expr::has_attr(timeline))
[
expr::stream << "[>"<< timeline << "<]"
]
<< expr::message); sink->locked_backend()->auto_flush(true); logging::core::get()->add_sink(sink); //////////////////////////////////////////////////////////////////////////
strFileName=m_strLogLocation+TEXT("WLS_%Y-%m-%d_IMPORTANT.log");
sink = boost::make_shared< text_sink >( keywords::open_mode=std::ios::app,
keywords::file_name =strFileName,
keywords::rotation_size = * * * ,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(, , )
);
sink->set_formatter(expr::stream
<< std::dec << std::setw() << std::setfill('') << line_id << std::dec << std::setfill(' ')
<<"\t"<<expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f")
<< "\t<"<< severity.or_default(normal)
<< ">\t\t"<<expr::if_(expr::has_attr(tag_attr))
[
expr::stream << "[" << tag_attr << "]"
]
<< expr::if_(expr::has_attr(timeline))
[
expr::stream << "[>"<< timeline << "<]"
]
<< expr::message); sink->locked_backend()->auto_flush(true);
sink->set_filter(severity>=alarm);
logging::core::get()->add_sink(sink); logging::add_common_attributes();
}
用法
AddRecord(severity_level sl,CString str)
{
src::wseverity_logger< severity_level > slg;
BOOST_LOG_SEV(slg, sl) <<str.GetBuffer(str.GetLength());
}