c++ - 在日志中,Boost日志 2.0: 空严重级别

  显示原文与译文双语对照的内容
0 0

我使用Boost Log 2 .0系统有一些与版本1 的区别,我很难输出" 严重性" 属性。

我使用" boost .Format样式" 格式化程序: "%TimeStamp% [%Uptime%] (%LineID%) <%Severity%>: %Message%"

TimeStamp, LineID以及 Messagecommon_attributes. Uptime是一个属性而添加使用 attrs::timer(). 我认为 Severity是自动添加在使用 severity_logger但是很显然不是这就是我的问题 收到空严重性,eg :

2013-Apr-06 19:21:52.408974 [00:00:00.001337] (3) <>: A warning severity message

注意到空 <>. 我试着添加严重性using register_simple_formatter_factory但是后来收到编译器错误:

error: no matching function for call to ‘register_simple_formatter_factory(const char [9])’

我也没办法。

这是我的代码:

#include <iostream>
#include <boost/log/common.hpp>
#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/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/formatter_parser.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/attributes.hpp>
using namespace std;
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;
enum severity_level
{
    DEBUG,
    INFO,
    WARNING,
    ERROR,
    CRITICAL
};
BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::severity_logger_mt< severity_level> )
// The formatting logic for the severity level
template< typename CharT, typename TraitsT >
inline std::basic_ostream< CharT, TraitsT >& operator<< (
    std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
{
    static const char* const str[] =
    {
        "DEBUG",
        "INFO",
        "WARNING",
        "ERROR",
        "CRITICAL"
    };
    if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
        strm << str[lvl];
    else
        strm << static_cast< int >(lvl);
    return strm;
}
void init() {
    // logging::register_simple_formatter_factory< severity_level >("Severity");
    logging::add_file_log(
            keywords::file_name = "blop.log",
            keywords::auto_flush = true,
            keywords::open_mode = (std::ios::out | std::ios::app),
            keywords::format = "%TimeStamp% [%Uptime%] (%LineID%) <%Severity%>: %Message%"
            );
    logging::add_common_attributes();
    logging::core::get()->add_global_attribute("Uptime", attrs::timer());
}
int main(int argc, const char *argv[]) {
    init();
    src::severity_logger_mt< severity_level > lg = my_logger::get();
    BOOST_LOG_SEV(lg, DEBUG) << "A debug severity message";
    BOOST_LOG_SEV(lg, INFO) << "An informational severity message";
    BOOST_LOG_SEV(lg, WARNING) << "A warning severity message";
    BOOST_LOG_SEV(lg, ERROR) << "An error severity message";
    BOOST_LOG_SEV(lg, CRITICAL) << "A critical severity message";
    return 0;
}

注意到注释掉一行:

// logging::register_simple_formatter_factory< severity_level >("Severity");

它能产生上述错误。

我将其编译为: g++ main.cpp -Wall -DBOOST_ALL_DYN_LINK -lboost_system -lboost_log_setup -lboost_log -lboost_filesystem -lboost_date_time -lboost_thread -o main

帮忙欣赏 !

时间:原作者:5个回答

0 0

遇到了相同的问题( 空白%Severity%在日志文件中同样的编译器错误当试图添加register函数) 。 我发现在调用register_simple_formatter_factory应该是:

boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
原作者:
0 0

你可以在上面创建自己的后端并调用set_formatter 。 这是我的代码,演示如何做加上一些补充如日志文件清理和自动刷新:

BOOST_LOG_DECLARE_GLOBAL_LOGGER(db_logger, src::severity_logger_mt< dreambridge::log::LogLevel >);
#define LOGGER(level) BOOST_LOG_STREAM_SEV(db_logger::get(), level)
    void InitializeFileLog(const std::string & logDir)
    {   
        boost::shared_ptr< logging::core > loggingCore = logging::core::get();
        db_logger::get().add_attribute(
            "TimeStamp",
            boost::make_shared< attrs::local_clock >());
        string logPath = logDir + "/DreamBridgeSapi2MrcpTts_%N.log";
        boost::shared_ptr< sinks::text_file_backend > backend =
            boost::make_shared< sinks::text_file_backend >(
                // file name pattern
                keywords::file_name = logPath,
                // rotate the file upon reaching 5 MiB size...
                keywords::rotation_size = 5 * 1024 * 1024,
                // ...or at midnight, whichever comes first
                keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0)                    
            );
        backend->auto_flush(true);
        // Wrap it into the frontend and register in the core.
        // The backend requires synchronization in the frontend.
        typedef sinks::synchronous_sink< sinks::text_file_backend > sink_t;
        boost::shared_ptr< sink_t > sink(new sink_t(backend));
        loggingCore->add_sink(sink);
        backend->set_formatter(fmt::stream
                        << fmt::date_time< boost::posix_time::ptime >("TimeStamp")
                        << " : [" << fmt::attr< dreambridge::log::LogLevel >("Severity")
                        << "] " << fmt::message()
                        );
        backend->set_file_collector(sinks::file::make_collector(
            // rotated logs will be moved here
            keywords::target = logDir + "/old_logs",
            // oldest log files will be removed if the total size reaches 100 MiB...
            keywords::max_size = 100 * 1024 * 1024,
            // ...or the free space in the target directory comes down to 50 MiB
            keywords::min_free_space = 50 * 1024 * 1024
        ));
        try
        {
            backend->scan_for_files(sinks::file::scan_all);
        }
        catch(std::exception & e)
        {
            LOGGER(emergency) << "exception during scanning : " << e.what();
        }
    }

还可以将日志记录级别设置为:

void SetLogLevel(dreambridge::log::LogLevel logLevel)
    {
        boost::shared_ptr< logging::core > loggingCore = logging::core::get();
        loggingCore->set_filter
        (
            flt::attr< dreambridge::log::LogLevel >("Severity") <= logLevel
        );
    }

注: 这里的代码来自我自己的存储库。 dreamdridge是产品名称,因此使用命名空间。

原作者:
0 0

这是Prototype.js的 register_simple_formatter_factory函数模板:

template<typename AttributeValueT, typename CharT>
void register_simple_formatter_factory(attribute_name const &);

这意味着你忘了 typename CharT,因此,这个很正确的:

logging::register_simple_formatter_factory<severity_level, char>("Severity");

或其他字符类型,比如 wchar_t.

原作者:
...