|
Boost : |
Subject: Re: [boost] Boost::log query
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2008-09-03 18:04:17
Rajpal Dangi wrote:
> Rephrasing my second question from previous post.
> Suppose NORMAL, ERROR and CRITICAL severity logs are directed to text
> file. How to identify which one of them belong to ERROR severity?
>
> I understand this can be done by using attribute (Tags in example) but
> I'm looking something implicit. So that we can avoid call to
> BOOST_LOG_SCOPED_LOGGER_TAG for each BOOST_LOG_SEV call.
>
> What I meant by print severity in log file is to have severity level as
> part of log file contents i.e.
> For BOOST_LOG_SEV(my_crit_logger, NORMAL)
> << "This record is explicitly marked as to NORMAL";
>
> Log file contents:
> ......[NORMAL] This record is explicitly marked as to NORMAL
Oh, I think, I understand now.
What you need is to specify a formatter. Formatters are used to describe
how different attributes will be represented in the log file. Severity
level is one of the attributes, it is automatically registered by the
severity_logger, so you don't need to put BOOST_LOG_SCOPED_LOGGER_TAG
macros or anything around your log records. All you need is to define
the formatter for the attribute and pass it to the sink backend. For
example:
// Assume, you have constructed the backend
shared_ptr< text_ostream_backend > backend;
// The formatter can be set in one of the two ways:
// the lambda-like expression
backend->set_formatter(
ostrm << "[" << attr< int >("Severity") << "] " << message()
);
// ... or with Boost.Format-like expression
backend->set_formatter(
format("[%1%] %2%") % attr< int >("Severity") % message()
);
The "Severity" in these examples is the attribute name. It is the name
that severity_logger uses to register the severity level attribute. int
is the type of the attribute, it is currently predefined and cannot be
customized. Now, you can log like this:
enum level { NORMAL, ERROR, CRITICAL };
severity_logger lg;
BOOST_LOG_SEV(lg, NORMAL)
<< "This record is explicitly marked as NORMAL";
BOOST_LOG_SEV(lg, Critical)
<< "This record is explicitly marked as CRITICAL";
The log file will contain then:
[0] This record is explicitly marked as NORMAL
[2] This record is explicitly marked as CRITICAL
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk