日志源有宽字节版、单字节版、多线程版、单线程版、带严重等级的版本
下面这行代码创建了一个宽字符、多线程安全、有日志严重等级的日志源:
boost::log::sources::wseverity_logger_mt log;
可以给每个日志源设置属性,也可以设置全局的日志属性,这里属性的的作用是为了给日志记录添加属性,比如给日志源log添加了属性A,那么用日志源log生成的日志记录都会有A这个属性,后面的日志过滤和日志格式化,都可以围绕这个属性来进行。
boost::log::core::get()->add_global_attribute("DateTime", boost::log::attributes::local_clock());
log.add_attribute("LoggerName", boost::log::attributes::constant
日志后端是日志数据最终保存的地方,这个保存的地方可以是文件,也可以是终端。
如果是文本文件的话,可以设置日志文件名的生成模式、日志文件有多少个、单个日志文件最大多少个字节、日志保存的位置等等.
可以设置全局的过滤器,也可以为每个后端日志槽设置过滤器。日志记录会先经过全局过滤器,然后才是每个后端(backend)的每个日志槽(sink),没有被过滤掉的日志记录会经过格式化后写到文件或终端上。
日志记录除了日志文本外,还有一些属性,比如时间,线程ID,进程文件名,自定义的属性等,这些属性也可以写道文件中去,这些属性该怎么输出就需要指定格式化函数
使用日志源来生成日志记录。
//使用全局的日志源来生成日志记录
BOOST_LOG_TRIVIAL(warning) << "warning log";
//使用自定义的日志源来生成日志记录
BOOST_LOG_SEV(log, boost::log::trivial::info) << "this is info log message";
BOOST_LOG(log) << "hello world";
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- using namespace std;
- namespace expr = boost::log::expressions;
-
- int main(int, char*[])
- {
- // boost::log::add_common_attributes();
- // boost::log::core::get()->remove_all_sinks();
- boost::log::sources::severity_logger
l1(boost::log::trivial::severity_level::info) ; - boost::log::sources::logger l2;
- boost::log::sources::wseverity_logger_mt log3;
- boost::log::add_console_log(
- cout,
- boost::log::keywords::format =
- (
- expr::stream
- // << expr::if_ (expr::has_attr< string >("filename"))
- // [
- // expr::stream << "[" << expr::attr< string >("filename") << "]"
- // ]
- // << expr::if_ (expr::has_attr< string >("function"))
- // [
- // expr::stream << "[" << expr::attr< string >("function") << "]"
- // ]
- << expr::if_(expr::has_attr
("Duration")) - [
- expr::stream << "[" << expr::attr
("Duration") << "]" - ]
- << expr::if_(expr::has_attr
("Process")) - [
- expr::stream << "[" << expr::attr
("Process")<< "]" - ]
- << expr::if_(expr::has_attr
("ThreadId")) - [
- expr::stream << "[" << expr::attr
("ThreadId")<< "]" - ]
- << expr::if_(expr::has_attr
("DateTime")) - [
- expr::stream << "[" << expr::format_date_time< boost::posix_time::ptime >("DateTime", "%Y-%m-%d %H:%M:%S.%f") << "] "
- ]
- << expr::if_(expr::has_attr
("MyFuncAttr")) - [
- expr::stream << "[" << expr::attr
("MyFuncAttr") << "] " - ]
- << expr::if_(expr::has_attr
("LoggerName")) - [
- expr::stream << "[" << expr::attr
("LoggerName") << "] " - ]
- << "[" << boost::log::trivial::severity << "] "
- << expr::smessage
- )
- );
- boost::log::core::get()->add_global_attribute("DateTime", boost::log::attributes::local_clock());
- boost::log::core::get()->add_global_attribute("Process", boost::log::attributes::current_process_name());
- boost::log::core::get()->add_global_attribute("ThreadId", boost::log::attributes::current_thread_id());
- l1.add_attribute("filename", boost::log::attributes::constant
(__FILE__)); - l1.add_attribute("function", boost::log::attributes::constant
(__FUNCTION__)); - l1.add_attribute("Duration", boost::log::attributes::timer());
- l1.add_attribute("MyFuncAttr",boost::log::attributes::make_function([]()->string{return "XXX";}));
- l1.add_attribute("LoggerName", boost::log::attributes::constant
("Logger_001")); - boost::shared_ptr
pBackend = boost::make_shared(); - boost::shared_ptr
pStream(&cout, boost::null_deleter()) ; - boost::shared_ptr
pFileStream(new std::ofstream("sample.log")) ; - pBackend->add_stream(pFileStream);
- pBackend->auto_flush(true);
-
- using sink_t = boost::log::sinks::synchronous_sink
; - boost::shared_ptr<sink_t> pSink = boost::make_shared<sink_t>(pBackend);
- boost::log::core::get()->add_sink(pSink);
-
- boost::shared_ptr
pTxtFileBackend = boost::make_shared( - boost::log::keywords::file_name = "MyLogs/file.log", //是日志文件大小没达到最大值时,日志写的文件
- boost::log::keywords::target_file_name = "file_%2N.log", //当日志文件达到最大值时,日志文件名会被改写,这里的%2N,表示用两位数字表示
- boost::log::keywords::rotation_size = 5 * 1024 * 1024, //单个日志文件最大值,这里是5M
- boost::log::keywords::open_mode = std::ios_base::out | std::ios_base::app, //添加模式
- boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(12, 0, 0)); //每天12点会重新生成一个日志文件,不管日志文件是否到了最大值
- using sink_t2 = boost::log::sinks::synchronous_sink
; - boost::shared_ptr
pSink2 = boost::make_shared(pTxtFileBackend); - pSink2->locked_backend()->set_file_collector(
- boost::log::sinks::file::make_collector(
- boost::log::keywords::max_files = 10, //最多10个日志文件
- boost::log::keywords::max_size = 1024*1024*51, //正目录最大占用多少磁盘空间
- boost::log::keywords::target = "MyLogs" //日志保存的目录,前面target_file_name所在的目录
- ));
- pSink2->locked_backend()->scan_for_files();
- boost::log::core::get()->add_sink(pSink2);
- pSink2->set_filter(
- boost::log::trivial::severity >= boost::log::trivial::warning &&
- boost::log::expressions::has_attr("LoggerName") && boost::log::expressions::attr
("LoggerName") == "Logger_001" - );
-
- sleep(1);
-
- for(int i = 0; i < 1; i++){
- BOOST_LOG_FUNCTION();
- BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
- BOOST_LOG_TRIVIAL(warning) << "warning log";
- BOOST_LOG_SEV(l1, boost::log::trivial::fatal) << "this is fatal log message";
- BOOST_LOG_SEV(l1, boost::log::trivial::info) << "this is info log message";
- BOOST_LOG(l2) << "BBB: hello world";
- BOOST_LOG(l1) << "这是默认严重等级(info)的日志";
- }
- return 0;
- }