Boost logo

Boost Users :

Subject: [Boost-users] Log: Coloring logging output
From: Florian Lindner (mailinglists_at_[hidden])
Date: 2017-07-04 02:42:54


Hello,

I try to create a formatter that can display logging messages (or just prints the severity level in different colors).

Given a format string like:

default_formatter = "%SeverityFormat% %Message%";

/// A custom formatter that handles the Severity formatting
class severity_formatter_factory :
    public boost::log::formatter_factory<char>
{
public:
  formatter_type create_formatter(boost::log::attribute_name const& name, args_map const& args)
  {
    namespace expr = boost::log::expressions;

    return expr::stream <<
      "|" << "\033[32m" << expr::attr<boost::log::trivial::severity_level >("Severity") << "|";
  }
};

and register with

bl::register_formatter_factory("SeverityFormat", boost::make_shared<severity_formatter_factory>());

it basically works, i.e. the output is colored (all green) and the severity name is printed.

But how can I get the actual severity of the message that is about to be formatted inside the formatter? So I can act on
that and color warn messages red and info messages green?

What I found on the internets, e.g.

https://stackoverflow.com/questions/38309479/how-to-add-color-coding-to-boostlog-console-output
http://www.boost.org/doc/libs/1_64_0/libs/log/doc/html/log/tutorial/formatters.html

was using the sink->set_formatter method, which gives me, afaik, no possbility to configure using a format string.

But I'm open to any ideas...

Thanks,
Florian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net