c++ - boost 日志,为什么不打印 threadid 和 processid

标签 c++ logging boost

我在使用boost日志库时遇到问题,如下:

BOOST_LOG_ATTRIBUTE_KEYWORD(log_severity, "Severity", SeverityLevel)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_uptime, "Uptime", attrs::timer::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_scope, "Scope", attrs::named_scope::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(thread_id, "ThreadID", unsigned int)
BOOST_LOG_ATTRIBUTE_KEYWORD(process_id, "ProcessID", unsigned int)

    auto file_sink=logging::add_file_log
        (
        keywords::file_name="%Y-%m-%d_%N.log",     
        keywords::rotation_size=100*1024*1024,       
        keywords::time_based_rotation=sinks::file::rotation_at_time_point(0,0,0)   
        );



    file_sink->set_filter(log_severity>= Log_Info);   

    file_sink->locked_backend()->scan_for_files();

    logging::formatter formatter = expr::stream << "[" << log_timestamp << "] [" << line_id << "] [" << log_scope << "] [" << log_severity << "] [" << thread_id << "] [" << process_id << "] " << expr::message;

    file_sink->set_formatter(formatter);

    file_sink->locked_backend()->auto_flush(true);
    logging::core::get()->add_sink(console_sink);
    logging::add_common_attributes();

我对mian函数的调用如下:

src::severity_logger<SeverityLevel>lg;
BOOST_LOG_FUNCTION();
BOOST_LOG_SEV(lg, Log_Info) << "====main";

输出信息:

enter image description here

最佳答案

您不必创建自定义属性关键字 ThreadIDProcessIDTimeStamp。只需使用 common attributes 构造格式化程序即可如下:

logging::formatter formatter = expr::stream << 
    "[" << log_timestamp << 
    "] [" << line_id << 
    "] [" << log_scope << 
    "] [" << log_severity <<
    "] [" << logging::expressions::attr<logging::attributes::current_thread_id::value_type>("ThreadID") << 
    "] [" << logging::expressions::attr<logging::attributes::current_process_id::value_type>("ProcessID") << 
    "] " << expr::message;

但是正如 Andrey 指出的那样,如果您仍想创建已经存在的自定义关键字,则必须指定它们的类型:

BOOST_LOG_ATTRIBUTE_KEYWORD(thread_id, "ThreadID", logging::attributes::current_thread_id::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(process_id, "ProcessID", logging::attributes::current_process_id::value_type)

工作示例是 here .

关于c++ - boost 日志,为什么不打印 threadid 和 processid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337337/

相关文章:

python - 为什么traceback.format_exception需要traceback参数而不是使用ex.__traceback__?

java - 如何为 Google App Engine Java 自定义日志记录?

c++ - 模板问题 ('typename' 因为不是模板函数参数)

c++ - dlldata.c不是在win32下生成的?

c++ - 有没有办法将 std::variant 与任意多个定义的类型一起使用?

c++ - 如何从 QT C++ 中的对话框将 key 发送到父 mainWindow

c++ - 多个 emplace_back 额外调用复制构造函数

python - 为什么 python 日志记录模块使用旧的字符串格式?

C++:有没有办法让这个反射宏与 IntelliSense 一起工作?

Boost 读/写 XML 文件 : how to change the characters encoding?