boost log file not written to
Asked Answered
B

1

17

I've been struggling with boost log for a while now - I got their simple example writing to a log file (http://boost-log.sourceforge.net/libs/log/example/doc/tutorial_file.cpp). However, when I try to copy that code into a 'Logger' class, I can't get it to write to the log file. I can see the file default.log get created, but there is nothing in it.

I'm on debian 7 64bit. Everything compiles fine - compile line is:

g++ -o build/Logger.o -c -std=c++11 -Wall -g -O0 -DBOOST_LOG_DYN_LINK -DDEBUG src/Logger.cpp
g++ -o build/logtest build/Logger.o -lboost_log -lboost_log_setup -lboost_date_time -lboost_thread -lboost_wave -lboost_regex -lboost_program_options

Here's my code:

Logger.cpp

/*
 * Logger.cpp
 *
 *  Created on: 2011-01-17
 *      Author: jarrett
 */

#include "Logger.h"

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

namespace dhlogging {

Logger::Logger(std::string fileName)
{
    initialize(fileName);
}

Logger::Logger(Logger const&)
{
}

Logger::~Logger()
{

}

Logger* Logger::logger_ = nullptr;
Logger* Logger::getInstance(std::string logFile)
{
    if ( Logger::logger_ == nullptr ) {
        logging::add_file_log( logFile );

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

        logging::add_common_attributes();
        Logger::logger_ = new Logger(logFile);
    }

    return Logger::logger_;
}

void Logger::initialize(std::string fileName)
{   
    BOOST_LOG(log_) << "Hello, World!";
    BOOST_LOG_SEV(log_, info) << "Hello, World2!";
}

void Logger::logInfo(std::string message)
{
    BOOST_LOG_SEV(log_, info) << message;
}

void Logger::logDebug(std::string message)
{
    BOOST_LOG_SEV(log_, debug) << message;
}

void Logger::logWarn(std::string message)
{
    BOOST_LOG_SEV(log_, warning) << message;
}

void Logger::logError(std::string message)
{
    BOOST_LOG_SEV(log_, error) << message;
}

void Logger::logFatal(std::string message)
{
    BOOST_LOG_SEV(log_, fatal) << message;
}

}

int main(int, char*[])
{
    logging::add_common_attributes();

    using namespace logging::trivial;

    dhlogging::Logger::getInstance()->logInfo("himom");

    return 0;
}

Logger.h

/*
 * Logger.h
 *
 *  Created on: 2011-01-17
 *      Author: jarrett
 */

#ifndef LOGGER_H_
#define LOGGER_H_

#include <map>
#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/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>

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

using namespace logging::trivial;

namespace dhlogging {
class Logger {

public:
    static Logger* getInstance(std::string logFile = "default.log");

    void logInfo(std::string message);
    void logDebug(std::string message);
    void logWarn(std::string message);
    void logError(std::string message);
    void logFatal(std::string message);


private:
    Logger(std::string fileName);
    Logger(Logger const&);
    Logger& operator=(Logger const&);
    virtual ~Logger();

    void initialize(std::string fileName);

    src::severity_logger< severity_level > log_;

    static Logger* logger_; // singleton instance
};
}
#endif /* LOGGER_H_ */
Bribe answered 3/8, 2013 at 16:9 Comment(0)
T
44

you need to this attribute when creating the file log

keywords::auto_flush = true 

that way log entrys get written immediately. by default, the file logger seems to write to file when it goes out of scope, or at some other mysterious point, the documentation doesn´t mention anything about

Toxoplasmosis answered 3/8, 2013 at 18:36 Comment(4)
That is exactly it - so weird. The example didn't even mention that keyword, and yet worked perfectly. Thanks @user1283078!Bribe
Example: #15854481Deaconry
Isn't turning auto_flush on for debug use only? I believe without auto_flush, boost log intentionally buffers data and writes infrequently for performance reasons. That said, I stumbled upon this post because I only see logs written when a file rotation happens. I wish this were clearer. I wonder if there's something else I (and many others, considering the popularity of this answer) am doing wrong that's preventing the log from flushing periodically.Photomontage
In my small test, the log flushed into file around 8.2 KB. Meaning the log file stays at 0 bytes and suddenly becomes 8 KB. I think keywords::auto_flush = true must be used with care if you have intensive logging on production systems.Darya

© 2022 - 2024 — McMap. All rights reserved.