I suggest you use this link as a reference...
Here below a small snippet of my code. In this small snippet I used a sync backend but you're free to use an async one.
log.hpp
#pragma once
#include <boost/log/common.hpp>
#include <boost/log/sources/severity_logger.hpp>
enum class LogSeverity {
trace, debug, info, warning, error, fatal
};
extern boost::log::sources::severity_logger<LogSeverity> g_logger;
void log_set_filter(LogSeverity level);
void init_logger();
#define LOG_TRACE BOOST_LOG_SEV(g_logger, LogSeverity::trace)
#define LOG_DEBUG BOOST_LOG_SEV(g_logger, LogSeverity::debug)
#define LOG_INFO BOOST_LOG_SEV(g_logger, LogSeverity::info)
#define LOG_WARNING BOOST_LOG_SEV(g_logger, LogSeverity::warning)
#define LOG_ERROR BOOST_LOG_SEV(g_logger, LogSeverity::error)
#define LOG_FATAL BOOST_LOG_SEV(g_logger, LogSeverity::fatal)
log.cpp
#include "bumper-common/log.hpp"
#include <boost/log/common.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/core/null_deleter.hpp>
#include <iomanip>
#include <iostream>
using namespace boost::log;
using LogTextSink = sinks::synchronous_sink<sinks::text_ostream_backend>;
LogSeverity g_logLevel = LogSeverity::info;
sources::severity_logger<LogSeverity> g_logger;
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", LogSeverity)
BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)
BOOST_LOG_ATTRIBUTE_KEYWORD(log_thread_id, "ThreadID", attributes::current_thread_id::value_type)
std::ostream& operator<< (std::ostream& strm, LogSeverity level)
{
static const std::array<std::string, 6> strings
{
std::string{"trace"},
std::string{"debug"},
std::string{"info"},
std::string{"warn"},
std::string{"error"},
std::string{"fatal"}
};
strm << strings[static_cast< std::size_t >(level)];
return strm;
}
void init_logger()
{
boost::shared_ptr<std::ostream> stream{&std::cout,
boost::null_deleter{}};
auto loggerSink = boost::make_shared<LogTextSink>();
add_common_attributes();
loggerSink->locked_backend()->add_stream(stream);
loggerSink->locked_backend()->auto_flush(true);
loggerSink->set_filter(severity >= g_logLevel);
loggerSink->set_formatter( expressions::stream
<< "[" << expressions::format_date_time(timestamp, "%H:%M:%S.%f") << "] ["
<< std::setw(5) << std::left << severity << "] ["
<< log_thread_id << "] "
<< expressions::smessage
);
boost::log::core::get()->add_sink(loggerSink);
}
void log_set_filter(LogSeverity level)
{
g_logLevel = level;
}
Hope this can help you. I've troubled a lot with this library. So I strongly suggest you read the documentation I've posted before.