c++loggingboostboost-logboost-logging

Boost log doesn't write to file, how to?


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

tutorial is here: 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


Solution

  • 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.