我看过格式化程序的几个例子,但那些使用了自定义属性。
如何指定severity_channel_logger
的格式?我现在得到的是:
[2015-07-20 11:53:09.879305] [0x00007f047b525740] [trace] Bla, bla bla, bla bla bla.
[2015-07-20 11:53:09.879370] [0x00007f047b525740] [trace] Bla, bla bla, bla bla bla.
[2015-07-20 11:53:09.879376] [0x00007f047b525740] [fatal] Bla, bla bla, bla bla bla.
logger.h
#ifndef LOGGER_H_
#define LOGGER_H_
#include <boost/log/trivial.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/channel_feature.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <string>
namespace Logging {
typedef boost::log::sources::severity_channel_logger<boost::log::trivial::severity_level,std::string> LoggerType;
BOOST_LOG_GLOBAL_LOGGER(clogger,LoggerType)
} // namespace Logging
#endif
logger.cpp
#include "logger.h"
#include <memory>
#include <boost/log/core/core.hpp>
#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/core/null_deleter.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
using namespace Logging;
BOOST_LOG_GLOBAL_LOGGER_INIT(clogger, LoggerType)
{
boost::log::sources::severity_channel_logger< boost::log::trivial::severity_level, std::string > lg;
return lg;
}
的main.cpp
#include "logger.h"
#include <boost/log/attributes/named_scope.hpp>
using namespace Logging;
int main()
{
BOOST_LOG_FUNCTION();
BOOST_LOG(clogger::get()) << "Bla, bla bla, bla bla bla.";
BOOST_LOG_CHANNEL(clogger::get(),"standard") << "Bla, bla bla, bla bla bla.";
BOOST_LOG_CHANNEL_SEV(clogger::get(),"standard",boost::log::trivial::fatal) << "Bla, bla bla, bla bla bla.";
}
答案 0 :(得分:1)
您需要将格式化程序设置为接收器。格式化程序可以在multiple ways中定义,但底线是它从日志记录中提取属性值并将它们放入字符串中。属性是由用户还是库定义并不重要 - 您只需要属性名称及其值的类型。
severity_channel_logger
是一个复合记录器,它在通过该记录器创建的日志记录中插入severity level和channel。因此,您可以从文档中了解到属性名称分别是“Severity”和“Channel”,属性值类型是您指定为记录器模板参数的类型 - boost::log::trivial::severity_level
和{{1在你的情况下。
现在,您可以将所需的格式化程序设置为接收器。例如:
std::string
答案 1 :(得分:0)
实际上,#include <fstream>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
void init()
{
typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
sink->locked_backend()->add_stream(
boost::make_shared< std::ofstream >("sample.log"));
sink->set_formatter
(
expr::stream
// line id will be written in hex, 8-digits, zero-filled
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S")
<< std::hex << std::setw(8) << std::setfill('0') << expr::attr< unsigned int >("LineID")
<< ": <" << logging::trivial::severity
<< "> " << expr::smessage
);
logging::core::get()->add_sink(sink);
}
int main()
{
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;
中有很多介绍。现在我会给我代码:
sample.log
}
根据我的代码,您将在timestamp
中获得格式输出。它将显示log level
和2015-07-20, 23:19:0200000001: <trace> A trace severity message
2015-07-20, 23:19:0200000002: <debug> A debug severity message
2015-07-20, 23:19:0200000003: <info> An informational severity message
2015-07-20, 23:19:0200000004: <warning> A warning severity message
2015-07-20, 23:19:0200000005: <error> An error severity message
2015-07-20, 23:19:0200000006: <fatal> A fatal severity message
。有关详细信息,请访问网站[http://www.boost.org/doc/libs/1_58_0/libs/log/example/doc/tutorial_fmt_format.cpp]。我希望这可以帮到你。顺便说一句,输出是:
<matplotlib.figure.Figure at 0x1c4150890>