I'm trying to add colored log output for boost::log under linux. I read the following and I tried this:
#define MY_LOG_ERROR() BOOST_LOG_TRIVIAL(error) << "\033[1;31"
MY_LOG_ERROR() << "This is an error log."
but it gives me the result below:
[2016-07-11 17:23:16.328435] [0x00007f15f03d6780] [error] [1;31This is an error log.
How to properly add colored log output to boost::log?
The proper way to customize output with Boost.Log is to use formatters. To set a formatter you will have to set up a sink for that as described here, but you can keep using the
BOOST_LOG_TRIVIAL
macro for generating log records.The good thing about formatters is that you can have access to log record attributes like severity level within the formatter. For example, you could use the severity level to choose the color of the formatted log record on the console.
I've recently done this with a simple custom sink backend
coloured_console_sink.h
coloured_console_sink.cpp
usage