Setting up sinks
有时候,trivial(简单的)logging并不能满足要求。例如,想要更精细的日志处理,而不是简单地打印出来。为此,必须构建自定义的sinks,并且将它们注册到core中。这通常只在程序的开始执行一次。
注意:
特别要提醒的是,在前面的章节中我们并没有初始化任何sinks,但trivial logging任然能够正常运行。这是因为,在用户没有设置任何sinks时,log库会使用一个默认的sink。这个默认的sink一般来说是将日志按照固定的格式打印到console,就像我们在前面所看到的一样。提供这样一个默认sink,是为了在没有做任何初始化时,trivial logging也能立即使用。一旦core中添加了任何其他的sinks,默认的sink就不再起作用。但trivial logging的宏任然能够使用。
File logging unleashed
作为开始,下面是如何初始化一个log,让日志记录出输出到一个文件:
void init() { logging::add_file_log("sample.log"); logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info ); }
增加的一行是 add_file_log 函数。正如名字所示,这个函数初始化一个logging sink,使其将日志记录写到一个文件中。这个函数还接受一些自定义选项,例如文件rotation(轮转)周期和大小限制。例如:
void init() { logging::add_file_log ( keywords::file_name = "sample_%N.log", //1 keywords::rotation_size = 10 * 1024 * 1024, //2 keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), //3 keywords::format = "[%TimeStamp%]: %Message%" //4 ); logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info ); }
1. 文件名模板
2. rotation在每个文件到达10M
3. ... 或到达0点
4. 日志格式
完整代码:
/* * Copyright Andrey Semashev 2007 - 2015. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) */ #include <boost/log/core.hpp> #include <boost/log/trivial.hpp> #include <boost/log/expressions.hpp> #include <boost/log/sinks/text_file_backend.hpp> #include <boost/log/utility/setup/file.hpp> #include <boost/log/utility/setup/common_attributes.hpp> #include <boost/log/sources/severity_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; #if 0 //[ example_tutorial_file_simple void init() { logging::add_file_log("sample.log"); logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info ); } //] // We need this due to this bug: https://svn.boost.org/trac/boost/ticket/4416 //[ example_tutorial_file_advanced_no_callouts void init() { logging::add_file_log ( keywords::file_name = "sample_%N.log", keywords::rotation_size = 10 * 1024 * 1024, keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), keywords::format = "[%TimeStamp%]: %Message%" ); logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info ); } //] #else //[ example_tutorial_file_advanced void init() { logging::add_file_log ( keywords::file_name = "sample_%N.log", /*< file name pattern >*/ keywords::rotation_size = 10 * 1024 * 1024, /*< rotate files every 10 MiB... >*/ keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/ keywords::format = "[%TimeStamp%]: %Message%" /*< log record format >*/ ); logging::core::get()->set_filter ( logging::trivial::severity >= logging::trivial::info ); } //] #endif int main(int, char*[]) { init(); logging::add_common_attributes(); using namespace logging::trivial; src::severity_logger< severity_level > lg; BOOST_LOG_SEV(lg, trace) << "A trace severity message"; BOOST_LOG_SEV(lg, debug) << "A debug severity message"; BOOST_LOG_SEV(lg, info) << "An informational severity message"; BOOST_LOG_SEV(lg, warning) << "A warning severity message"; BOOST_LOG_SEV(lg, error) << "An error severity message"; BOOST_LOG_SEV(lg, fatal) << "A fatal severity message"; return 0; }