How to match strictness with same width in Boost magazine

For example, "information" and "warning" have different widths; they do not look in preety in the log. I want to align them with the same width in the log.

It seems that I could use a custom factory formatter as described in this article: format a single log format attribute with logging :: init_from_stream

This is another solution using an individual severity level: how to format a user severity level using a format string

Also, is there an easier way to achieve this by adjusting the format string in the same way as printf?

+4
source share
1

-

expr::stream << std::left << std::setw(7) << std::setfill(' ') <<
                logging::trivial::severity << " " << expr::smessage;

7 ( "" ). , .

( ):

namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;

using namespace logging::trivial;

logging::sources::severity_logger<severity_level> s_log;

void init_logging(const std::string& log_dir)
{
  boost::shared_ptr<logging::core> core = logging::core::get();
  boost::shared_ptr<sinks::text_file_backend> backend =
    boost::make_shared<sinks::text_file_backend>(
      keywords::file_name = log_dir + "/l_%Y-%m-%d_%5N.log", // one file per date and execution
      keywords::rotation_size = 512 * 1024); // rotate after 512KB

  // Collect clashing logs
  backend->set_file_collector(sinks::file::make_collector(keywords::target = log_dir));
  backend->scan_for_files();

  typedef sinks::synchronous_sink<sinks::text_file_backend> sink_t;
  boost::shared_ptr<sink_t> sink(new sink_t(backend));
  sink->locked_backend()->auto_flush(true);
  sink->set_formatter(expr::stream <<
                      "[" << expr::attr<boost::posix_time::ptime>("TimeStamp") << "] " <<
                      std::left << std::setw(7) << std::setfill(' ') << logging::trivial::severity << " " <<
                      expr::smessage);
  core->add_sink(sink);

  s_log.add_attribute("TimeStamp", logging::attributes::local_clock());
}
0

Source: https://habr.com/ru/post/1662224/


All Articles