Log: Coloring logging output

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Log: Coloring logging output

Boost - Users mailing list
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 mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Loading...