【问题标题】:boost log file not written to提升日志文件未写入
【发布时间】:2013-08-03 16:09:06
【问题描述】:

我一直在为 boost log 苦苦挣扎 - 我将他们的简单示例写入日志文件 (http://boost-log.sourceforge.net/libs/log/example/doc/tutorial_file.cpp)。但是,当我尝试将该代码复制到“记录器”类中时,我无法将其写入日志文件。我可以看到文件default.log 已创建,但其中没有任何内容。

我使用的是 debian 7 64 位。一切编译正常 - 编译行是:

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

这是我的代码:

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_ */

【问题讨论】:

    标签: c++ boost boost-log


    【解决方案1】:

    创建文件日志时需要这个属性

    keywords::auto_flush = true 
    

    这样日志条目会立即被写入。 默认情况下,文件记录器似乎在超出范围时写入文件,或者在其他一些神秘的时刻,文档没有提到任何关于

    【讨论】:

    • 就是这样——太奇怪了。该示例甚至没有提到该关键字,但效果很好。谢谢@user1283078!
    • 开启 auto_flush 不是仅用于调试吗?我相信如果没有 auto_flush,boost log 会出于性能原因有意缓冲数据并不经常写入。也就是说,我偶然发现了这篇文章,因为我只看到文件轮换发生时写入的日志。我希望这更清楚。我想知道我(以及许多其他人,考虑到这个答案的受欢迎程度)是否做错了其他事情,导致日志无法定期刷新。
    • 在我的小测试中,日志刷新到大约 8.2 KB 的文件中。这意味着日志文件停留在 0 字节,突然变成 8 KB。如果您在生产系统上进行大量日志记录,我认为keywords::auto_flush = true 必须小心使用。
    猜你喜欢
    • 1970-01-01
    • 2019-07-04
    • 2016-05-08
    • 2012-11-11
    • 2015-03-19
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多