Boost日志在使用配置文件时不显示严重性或按严重性过滤

Boost Log not displaying severity or filtering by severity when using config file

本文关键字:严重性 显示 过滤 日志 配置文件 Boost      更新时间:2023-10-16

我一直在尝试使用:

让Boost日志库与配置文件一起工作
boost::log::init_from_stream();

方法。我使用:

BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)

方法来注册严重性,但这似乎没有做任何事情。当我运行代码时,得到以下输出:

1。[]常规消息

2。[]警告严重性消息

3。[]错误严重性消息

。这种严肃性是缺失的。当我添加这行代码时:

boost::log::register_simple_formatter_factory< severity_level, char >("Severity");

它按预期工作,即它像上面一样记录日志,但具有严重性级别。但是,当我尝试在配置文件中按严重性过滤时,它不起作用,并且没有任何内容写入文件,这意味着过滤器不知道"严重性"是什么,因此没有记录与此过滤器匹配。

如何使用init_from_stream方法使Boost日志与严重性和过滤严重性一起工作?

这里是完整的源代码:(改编自Andrey Semashev, http://boost-log.sourceforge.net/libs/log/example/doc/tutorial_filtering.cpp)

enum severity_level
{
    normal,
    notification,
    warning,
    error,
    critical
}; 
std::ostream& operator<< (std::ostream& strm, severity_level level)
{
    static const char* strings[] =
    {
        "normal",
        "notification",
        "warning",
        "error",
        "critical"
    };
if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
    strm << strings[level];
else
    strm << static_cast< int >(level);
return strm;
}
BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
int main(int, char*[])
{
    logging::add_common_attributes();
    //  boost::log::register_simple_formatter_factory< severity_level, char >("Severity"); // when commented out severities are not shown in the log file
    std::ifstream configFile_l("config.ini");
    boost::log::init_from_stream(configFile_l);
    src::severity_logger< severity_level > lg_l;
    BOOST_LOG_SEV(lg_l, normal) << "A regular message";
    BOOST_LOG_SEV(lg_l, warning) << "A warning severity message";
    BOOST_LOG_SEV(lg_l, error) << "An error severity message";

    return 0;
}

配置文件是这样的:

[下沉。满)

目标=文本文件

文件名= full.log

= " % LineID %

格式。(%严重性%)%消息%"

Filter="%Severity%> 3" #也尝试过Filter="%Severity%> info/error等…

你应该加上

boost::log::register_simple_formatter_factory< severity_level, char >("Severity");
在调用add_common_attributes方法之前在main函数中

。问候。

所以答案最终来自于添加:

boost::log::register_simple_formatter_factory< severity_level, char >("Severity");

aleksandrm8建议。

然后是错误:

‘sizeof’ to incomplete type ‘boost::STATIC_ASSERTION_FAILURE

通过添加以下操作符重载来解决:

template< typename CharT, typename TraitsT >
inline std::basic_istream< CharT, TraitsT >& operator>> (
    std::basic_istream< CharT, TraitsT >& strm, severity_level& lvl)
{
    int n = normal;
    strm >> n;
    if (n >= normal && n <= critical)
        lvl = static_cast< severity_level >(n);
    else
        lvl = normal;
    return strm;
}

在这里找到:http://sourceforge.net/p/boost-log/discussion/710021/thread/2b0325f8。我认为可能其他操作符重载可能需要其他过滤,信息可以在这里找到:http://www.boost.org/doc/libs/1_55_0/libs/log/doc/html/log/extension/settings.html#log.extension.settings.adding_support_for_user_defined_types_to_the_filter_parser