在日志消息中提升日志严重性为空

boost log severity empty in the log message

我正在设置日志记录的格式化程序,严重性字段在最终日志消息中打印为空。代码编译正常但没有按预期工作。请指教。 boost 日志上的文档非常含糊不清。

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

问题是您没有添加严重级别属性。此属性通常由记录器提供,但您使用的是 logger_mt 记录器,它不添加任何属性并忽略您提供给 BOOST_LOG_SEV 宏的参数。您应该使用 severity_logger_mt 或其他提供严重级别支持的记录器。此记录器会将严重级别属性添加到通过它创建的每条日志记录,并将级别设置为您在 BOOST_LOG_SEV 宏调用中指定的值。