Boost.log手动创建文本文件后端

时间:2016-03-21 08:51:47

标签: boost-log

Logger.h

enum ChannelType {
    main_channel,
    sub_channel
};
BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", ChannelType)

class Logger {
public:
    enum severity_level
    {
        debug,
        info,
        warning,
        error,
        fatal
    };
    static void init(Logger::severity_level level);
    typedef boost::log::sources::severity_channel_logger< Logger::severity_level, ChannelType > logger_type;
    static thread_local logger_type gl;
    static thread_local logger_type motion_checker;
};

Logger.cpp

    void Logger::init(Logger::severity_level level)
    {
        boost::shared_ptr<boost::log::core> core = boost::log::core::get();
        core->set_exception_handler(boost::log::make_exception_suppressor());
        core->set_filter(boost::log::expressions::attr<Logger::severity_level>("Severity") >= level);
        {
            typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_multifile_backend> multifile_sink;
            boost::shared_ptr<multifile_sink> sink(new multifile_sink);
            sink->locked_backend()->set_file_name_composer(boost::log::sinks::file::as_file_name_composer(
                boost::log::expressions::stream << "./log/Sub/" << boost::log::expressions::attr< std::string >("RoleName") << ".log"));
            sink->set_formatter
                (
                    boost::log::expressions::format("[%1%] - %2%")
                    % boost::log::expressions::attr< boost::posix_time::ptime >("TimeStamp")
                    % boost::log::expressions::smessage
                    );
            sink->set_filter(channel == sub_channel);
            core->add_sink(sink);
        }
//============use add_file_log==============
        boost::log::add_file_log(
            boost::log::keywords::filter = channel == main_channel,
            boost::log::keywords::file_name = "./log/%Y%m%d.log",
            boost::log::keywords::auto_flush = true,
            boost::log::keywords::open_mode = (std::ios::out | std::ios::app),
            boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
            boost::log::keywords::format = "[%TimeStamp%] (%LineID%) {%ThreadID%}: %Message%"
            );
//==================use add_file_log end====================
//=========manually add sink, this has problem================
        boost::shared_ptr< boost::log::sinks::text_file_backend > backend =
            boost::make_shared< boost::log::sinks::text_file_backend >(
                boost::log::keywords::filter = channel == main_channel,
                boost::log::keywords::file_name = "./log/%Y%m%d.log",
                boost::log::keywords::auto_flush = true,
                boost::log::keywords::open_mode = (std::ios::out | std::ios::app),
                boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
                boost::log::keywords::format = "[%TimeStamp%] (%LineID%) {%ThreadID%}: %Message%"
                );
        typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_file_backend> file_sink;
        boost::shared_ptr< file_sink > sink(new file_sink(backend));
        core->add_sink(sink);
//============manually add end==========================
        boost::log::add_common_attributes();
    }

    thread_local Logger::logger_type Logger::gl(boost::log::keywords::channel = main_channel);
    thread_local Logger::logger_type Logger::motion_checker(boost::log::keywords::channel = sub_channel);

的main.cpp

int main()
{
    Logger::init(Logger::debug);
    BOOST_LOG_SEV(Logger::gl, Logger::debug) << "ssss";
    {
        BOOST_LOG_SCOPED_LOGGER_ATTR(Logger::motion_checker, "RoleName", boost::log::attributes::constant< std::string >("aaa"))
        BOOST_LOG_SEV(Logger::motion_checker, Logger::debug) << "aaaa";
    }
    system("pause");
    return 0;
}

使用add_file_log,一切都很好,但手动添加接收器丢失了所有属性,只有消息在那里。并且过滤器也不起作用,sub_channel消息将添加到此文件中。我手动创建此文件接收的原因是因为add_file_log创建了同步接收器,我想要一个异步接收器。

1 个答案:

答案 0 :(得分:1)

问题是,命名参数formatfilter由接收器前端使用,而不是后端,并将它们传递给后端。因此,既没有设置过滤器也没有设置格式化器。

我建议你将所有命名参数移到sink前端构造函数中,让它将参数传递给后端构造函数,而不是自己构造后端。

    typedef boost::log::sinks::asynchronous_sink<boost::log::sinks::text_file_backend> file_sink;
    boost::shared_ptr< file_sink > sink = boost::make_shared< file_sink >(
            boost::log::keywords::filter = channel == main_channel,
            boost::log::keywords::file_name = "./log/%Y%m%d.log",
            boost::log::keywords::auto_flush = true,
            boost::log::keywords::open_mode = (std::ios::out | std::ios::app),
            boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0),
            boost::log::keywords::format = "[%TimeStamp%] (%LineID%) {%ThreadID%}: %Message%"
    );
    core->add_sink(sink);