c++ - 如何从配置中设置 boost 日志记录严重性级别?

标签 c++ boost boost-log

我希望能够从配置中指定日志严重级别:

# my config file
LogSeverity = info

我该怎么做?目前我的 main 函数中有这样的东西:

// logging settings
boost::log::trivial::severity_level logSeverity;
boost::program_options::options_description loggingSettings                     
    ("Logging settings");                                                       
loggingSettings.add_options()                                                   
("LogSeverity", value<boost::log::trivial::severity_level>(&logSeverity)       
    ->required(),                                                               
        "log level to output");

variables_map vm;
store(parse_config_file(configFilestream, loggingSettings), vm);
notify(vm);

boost::log::core::get()->set_filter(                                         
    boost::log::trivial::severity >= logSeverity);
BOOST_LOG_TRIVIAL(info) << "severity " << logSeverity;

这个程序的输出是:

[2015-05-18 09:58:40.783298] [0x000007f017445078] [info] severity trace

但是,我在我的配置中将严重性设置为 info(如上所述),那么为什么它被设置为 trace

最佳答案

完整的工作示例:

默认配置文件:

# my config file
LogSeverity = info

主要.cpp:

#include <string>
#include <fstream>

#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/program_options.hpp>

int main()
{
    // logging settings
    boost::log::trivial::severity_level logSeverity;
    boost::program_options::options_description loggingSettings
    ("Logging settings");
    loggingSettings.add_options()
    ("LogSeverity", boost::program_options::value<boost::log::trivial::severity_level>(&logSeverity)
    ->required(),
    "log level to output");

    std::ifstream conf_file("./default.conf");
    if (!conf_file)
        return 1;

    boost::program_options::variables_map variables_map;
    boost::program_options::store(boost::program_options::parse_config_file(conf_file, loggingSettings), variables_map);
    boost::program_options::notify(variables_map);

    boost::log::core::get()->set_filter(
    boost::log::trivial::severity >= logSeverity);
    BOOST_LOG_TRIVIAL(info) << "severity " << logSeverity;

    return 0;
}

输出:

[2015-05-19 01:22:57.666571] [0xc000027d] [info]    severity info

关于c++ - 如何从配置中设置 boost 日志记录严重性级别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30293400/

相关文章:

c++ - 两个控制台应用程序之间的管道?

c++ - boost::asio 缓冲区不可能将参数从 char 转换为 const mutable_buffer&

C++ Http POST 400 错误请求

c++ - 什么会导致应用程序在工作几个小时后因 SIGABRT 获取锁而失败?

c++ - boost log 打印源代码文件名和行号

c++ - 使用 boost::log 输出用户定义的结构

c++ - 修剪:什么时候停止?

c++ - 一条定义规则警告

c++ - 使用 CFITSIO 库从 FITS 表中读取可变长度数组

c++ - 如何为具有共享指针的结构的多索引制作修饰符以 boost 记录器后端以重置此后端?