pdlarue - 2015-04-24

I've been trying for days now to create a Boost Global Logger for use throughout the application but I can't seem to get the severity level set in the Global Logger.

Right out of the Boost Documentation here

...it would be more convenient to have one or several global loggers in
order to easily access them in every place when needed. In this regard
std::cout is a good example of such a logger.

The library provides a way to declare global loggers that can be
accessed pretty much like std::cout. In fact, this feature can be used
with any logger, including user-defined ones. Having declared a global
logger, one can be sure to have a thread-safe access to this logger
instance from any place of the application code. The library also
guarantees that a global logger instance will be unique even across
module boundaries. This allows employing logging even in header-only
components that may get compiled into different modules.


Regardless of the macro you used to declare the logger, you can
acquire the logger instance with the static get function of the logger
tag:

src::severity_logger_mt< >& lg = my_logger::get();

Now, I'm trying a simpletest. Here is the code:

HEADER

#include <iostream>

#include <boost/log/common.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/console.hpp>

#include <boost/log/attributes.hpp>

using namespace std;

namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;

enum severity_level
{
    DEBUG,
    INFO,
    WARNING,
    ERROR,
    CRITICAL
};

typedef boost::log::sources::severity_logger_mt< > logger_t;
//typedef sinks::asynchronous_sink<sinks::text_ostream_backend> asynchronousSink;

BOOST_LOG_GLOBAL_LOGGER(my_logger, logger_t)
//BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::severity_logger_mt< > )

CPP

#include "simpletest.h"

BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, src::severity_logger_mt)
{
    src::severity_logger_mt< > my_logger;
    return my_logger;
}

// The formatting logic for the severity level
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
        std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
    static const char* const str[] =
            {
                    "DEBUG",
                    "INFO",
                    "WARNING",
                    "ERROR",
                    "CRITICAL"
            };
    if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
        strm << str[lvl];
    else
        strm << static_cast< int >(lvl);
    return strm;
}

bool onlyWarnings(const boost::log::attribute_value_set& set)
{
    return set["Severity"].extract<int>() > 0;
}

BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)

void SetLogLevel(int logLevel)
{
    boost::shared_ptr< logging::core > loggingCore = logging::core::get();
    loggingCore->set_filter(&onlyWarnings);
}

int main(int argc, const char *argv[]) {
    SetLogLevel(WARNING);
    src::severity_logger_mt< > lg = my_logger::get();
    BOOST_LOG_SEV(lg, DEBUG) << "A debug severity message";
    BOOST_LOG_SEV(lg, INFO) << "An informational severity message";
    BOOST_LOG_SEV(lg, WARNING) << "A warning severity message";
    BOOST_LOG_SEV(lg, ERROR) << "An error severity message";
    BOOST_LOG_SEV(lg, CRITICAL) << "A critical severity message";
    return 0;
}

I found a better example 62.10. A macro to define a global logger and a working version in this SO question. But the working example does not use the get() method. So, after declaring the BOOST_LOG_GLOBAL_LOGGER, I was able to access log::get() but I still can't get it to recognize the severity.

Here is the output:

/home/pdl/.clion/system/cmake/generated/32331b87/32331b87/Debug/BoostTest
[2015-04-17 15:17:22.157894] [0x000007f1c0eca078] [info]    An informational severity message
[2015-04-17 15:17:22.173623] [0x000007f1c0eca078] [info]    A warning severity message
[2015-04-17 15:17:22.173649] [0x000007f1c0eca078] [info]    An error severity message
[2015-04-17 15:17:22.173659] [0x000007f1c0eca078] [info]    A critical severity message

Process finished with exit code 0

Even though I've specified BOOST_LOG_SEV(lg, CRITICAL) everything gets logged as [info]

Will someone please help me figure out what is missing? Thank you in advance.