I'm using custom boost::log formatter for color coding the output log message, but I'm failing to find the proper way to add TimeStamp and ThreadID attributes to the log. When I'm using file logging I just write keywords::format = "[%TimeStamp%] [%ThreadID%] [%Severity%]: %Message%"
as logging::add_file_log
parameter. I want to have similar effect in the following custom formatter:
void coloring_formatter(const logging::record_view& record,
logging::formatting_ostream& stream)
{
auto severity = record[logging::trivial::severity];
assert(severity);
stream << "\e[1m";
switch (severity.get())
{
case logging::trivial::severity_level::trace:
stream << "\e[97m";
break;
case logging::trivial::severity_level::debug:
stream << "\e[34m";
break;
case logging::trivial::severity_level::info:
stream << "\e[32m";
break;
case logging::trivial::severity_level::warning:
stream << "\e[93m";
break;
case logging::trivial::severity_level::error:
stream << "\e[91m";
break;
case logging::trivial::severity_level::fatal:
stream << "\e[41m";
break;
}
stream // << output TimeStamp
// << output ThreadID
<< "[" << severity << "] "
<< record[logging::expressions::smessage]
<< "\e[0m";
}
Given that you have added these attributes to log records, there are multiple ways to extract them. The simplest way is to use keywords. First you declare keywords for the attributes:
BOOST_LOG_ATTRIBUTE_KEYWORD(a_timestamp, "TimeStamp", attrs::local_clock::value_type)
BOOST_LOG_ATTRIBUTE_KEYWORD(a_thread_id, "ThreadID", attrs::current_thread_id::value_type)
Note that the third parameter is the value type of the corresponding attribute. In this example I assumed that the attributes you used were local_clock
and current_thread_id
.
Now you can use these keywords to extract the value from log records.
stream << record[a_timestamp] << " "
<< record[a_thread_id]
<< " [" << severity << "] "
<< record[logging::expressions::smessage]
<< "\e[0m";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With