提升日志更改默认的日志记录::核心格式化? [英] Boost Log changing the default logging::core formatter?

查看:156
本文介绍了提升日志更改默认的日志记录::核心格式化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用升压v1.54,我想简单地改变记录::核心的默认格式下沉,但还没有找到一种方法,这样做。在日志记录格式文档他们只显示如何更改格式自定义汇如日志文件,而不是默认的?有没有在全球范围内设置升压日志的默认格式的方式?

这是他们做了什么:

 无效的init()
{
    记录:: add_file_log
    (
        关键词:FILE_NAME =sample_%N.log
        关键词:rotation_size = 10 * 1024 * 1024,
        关键词:time_based_rotation =下沉::文件:: rotation_at_time_point(0,0,0),
        关键词:格式=[%时间戳%]:%讯息%
    );    记录::核心::得到() - GT; set_filter
    (
        记录::琐碎::严重性> =记录::琐碎::信息
    );
}

这是我想什么:

 无效的init()
{
    记录::核心::得到() - GT; set_default_format([%时间戳%]:%讯息%);
    记录::核心::得到() - GT; set_filter
    (
        记录::琐碎::严重性> =记录::琐碎::信息
    );
}


不知道为什么你想这样做,由于没有什么重大收获。
这是不可能的,但也有理由。

如果你看看下boost_1_54_0 /升压/日志/核心/ core.hpp它不允许设置任何东西,除了属性 - 这本身就是一个很大的特点。

预告背面,一个单一的接收器可以在同一时间有许多类型的数据流的


  • 基于文本文件

  • 基于控制台

  • 或两者(如果你想要写一次,输出去的文本文件以及控制台)

有关的核心,你只能有一个下沉到其中,只要你喜欢,你可以添加尽可能多的数据流。这些流可能是因为你的要求各不相同。但是格式将适用于水槽 - 所有流。

因此​​,这里是你如何看待的关系:


  • 一个核心 - >一个水槽(带格式) - >多个数据流

您发布一次,它会与你的应用格式的所有数据流。

小小的例子来自的相同的链接。

此外,一些code为使用多个数据流样本。

 的shared_ptr<的std :: ostream的> STRM(新的std ::的ofstream(test.log中));
    mSink-> locked_backend() - GT; add_stream(STRM);
    shared_ptr的<的std :: ostream的> pStream(安培;的std ::堵塞,记录:: empty_deleter());
    mSink-> locked_backend() - > add_stream(pStream);
    mSink-> set_formatter
    (
        EXPR ::格式(%1%:[%2%]%3%)
            %EXPR :: ATTR<提高::了posix_time ::分组时间>(时间戳)
            //%EXPR :: ATTR<提高::螺纹:: ID>(的ThreadID)
            %EXPR :: smessage
    );

I'm using Boost v1.54 and I would like to simply change the default sink format of logging::core but haven't found a way to do so. In the Log Record Formatting documentation they only show how to change the format for custom sinks e.g. log files and not the default one? Is there a way to globally set the default format in Boost Log?

This is what they do:

void init()
{
    logging::add_file_log
    (
        keywords::file_name = "sample_%N.log",
        keywords::rotation_size = 10 * 1024 * 1024,
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
        keywords::format = "[%TimeStamp%]: %Message%"
    );

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

This is what I would like:

void init()
{
    logging::core::get()->set_default_format("[%TimeStamp%]: %Message%");
    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::info
    );
}

解决方案

Not sure why you would like to do that as there's nothing major to gain. It is not possible but there are reasons for it.

If you look under boost_1_54_0/boost/log/core/core.hpp it does not allow anything to be set except attributes - which by itself is a great feature.

Coming back, one single sink can have many types of streams at the same time.

  • text file based
  • console based
  • or both (if you want to write once and the output to go to text file as well as console)

For the core you may have only one sink onto which you may add as many streams as you like. The streams may be as varied as your requirements. BUT the format will apply to the sink - all streams.

So here's how you view the relationship:

  • One Core --> One Sink (With Format) --> Multiple Streams

You publish once and it will go to all streams with the format you applied.

One minor example from the same link.

Also, some sample code for using multiple streams.

    shared_ptr< std::ostream > strm(new std::ofstream("test.log"));
    mSink->locked_backend()->add_stream(strm);
    shared_ptr< std::ostream > pStream(&std::clog, logging::empty_deleter());
    mSink->locked_backend()->add_stream(pStream);


    mSink->set_formatter
    (
        expr::format("%1%:[%2%] %3%")
            % expr::attr< boost::posix_time::ptime >("TimeStamp")
            //% expr::attr< boost::thread::id >("ThreadID")
            % expr::smessage
    );

这篇关于提升日志更改默认的日志记录::核心格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆