我在这个函数中使用了 boost::log
:
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
void InitLog() {
logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::debug);
logging::add_file_log(
keywords::file_name = AppHolder::Instance().config().log_folder + "/sign_%Y-%m-%d_%H-%M-%S.%N.log",
keywords::rotation_size = 10 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format = "%TimeStamp% (%LineID%) <%Severity%>: %Message%"
);
}
然后,调用:
BOOST_LOG_TRIVIAL(info) << "thread id: " << this_thread::get_id() << " Initialization succeeded";
输出不符合预期:timestamp、line id 和severity 为空。
() <>: thread id: 7f58e30e8740 Initialization succeeded
最佳答案
您必须向日志核心注册这些属性。像这样:
boost::log::add_common_attributes();
或手动:
boost::log::core::get()->add_global_attribute("TimeStamp", boost::log::attributes::local_clock());
// etc...
关于c++ - 为什么我的格式在 boost 日志中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20352584/