The source code can be found here
As discussed in Part 1, BOOST_LOG_TRIVIAL is a quick way to have a logger in a console with a predetermined formatting. Our goal is to customize the message structure and colors.
Create a console sink
The first step is to use a new macro called BOOST_LOG_SEV that receives a custom logger lg and the severity.
namespace src = boost::log::sources;
...
int main() {
init();
src::severity_logger<trivial::severity_level> lg;
BOOST_LOG_SEV(lg, trivial::trace) << "A trace severity message";
BOOST_LOG_SEV(lg, trivial::debug) << "A debug severity message";
BOOST_LOG_SEV(lg, trivial::info) << "An informational severity message";
BOOST_LOG_SEV(lg, trivial::warning) << "A warning severity message";
BOOST_LOG_SEV(lg, trivial::error) << "An error severity message";
BOOST_LOG_SEV(lg, trivial::fatal) << "A fatal severity message";
return 0;
}
[2024-12-14 00:12:15.390463] [0x00007e195fcdf740] [debug] A debug severity message
[2024-12-14 00:12:15.390476] [0x00007e195fcdf740] [info] An informational severity message
[2024-12-14 00:12:15.390479] [0x00007e195fcdf740] [warning] A warning severity message
[2024-12-14 00:12:15.390481] [0x00007e195fcdf740] [error] An error severity message
[2024-12-14 00:12:15.390482] [0x00007e195fcdf740] [fatal] A fatal severity message
As you can appreciate, the formatting with BOOST_LOG_SEV is the same as BOOST_LOG_TRIVIAL because Boost uses the same default sink. How do we modify the default message structure? Let’s create a custom sink that outputs to console, replacing the init method: