Boost Log changing the default logging::core formatter?

丶灬走出姿态 提交于 2019-12-06 03:46:14

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
    );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!