Boost logo

Boost Users :

Subject: Re: [Boost-users] init log with BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT
From: Igor Mironchik (igor.mironchik_at_[hidden])
Date: 2015-01-14 08:13:28


Hi. You missed this:

BOOST_LOG_ATTRIBUTE_KEYWORD( severity, "Severity", severity_level )
BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp",
boost::posix_time::ptime)

using namespace boost::log;

void init_logging()
{
        auto sink = boost::log::add_file_log
        (
                "sample.log",
                keywords::format = expressions::stream
                        << expressions::format_date_time( timestamp, "%Y-%m-%d, %H:%M:%S.%f" )
                        << " <" << severity.or_default(normal)
                        << "> " << expressions::message
        );

        // The sink will perform character code conversion as needed, according
to the locale set with imbue()
        std::locale loc = boost::locale::generator()("en_US.UTF-8");
        sink->imbue(loc);

        // Let's add some commonly used attributes, like timestamp and record
counter.
        boost::log::add_common_attributes();
}

On Wed, 14 Jan 2015 14:02:49 +0300, Bill Tian <billtian945_at_[hidden]>
wrote:

> Hi guys,
> I want to define a global logger that can be used in other place.
> The severity level always be "info".
> I init the log like this:
>
> #include <cstdlib>
> #include <boost/log/trivial.hpp>
> #include <boost/log/core.hpp>
> #include <boost/log/sources/global_logger_storage.hpp>
> #include <boost/log/sources/severity_logger.hpp>
> #include <boost/log/sources/record_ostream.hpp>
> #include <boost/log/sources/severity_feature.hpp>
>
>
> // Here we define our application severity levels.
> enum severity_level
> {
> normal,
> notification,
> warning,
> error,
> critical
> };
>
> // 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[] =
> {
> "normal",
> "notification",
> "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;
> }
>
> // Global logger declaration
> BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(test_lg,
> boost::log::sources::severity_logger_mt<severity_level>)
>
> void try_logging()
> {
> boost::log::sources::severity_logger_mt<severity_level>& lg =
> test_lg::get();
>
> // TODO:
> // always write "info" record, Unknown reason!
> BOOST_LOG_SEV(lg, normal) << "This is a normal severity record";
> BOOST_LOG_SEV(lg, notification) << "This is a notification severity
> record";
> BOOST_LOG_SEV(lg, warning) << "This is a warning severity record";
> BOOST_LOG_SEV(lg, error) << "This is a error severity record";
> BOOST_LOG_SEV(lg, critical) << "This is a critical severity record";
> }
>
> int main(int argc, char* argv[])
> {
> try_logging();
>
> return EXIT_SUCCESS;
> }
>
> In the log i got this:
> [2015-01-14 19:00:34.800935] [0x40000300] [info] This is a normal
> severity re
> cord
> [2015-01-14 19:00:34.805935] [0x40000300] [info] This is a
> notification sever
> ity record
> [2015-01-14 19:00:34.806935] [0x40000300] [info] This is a warning
> severity r
> ecord
> [2015-01-14 19:00:34.806935] [0x40000300] [info] This is a error
> severity rec
> ord
> [2015-01-14 19:00:34.806935] [0x40000300] [info] This is a critical
> severity
> record
>
> What do i miss?
> Thank you!
>
> Bill

-- 
Best Regards,
Igor Mironchik.



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net