Boost Logger的问题

时间:2019-11-11 14:20:00

标签: c++ boost

我正在尝试创建一个多线程的Boost logger文件。 那可以做以下事情:

  1. 写信给目录。
  2. 可以在午夜或rache最大文件大小时旋转文件。
  3. 需要检查是否有足够的空间将文件刻录到光盘上。
  4. 最多可以创建10个日志文件。

我使用的增强版本是1.65.1。

问题是:

  1. 提高了具有相同内容示例的2个文件箱: BioEngine_17145_Mon_Nov_11_15_32_40_2019和BioEngine_11_11_2019_15_32_40_0.log
  2. 没有一个文件在我需要的目标目录中创建。文件中包含要在哪里运行的文件。
  3. 最大文件数不起作用,我分配的文件数超过10个。

我找不到我在做什么。 感谢您提供以下帮助代码。

Logger.h

    #ifndef LOG_LOGGER_HXX
#define LOG_LOGGER_HXX
#pragma once
#include <string>
#include <boost/log/trivial.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/move/utility.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/log/support/date_time.hpp>
#include <string>
using std::string;
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;
namespace attrs = boost::log::attributes;
namespace sinks = boost::log::sinks;
namespace expr = boost::log::expressions;
namespace trivial = boost::log::trivial;

#define LogInfo     MYLOG(logging::trivial::severity_level::info)
#define LogWarning  MYLOG(logging::trivial::severity_level::warning)
#define LogError    MYLOG(logging::trivial::severity_level::error)
#define LogTrace    MYLOG(logging::trivial::severity_level::trace)
#define LogFatal    MYLOG(logging::trivial::severity_level::fatal)
#define LogDebug    MYLOG(logging::trivial::severity_level::debug)
typedef boost::log::sources::severity_logger_mt<boost::log::trivial::severity_level> logger_t;
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > file_sink;
BOOST_LOG_GLOBAL_LOGGER(my_logger, logger_t)

#define MYLOG(severity) BOOST_LOG_SEV(my_logger::get(),severity) << "[" << __FUNCTION__ << ":" << __LINE__ <<"] "




#endif /* LOG_LOGGER_HXX */

在将任何日志写入文件之前,从Init函数调用记录器Init函数。

 void LoggerInit(string& log_dir)
{
    boost::shared_ptr< file_sink > sink(new file_sink(
        boost::log::keywords::target = log_dir,
        boost::log::keywords::file_name = "BioEngine_%d_%m_%Y_%H_%M_%S_%N.log",
        boost::log::keywords::rotation_size = 10 * 1024 * 1024,
        boost::log::keywords::max_size = 10 * 1024 * 1024,
        boost::log::keywords::min_free_space = 100 * 1024 * 1024,
        boost::log::keywords::max_files = 10,
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
        boost::log::sinks::file::scan_method::scan_matching,
        boost::log::keywords::open_mode = std::ios_base::app,
        boost::log::keywords::auto_flush = true
    ));


    sink->locked_backend()->set_file_collector(boost::log::sinks::file::make_collector(
        boost::log::keywords::target = log_dir,                      /*< the target directory >*/
        boost::log::keywords::max_size = 10 * 1024 * 1024,          /*< maximum total size of the stored files, in bytes >*/
        boost::log::keywords::min_free_space = 100 * 1024 * 1024,   /*< minimum free space on the drive, in bytes >*/
        boost::log::keywords::max_files = 10                       /*< maximum number of stored files >*/
    ));

    sink->set_formatter(
        expr::stream
        << "[" << boost::log::expressions::attr<boost::log::process_id>("ProcessID")
        << "][" << boost::log::expressions::attr<boost::log::thread_id>("ThreadID")
        << "][" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%d-%m-%Y %H:%M:%S.%fz") << "]"
        << "[" << logging::trivial::severity << "]"
        << expr::smessage
    );


    sink->locked_backend()->scan_for_files(boost::log::sinks::file::scan_method::scan_matching, true);

    logging::core::get()->add_sink(sink);



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

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,现在只有1个日志写入到进程工作目录,并且在文件旋转后将其旋转到boost :: log :: keywords :: target

void LoggerInit(string& log_dir)
{
    logging::add_common_attributes();
    boost::shared_ptr< logging::core > core = logging::core::get();

    logging::add_file_log
    (
            keywords::file_name =   "BioEngine_%d_%m_%Y__%H_%M_%S_%N.log",
            keywords::rotation_size = 10 * 1024 * 1024,
            boost::log::keywords::target = log_dir,
            boost::log::keywords::min_free_space = 100 * 1024 * 1024,
            boost::log::keywords::max_size = 100 * 1024 * 1024,
            keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
            boost::log::keywords::scan_method = boost::log::sinks::file::scan_matching,
            keywords::auto_flush = true,
            keywords::max_files = 10,
            keywords::format =
            (
                    expr::stream
                    << "[" << boost::log::expressions::attr<boost::log::process_id>("ProcessID")
                    << "][" << boost::log::expressions::attr<boost::log::thread_id>("ThreadID")
                    << "][" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%d-%m-%Y %H:%M:%S.%f") << "]"
                    << "[" << logging::trivial::severity << "]"
                    << expr::smessage
                    )
    );


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