Question

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
    );
}
Était-ce utile?

La solution

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
    );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top