10

I am trying to create a Global Logger within my entire application so I can use

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

to get the global logger for different classes (resided in different files) logging.

I try to follow the example listed in boost.org (as listed below). But does not seems to work. Did anyone know any example I can follow or what I need to do make if works. Thanks.

http://www.boost.org/doc/libs/1_54_0/libs/log/doc/html/log/detailed/sources.html

BOOST_LOG_GLOBAL_LOGGER(my_logger, src::severity_logger_mt)


// my_logger.h
// ===========

#include "my_logger.h"

BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, src::severity_logger_mt)
{
    src::severity_logger_mt< > lg;
    lg.add_attribute("StopWatch", boost::make_shared< attrs::timer >());
    return lg;
}

// my_logger.cpp
// ===========

#include "my_logger.h"

BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, src::severity_logger_mt)
{
    src::severity_logger_mt< > lg;
    lg.add_attribute("StopWatch", boost::make_shared< attrs::timer >());
    return lg;
}
user2844779
  • 101
  • 1
  • 4

1 Answers1

21

I've just managed to get this working myself

Logging.h

#pragma once

#include <boost/log/expressions.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>

#define INFO  BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::info)
#define WARN  BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::warning)
#define ERROR BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::error)

#define SYS_LOGFILE             "/var/log/example.log"

//Narrow-char thread-safe logger.
typedef boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level> logger_t;

//declares a global logger with a custom initialization
BOOST_LOG_GLOBAL_LOGGER(my_logger, logger_t)

Logging.cpp

#include "Logging.h"

namespace attrs   = boost::log::attributes;
namespace expr    = boost::log::expressions;
namespace logging = boost::log;

//Defines a global logger initialization routine
BOOST_LOG_GLOBAL_LOGGER_INIT(my_logger, logger_t)
{
    logger_t lg;

    logging::add_common_attributes();

    logging::add_file_log(
            boost::log::keywords::file_name = SYS_LOGFILE,
            boost::log::keywords::format = (
                    expr::stream << expr::format_date_time<     boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
                    << " [" << expr::attr<     boost::log::trivial::severity_level >("Severity") << "]: "
                    << expr::smessage
            )
    );

    logging::add_console_log(
            std::cout,
            boost::log::keywords::format = (
                    expr::stream << expr::format_date_time<     boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
                    << " [" << expr::attr<     boost::log::trivial::severity_level >("Severity") << "]: "
                    << expr::smessage
            )
    );

    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::info
    );

    return lg;
}

main.c

#include "Logging.h"

int main(int argc, char **argv)
{
    INFO << "Program started";

    return 0;
}

My build settings

AM_LDFLAGS += -lboost_system -lboost_thread -lpthread
AM_LDFLAGS += -DBOOST_LOG_DYN_LINK -lboost_log_setup -lboost_log
AM_CXXFLAGS += -std=c++11 -DBOOST_LOG_DYN_LINK
Shiftee
  • 425
  • 4
  • 11
  • Hey, Shiftee: I have a question here: http://stackoverflow.com/q/29785243/1735836 about creating a global logger. Your answer above helped me tremendously! Have you figured out how to do this with multiple filters for multiple logs or even multiple sinks? Or how to make this code so the get() method can be used? I'm just learning Boost and my C++ isn't that great either. :-) – Patricia Apr 22 '15 at 14:38
  • Hey, Shiftee: I wish I could uproot this answer a dozen times! YOU ROCK!!!!! After banging my head on the monitor for a week, I finally looked at your code again and it didn't look like Japanese any more. You saved my @$$! :-) – Patricia Apr 28 '15 at 01:09
  • It's important to note the importance of adding '#pragma once' at the top of the logging header, as it takes care of this problem: http://stackoverflow.com/a/16727677/1735836 – Patricia Apr 29 '15 at 16:00
  • Hi Lucy, Glad you figured it out. It looks a little like Japanese to me now. If you have any improvements please add them – Shiftee Apr 29 '15 at 16:03
  • For folks using this with CMake, you can use `ADD_DEFINITIONS(-DBOOST_LOG_DYN_LINK)` after you find Boost. – firefly2442 Oct 10 '15 at 04:49
  • I don't understand how the `#define`s that you put in `Logging.h` are able to be used in `main.c`. It seems that `INFO` is undefined in it's context? – Jon Deaton Oct 30 '17 at 05:12
  • Those 3 #defines should technically be after BOOST_LOG_GLOBAL_LOGGER. It works because they are not compiled in place, they are used to replace text in main.c below the BOOST_LOG_GLOBAL_LOGGER & typedef calls which declare logger_t and my_logger – Shiftee Oct 31 '17 at 09:36