/* * Copyright Andrey Semashev 2007 - 2015. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace logging = boost::log; namespace src = boost::log::sources; namespace expr = boost::log::expressions; namespace sinks = boost::log::sinks; //[ example_tutorial_formatters_custom void my_formatter(logging::record_view const& rec, logging::formatting_ostream& strm) { // Get the LineID attribute value and put it into the stream strm << logging::extract< unsigned int >("LineID", rec) << ": "; // The same for the severity level. // The simplified syntax is possible if attribute keywords are used. strm << "<" << rec[logging::trivial::severity] << "> "; // Finally, put the record message to the stream strm << rec[expr::smessage]; } void init() { typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink; boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >(); sink->locked_backend()->add_stream( boost::make_shared< std::ofstream >("sample.log")); sink->set_formatter(&my_formatter); logging::core::get()->add_sink(sink); } //] int main(int, char*[]) { init(); logging::add_common_attributes(); using namespace logging::trivial; src::severity_logger< severity_level > lg; BOOST_LOG_SEV(lg, trace) << "A trace severity message"; 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, fatal) << "A fatal severity message"; return 0; }