Menu

Boost Global Logger How to write stream to a file

pdlarue
2015-04-28
2015-04-28
  • pdlarue

    pdlarue - 2015-04-28

    I am using the BOOST_LOG_GLOBAL_LOGGER_INIT template to set up my global logger. Here is the code:

    HEADER

    #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>
    #include <boost/log/utility/setup/file.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"
    #define APP_LOGFILE             "/home/pdl/ClionProjects/WrokingLogger/example.log"
    
    namespace attrs   = boost::log::attributes;
    namespace expr    = boost::log::expressions;
    namespace logging = boost::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(logger, logger_t)
    

    CPP

    #include "GlobalLogger.h"
    
    //Defines a global logger initialization routine
    BOOST_LOG_GLOBAL_LOGGER_INIT(logger, logger_t)
    {
        logger_t lg;
    
        logging::add_common_attributes();
    
        //logging::add_file_log("sample.log");
        logging::add_file_log(
                boost::log::keywords::file_name = APP_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.cpp

    #include <iostream>
    
    #include "GlobalLogger.h"
    
    using namespace std;
    
    int main() {
        boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level>& lg = logger::get();
    
        BOOST_LOG_SEV(lg, boost::log::trivial::info) << "info";
        BOOST_LOG_SEV(lg, boost::log::trivial::warning) << "warning";
        BOOST_LOG_SEV(lg, boost::log::trivial::error) << "error";
        BOOST_LOG_SEV(lg, boost::log::trivial::fatal) << "fatal";
    
        return 0;
    }
    

    This code is writing to the console perfectly but it's not writing to the file. Looking at the tutorial for writing to a file shows this snippet of code:

        logging::record rec = lg.open_record();
        if (rec)
        {
            logging::record_ostream strm(rec);
            strm << "Hello, World!";
            strm.flush();
            lg.push_record(boost::move(rec));
        }
    

    The full example code is located here: http://www.boost.org/doc/libs/1_54_0/libs/log/example/doc/tutorial_logging.cpp

    My BIG question is where to I put that code? Ideally I want the stream to be set up in the BOOST_LOG_GLOBAL_LOGGER_INIT template method. Can I do this? If yes, any examples of how to do this?

     
  • pdlarue

    pdlarue - 2015-04-28

    Andrey:

    Can you delete this post? I got it working.

    Thank you.

     

Log in to post a comment.