Boost log庫的使用
阿新 • • 發佈:2019-02-11
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架構
- 左邊是 log source, 負責從程式中收集日誌資訊,如下:
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
- 右邊是sink, 負責處理日誌資訊,決定日誌輸出到哪裡,怎麼使用。
- 中間的logging core將它們連線在一起。