我正在设置日志记录的格式化程序,严重性字段在最终日志消息中打印为空。代码编译良好但不按预期工作。请指教。关于升压日志的文档非常神秘且不清楚。
auto logFmt = (
boost::log::expressions::stream
<<boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d:%H:%M:%S")
<<"|"<< boost::log::expressions::attr<boost::log::trivial::severity_level>("Severity")
<<"|pid:"<<boost::log::expressions::attr<boost::log::process_id>("ProcessID")
<<"|tid:"<<boost::log::expressions::attr<boost::log::thread_id>("ThreadID")
<<"| "<<boost::log::expressions::smessage);
我正在粘贴独立的代码片段,它与较大的片段分开。
#ifndef BOOST_LOG_USE_NATIVE_SYSLOG
#define BOOST_LOG_USE_NATIVE_SYSLOG
#endif
#include <string>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <syslog.h>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/phoenix.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/basic_sink_backend.hpp>
#include <boost/log/sinks/syslog_backend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/attributes/value_visitation.hpp>
#include <boost/log/utility/manipulators/add_value.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/attributes/timer.hpp>
#include <boost/log/attributes/named_scope.hpp>
#include <boost/log/support/date_time.hpp>
#define __LOCATION_TAG__ <<__FILE__<<"|"<<__LINE__<<"|"<<__FUNCTION__<<"()|"<<" "
#define _fatal BOOST_LOG_SEV(Logger::getLogger(), boost::log::v2_mt_posix::trivial::fatal)__LOCATION_TAG__
#define _error BOOST_LOG_SEV(Logger::getLogger(), boost::log::v2_mt_posix::trivial::error)__LOCATION_TAG__
#define _warning BOOST_LOG_SEV(Logger::getLogger(), boost::log::v2_mt_posix::trivial::warning)__LOCATION_TAG__
#define _info BOOST_LOG_SEV(Logger::getLogger(), boost::log::v2_mt_posix::trivial::info)__LOCATION_TAG__
#define _debug BOOST_LOG_SEV(Logger::getLogger(), boost::log::v2_mt_posix::trivial::debug)__LOCATION_TAG__
#define _trace BOOST_LOG_SEV(Logger::getLogger(), boost::log::v2_mt_posix::trivial::trace)__LOCATION_TAG__
class Logger
{
class custom_backend :
public boost::log::sinks::basic_sink_backend<
boost::log::sinks::combine_requirements<
boost::log::sinks::formatted_records,
boost::log::sinks::concurrent_feeding,
boost::log::sinks::flushing
>::type
>
{
public:
typedef char char_type; // Character type.
typedef std::basic_string< char_type > string_type; // Formatted string type.
explicit custom_backend()
{
return;
}
void consume(boost::log::record_view const& rec,
string_type const& formatted_message)
{
std::cerr<<formatted_message<<std::endl;
return;
}
void flush()
{
return;
}
};
public:
typedef boost::log::sinks::synchronous_sink< custom_backend > sink_t;
void initialize(std::string tag)
{
auto logFmt = (
boost::log::expressions::stream
<<boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d:%H:%M:%S")
<<"|"<< boost::log::expressions::attr<boost::log::trivial::severity_level>("Severity")
<<"|pid:"<<boost::log::expressions::attr<boost::log::process_id>("ProcessID")
<<"|tid:"<<boost::log::expressions::attr<boost::log::thread_id>("ThreadID")
<<"| "<<boost::log::expressions::smessage);
cbackend.reset(new custom_backend());
custom_sink.reset(new sink_t(cbackend));
custom_sink->set_formatter(logFmt);
boost::log::core::get()->add_sink(custom_sink);
return;
}
static Logger& getInstance()
{
static Logger logger;
return logger;
}
void finalize()
{
return;
}
static boost::log::sources::logger_mt & getLogger()
{
return _lg;
}
private:
static boost::log::sources::logger_mt _lg;
boost::shared_ptr< custom_backend > cbackend;
boost::shared_ptr< sink_t > custom_sink;
};
boost::log::sources::logger_mt Logger::_lg;
int main(int ac, char **av)
{
auto lg = Logger::getInstance();
lg.initialize("hello");
_info<<"hello world."<<std::endl;
_error<<"hello world."<<std::endl;
return 0;
}
答案 0 :(得分:2)
问题是您没有添加严重性级别属性。此属性通常由记录器提供,但您使用的是logger_mt
记录器,它不会添加任何属性并忽略您为BOOST_LOG_SEV
宏提供的参数。您应该使用severity_logger_mt
或其他一些提供严重性级别支持的记录器。此记录器将严重性级别属性添加到通过它创建的每个日志记录中,并将级别设置为您在BOOST_LOG_SEV
宏调用中指定的值。