Search code examples
c++boost-log

make boost::log::core thread local


I use boost.log in my multi-thread project, but I don't need a thread-safe version logger because I already make codes runs in thread-safe manner.

If use non thread safe logger, I need to make the logger object be thread_local: each thread has its own logger object. But the object is hidden behind boost.log's API: to create a logger object, user can not define the object as type obj; Its creating done by macro like BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULTI and get by logging::core::get() . I can not just write like:

thread_local boost::core::logger logger_obj;

How to do this?


Solution

  • Yes, you can create loggers in thread-local storage, almost exactly as you wrote:

    thread_local boost::log::sources::logger logger_obj;
    

    The logging core, however, cannot be made thread-local because it contains references to sinks and is used by all loggers from all threads concurrently.