Boost 日志设置输出槽
- 课程 《Boost日志库快速入门》笔记 免费课程,强烈推荐
设置输出槽的步骤
- ① 创建输出槽
- ② 在日志核心中注册已创建的输出槽
【设置输出槽的注意事项】
- ① 一般在程序的启动代码中设置输出槽
- ② 快速日志输出使用库内建的默认输出槽
- ③ 一旦用户向日志核心添加其他输出槽, 内建的默认输出槽将不再被使用
程序实例
- 输出到指定文件
#include <boost/log/trivial.hpp> #include <boost/log/core.hpp> #include <boost/log/expressions.hpp> #include <boost/log/utility/setup/file.hpp> namespace logging = boost::log; void inti() { //向指定文件输出日志 logging::add_file_log("sample.log"); //设置显示级别,进行过滤 logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info ); } int main() { inti(); BOOST_LOG_TRIVIAL(trace) << "A trace severity message"; BOOST_LOG_TRIVIAL(debug) << "A debug severity message"; BOOST_LOG_TRIVIAL(info) << "An informational severity message"; BOOST_LOG_TRIVIAL(warning) << "A warning severity message"; BOOST_LOG_TRIVIAL(error) << "An error severity message"; BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message"; return 0; }
- 输出:
【案例:根据文件大小,自动归档】
#include <boost/log/trivial.hpp> #include <boost/log/core.hpp> #include <boost/log/expressions.hpp> #include <boost/log/utility/setup/file.hpp> #include <boost/log/sinks/text_file_backend.hpp> #include <boost/log/utility/setup/common_attributes.hpp> #include <boost/log/sources/severity_channel_logger.hpp> #include <boost/log/sources/record_ostream.hpp> namespace logging = boost::log; namespace src = boost::log::sources; namespace sinks = boost::log::sinks; namespace keywords = boost::log::keywords; void inti() { logging::add_file_log ( keywords::file_name = "sample_%N.log", keywords::rotation_size = 1 * 1024 * 1024, keywords::format = "[%TimeStamp%]:%Message%" ); //设置显示级别,进行过滤 logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info ); } int main() { inti(); logging::add_common_attributes(); //内置的日志源 using namespace logging::trivial; src::severity_logger<severity_level> lg; for (size_t i = 0; i < 70000; ++i) { std::cout << i << std::endl; BOOST_LOG_SEV(lg, info) << i; } return 0; }
- 输出:
- sample_2.log内容:
【案例:根据时间进行归档】
#include <boost/log/trivial.hpp> #include <boost/log/core.hpp> #include <boost/log/expressions.hpp> #include <boost/log/utility/setup/file.hpp> #include <boost/log/sinks/text_file_backend.hpp> #include <boost/log/utility/setup/common_attributes.hpp> #include <boost/log/sources/severity_channel_logger.hpp> #include <boost/log/sources/record_ostream.hpp> namespace logging = boost::log; namespace src = boost::log::sources; namespace sinks = boost::log::sinks; namespace keywords = boost::log::keywords; void inti() { logging::add_file_log ( keywords::file_name = "sample_%N.log", keywords::time_based_rotation = sinks::file::rotation_at_time_point(16, 10, 0), //设置时间点 时 分 keywords::format = "[%TimeStamp%]:%Message%" ); //设置显示级别,进行过滤 logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info ); } int main() { inti(); logging::add_common_attributes(); //内置的日志源 using namespace logging::trivial; src::severity_logger<severity_level> lg; for (size_t i = 0; i < 70; ++i) { std::cout << i << std::endl; BOOST_LOG_SEV(lg, info) << i; Sleep(1000); } return 0; }
- 输出:
- 注意时间:
- sample_0.log尾部
- sample_1.log头部
【分析】
程序add_file_log等价函数
void init() { // Construct the sink typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink; boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >(); // Add a stream to write log to sink->locked_backend()->add_stream( boost::make_shared< std::ofstream >("sample.log")); // Register the sink in the logging core logging::core::get()->add_sink(sink); }
- ① 输出槽由前端和后端组成
- ② 前端(synchronous_sink) 处理各种输出槽公共任务,如线程同步、过滤、格式化(对于基于文本的输出槽)
- ③ 后端(text_ostream_backend ) 处理与特定输出槽相关的事,如文件写入
- ④ 日志库中提供许多预置的可以相互配合使用的前后端
- ⑤ synchronous_sink是同步前端,允许多个线程同时写日志,后端无需考虑多线程问题
- ⑥ 后端(text_ostream_backend ),将格式化后的日志内容写入一个STL兼容的流对象中,除了写文件也可以写入控制台
【输出槽添加多个流对象实例程序】
- 代码
#include <fstream> #include <iostream> #include <boost/smart_ptr/shared_ptr.hpp> #include <boost/smart_ptr/make_shared_object.hpp> #include <boost/log/core.hpp> #include <boost/log/trivial.hpp> #include <boost/log/sinks/sync_frontend.hpp> #include <boost/log/sinks/text_ostream_backend.hpp> #include <boost/log/sources/logger.hpp> #include <boost/log/sources/record_ostream.hpp> #include <boost/core/null_deleter.hpp> namespace logging = boost::log; namespace src = boost::log::sources; namespace sinks = boost::log::sinks; void init() { // 创建输出槽 typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink; boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >(); // 添加写文件的流 sink->locked_backend()->add_stream( boost::make_shared< std::ofstream >("sample.log")); // 向输出槽对象中加入标准日志流对象 boost::shared_ptr< std::ostream > stream(&std::clog, boost::null_deleter()); sink->locked_backend()->add_stream(stream); // 把输出槽注册到日志库核心 logging::core::get()->add_sink(sink); } int main(int, char*[]) { init(); src::logger lg; BOOST_LOG(lg) << "Hello world!"; return 0; }
- 控制台输出:
- 日志输出: