• Boost之Log: (2)、代码练习


    1、创建日志源(Logging source)

           日志源有宽字节版、单字节版、多线程版、单线程版、带严重等级的版本

            下面这行代码创建了一个宽字符、多线程安全、有日志严重等级的日志源:

                            boost::log::sources::wseverity_logger_mt log;

    2、设置日志源属性

            可以给每个日志源设置属性,也可以设置全局的日志属性,这里属性的的作用是为了给日志记录添加属性,比如给日志源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("Logger_001"));

    3、创建日志后端(backend)

            日志后端是日志数据最终保存的地方,这个保存的地方可以是文件,也可以是终端。

           如果是文本文件的话,可以设置日志文件名的生成模式、日志文件有多少个、单个日志文件最大多少个字节、日志保存的位置等等.

    4、设置日志过滤器

            可以设置全局的过滤器,也可以为每个后端日志槽设置过滤器。日志记录会先经过全局过滤器,然后才是每个后端(backend)的每个日志槽(sink),没有被过滤掉的日志记录会经过格式化后写到文件或终端上。

    5、设置日志格式

            日志记录除了日志文本外,还有一些属性,比如时间,线程ID,进程文件名,自定义的属性等,这些属性也可以写道文件中去,这些属性该怎么输出就需要指定格式化函数

    6、写日志

            使用日志源来生成日志记录。

    //使用全局的日志源来生成日志记录

                  BOOST_LOG_TRIVIAL(warning) << "warning log";

    //使用自定义的日志源来生成日志记录

              BOOST_LOG_SEV(log, boost::log::trivial::info) << "this is info log message";

              BOOST_LOG(log) << "hello world";

    7、参考代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. using namespace std;
    22. namespace expr = boost::log::expressions;
    23. int main(int, char*[])
    24. {
    25. // boost::log::add_common_attributes();
    26. // boost::log::core::get()->remove_all_sinks();
    27. boost::log::sources::severity_logger l1(boost::log::trivial::severity_level::info);
    28. boost::log::sources::logger l2;
    29. boost::log::sources::wseverity_logger_mt log3;
    30. boost::log::add_console_log(
    31. cout,
    32. boost::log::keywords::format =
    33. (
    34. expr::stream
    35. // << expr::if_ (expr::has_attr< string >("filename"))
    36. // [
    37. // expr::stream << "[" << expr::attr< string >("filename") << "]"
    38. // ]
    39. // << expr::if_ (expr::has_attr< string >("function"))
    40. // [
    41. // expr::stream << "[" << expr::attr< string >("function") << "]"
    42. // ]
    43. << expr::if_(expr::has_attr("Duration"))
    44. [
    45. expr::stream << "[" << expr::attr("Duration") << "]"
    46. ]
    47. << expr::if_(expr::has_attr("Process"))
    48. [
    49. expr::stream << "[" << expr::attr("Process")<< "]"
    50. ]
    51. << expr::if_(expr::has_attr("ThreadId"))
    52. [
    53. expr::stream << "[" << expr::attr("ThreadId")<< "]"
    54. ]
    55. << expr::if_(expr::has_attr("DateTime"))
    56. [
    57. expr::stream << "[" << expr::format_date_time< boost::posix_time::ptime >("DateTime", "%Y-%m-%d %H:%M:%S.%f") << "] "
    58. ]
    59. << expr::if_(expr::has_attr("MyFuncAttr"))
    60. [
    61. expr::stream << "[" << expr::attr("MyFuncAttr") << "] "
    62. ]
    63. << expr::if_(expr::has_attr("LoggerName"))
    64. [
    65. expr::stream << "[" << expr::attr("LoggerName") << "] "
    66. ]
    67. << "[" << boost::log::trivial::severity << "] "
    68. << expr::smessage
    69. )
    70. );
    71. boost::log::core::get()->add_global_attribute("DateTime", boost::log::attributes::local_clock());
    72. boost::log::core::get()->add_global_attribute("Process", boost::log::attributes::current_process_name());
    73. boost::log::core::get()->add_global_attribute("ThreadId", boost::log::attributes::current_thread_id());
    74. l1.add_attribute("filename", boost::log::attributes::constant(__FILE__));
    75. l1.add_attribute("function", boost::log::attributes::constant(__FUNCTION__));
    76. l1.add_attribute("Duration", boost::log::attributes::timer());
    77. l1.add_attribute("MyFuncAttr",boost::log::attributes::make_function([]()->string{return "XXX";}));
    78. l1.add_attribute("LoggerName", boost::log::attributes::constant("Logger_001"));
    79. boost::shared_ptr pBackend = boost::make_shared();
    80. boost::shared_ptr pStream(&cout, boost::null_deleter());
    81. boost::shared_ptr pFileStream(new std::ofstream("sample.log"));
    82. pBackend->add_stream(pFileStream);
    83. pBackend->auto_flush(true);
    84. using sink_t = boost::log::sinks::synchronous_sink;
    85. boost::shared_ptr<sink_t> pSink = boost::make_shared<sink_t>(pBackend);
    86. boost::log::core::get()->add_sink(pSink);
    87. boost::shared_ptr pTxtFileBackend = boost::make_shared(
    88. boost::log::keywords::file_name = "MyLogs/file.log", //是日志文件大小没达到最大值时,日志写的文件
    89. boost::log::keywords::target_file_name = "file_%2N.log", //当日志文件达到最大值时,日志文件名会被改写,这里的%2N,表示用两位数字表示
    90. boost::log::keywords::rotation_size = 5 * 1024 * 1024, //单个日志文件最大值,这里是5M
    91. boost::log::keywords::open_mode = std::ios_base::out | std::ios_base::app, //添加模式
    92. boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(12, 0, 0)); //每天12点会重新生成一个日志文件,不管日志文件是否到了最大值
    93. using sink_t2 = boost::log::sinks::synchronous_sink;
    94. boost::shared_ptr pSink2 = boost::make_shared(pTxtFileBackend);
    95. pSink2->locked_backend()->set_file_collector(
    96. boost::log::sinks::file::make_collector(
    97. boost::log::keywords::max_files = 10, //最多10个日志文件
    98. boost::log::keywords::max_size = 1024*1024*51, //正目录最大占用多少磁盘空间
    99. boost::log::keywords::target = "MyLogs" //日志保存的目录,前面target_file_name所在的目录
    100. ));
    101. pSink2->locked_backend()->scan_for_files();
    102. boost::log::core::get()->add_sink(pSink2);
    103. pSink2->set_filter(
    104. boost::log::trivial::severity >= boost::log::trivial::warning &&
    105. boost::log::expressions::has_attr("LoggerName") && boost::log::expressions::attr("LoggerName") == "Logger_001"
    106. );
    107. sleep(1);
    108. for(int i = 0; i < 1; i++){
    109. BOOST_LOG_FUNCTION();
    110. BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
    111. BOOST_LOG_TRIVIAL(warning) << "warning log";
    112. BOOST_LOG_SEV(l1, boost::log::trivial::fatal) << "this is fatal log message";
    113. BOOST_LOG_SEV(l1, boost::log::trivial::info) << "this is info log message";
    114. BOOST_LOG(l2) << "BBB: hello world";
    115. BOOST_LOG(l1) << "这是默认严重等级(info)的日志";
    116. }
    117. return 0;
    118. }

  • 相关阅读:
    在数字海洋中驶向成功:公众号关键词排名优化的旅程
    【C++】用命名空间避免命名冲突
    引用特定条件下的第一条数据
    网络基础(1)
    RK3588 linux内核中断之下半部-线程化(三)
    麒麟KYLINOS上使用开始菜单图标开启或关闭vnc
    算网协同对算力产业发展的影响分析
    vue中渲染器原理
    R Markdown 格式
    mac连接easyconnnect显示“本地环境出现异常”
  • 原文地址:https://blog.csdn.net/qq_33271629/article/details/137282639