Boost日志不写入文件,怎么办?

时间:2021-07-10 13:20:33

here is my code:

这是我的代码:

#include <boost/log/trivial.hpp>
#include <boost/move/utility.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;

BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)

void Foo::Some()
{
  std::cout << "TEST";

  src::logger_mt& lg = my_logger::get();

  logging::add_file_log("F:\\sample.log");
  //logging::core::get()->set_filter
  //  (
  //  logging::trivial::severity >= logging::trivial::info
  //  );
  logging::add_common_attributes();

  BOOST_LOG(lg) << "Greetings from the global logger!";

it's taken from boost tutorial but it doesn't write to file and doesn't show any error messages

它来自boost教程,但它没有写入文件,也没有显示任何错误消息

tutorial is here: http://www.boost.org/doc/libs/master/libs/log/example/doc/tutorial_file.cpp

教程在这里:http://www.boost.org/doc/libs/master/libs/log/example/doc/tutorial_file.cpp

what am I doing wrong?

我究竟做错了什么?

it works without line: logging::add_file_log("F:\\sample.log"); but I was trying different file locations and it doesn't write anywhere

它没有行:logging :: add_file_log(“F:\\ sample.log”);但我正在尝试不同的文件位置,它不会写任何地方

2 个解决方案

#1


4  

The problem is that your logger does not have a severity attribute. It does not produce severity values in log records, and because of that the filter you set always rejects records.

问题是您的记录器没有严重性属性。它不会在日志记录中生成严重性值,因此您设置的过滤器始终拒绝记录。

In the example from the docs you linked you can see that severity_logger and BOOST_LOG_SEV are used for logging (see the docs here). The severity_logger logger has the severity attribute of the type specified in the template argument, and the BOOST_LOG_SEV macro provides the severity value for every log record. Note that the type of the attribute value (the severity level) has to match the filter and formatter, if there are any installed. The logging::trivial::severity keyword in the filter you set implies that the severity level is expected to have type boost::log::trivial::severity_level.

在您链接的文档的示例中,您可以看到severity_logger和BOOST_LOG_SEV用于记录(请参阅此处的文档)。 severity_logger记录器具有template参数中指定类型的severity属性,BOOST_LOG_SEV宏提供每个日志记录的严重性值。请注意,如果已安装任何属性值,则属性值的类型(严重性级别)必须与过滤器和格式化程序匹配。您设置的过滤器中的logging :: trivial :: severity关键字意味着严重性级别应具有boost :: log :: trivial :: severity_level类型。

#2


3  

The file path "F:\sample.log" may be equivalent to "F:sample.log" when the escape sequence is taken care of. (this is implementation-defined, according to N3337 2.14.3 Character literals)
: is not usable in names of files in Windows.
Did you mean "F:\\sample.log"?

当处理转义序列时,文件路径“F:\ sample.log”可能等同于“F:sample.log”。 (根据N3337 2.14.3字符文字,这是实现定义的):在Windows中的文件名中不可用。你的意思是“F:\\ sample.log”?

#1


4  

The problem is that your logger does not have a severity attribute. It does not produce severity values in log records, and because of that the filter you set always rejects records.

问题是您的记录器没有严重性属性。它不会在日志记录中生成严重性值,因此您设置的过滤器始终拒绝记录。

In the example from the docs you linked you can see that severity_logger and BOOST_LOG_SEV are used for logging (see the docs here). The severity_logger logger has the severity attribute of the type specified in the template argument, and the BOOST_LOG_SEV macro provides the severity value for every log record. Note that the type of the attribute value (the severity level) has to match the filter and formatter, if there are any installed. The logging::trivial::severity keyword in the filter you set implies that the severity level is expected to have type boost::log::trivial::severity_level.

在您链接的文档的示例中,您可以看到severity_logger和BOOST_LOG_SEV用于记录(请参阅此处的文档)。 severity_logger记录器具有template参数中指定类型的severity属性,BOOST_LOG_SEV宏提供每个日志记录的严重性值。请注意,如果已安装任何属性值,则属性值的类型(严重性级别)必须与过滤器和格式化程序匹配。您设置的过滤器中的logging :: trivial :: severity关键字意味着严重性级别应具有boost :: log :: trivial :: severity_level类型。

#2


3  

The file path "F:\sample.log" may be equivalent to "F:sample.log" when the escape sequence is taken care of. (this is implementation-defined, according to N3337 2.14.3 Character literals)
: is not usable in names of files in Windows.
Did you mean "F:\\sample.log"?

当处理转义序列时,文件路径“F:\ sample.log”可能等同于“F:sample.log”。 (根据N3337 2.14.3字符文字,这是实现定义的):在Windows中的文件名中不可用。你的意思是“F:\\ sample.log”?