Boost log库的使用

1. Boost的log库最简单的用法

#include <boost/log/trivial.hpp>

void log_test()
{
    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";
}

这里写图片描述

2. log库中使用到的头文件和名字空间

#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;

3. 日志过滤器

只有大于等于设置的日志级别,才会输出。

//将此设置过滤器函数放在日志使用之前
void set_filter()
{
    //filter,日志过滤
    logging::core::get()->set_filter
        (
        logging::trivial::severity >= logging::trivial::info  //日志级别>=info才输出
        );
}

4. 输出到文件

void log_add_file()
{
    //输出到文件,并设置日志格式
    logging::add_file_log
        (
        keywords::file_name = "sample.log",
        /*keywords::rotation_size = 10 * 1024 * 1024,
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), */
        keywords::format = "[%TimeStamp%]: %Message%"

        );
}

5. 实例

/*
*          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  //0 代表off, 1代表on

//[ 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%], threadID: %ThreadID%, Message: %Message%"    /*< log record format >*/
        /*需要add_common_attributes()来初始化*/
        );

    logging::core::get()->set_filter
        (
        logging::trivial::severity >= logging::trivial::info
        );
}
//]

#endif

int main(int, char*[])
{
    init();

    /*!
    * \brief Simple attribute initialization routine
    *
    * The function adds commonly used attributes to the logging system. Specifically, the following
    * attributes are registered globally:
    *
    * \li LineID - logging records counter with value type <tt>unsigned int</tt>
    * \li TimeStamp - local time generator with value type <tt>boost::posix_time::ptime</tt>
    * \li ProcessID - current process identifier with value type
    *     <tt>attributes::current_process_id::value_type</tt>
    * \li ThreadID - in multithreaded builds, current thread identifier with
    *     value type <tt>attributes::current_thread_id::value_type</tt>
    */
    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;
}

6. boost log架构

这里写图片描述

  1. 左边是 log source, 负责从程序中收集日志信息,如下:
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";  
  1. 右边是sink, 负责处理日志信息,决定日志输出到哪里,怎么使用。
  2. 中间的logging core将它们连接在一起。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值