user1633272
user1633272

Reputation: 2309

How to config Boost logging filter in configuration file

I'm using Boost 1.63 in VS2015 on Windows 10.

The code is following Boost example.

Here's code, seems the record generated by 'severityLogger.open_record' is NOT valid, it doesn't go inside the if-statement.

#include <boost/format.hpp>
#include <boost/locale/generator.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/from_settings.hpp>
#include <boost/log/utility/setup/settings_parser.hpp>
#include <boost/phoenix.hpp>

namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
namespace sinks = boost::log::sinks;
namespace src = boost::log::sources;

src::wseverity_logger<logging::trivial::severity_level> severityLogger;
std::wifstream strm(L"config\\logger.ini");
strm.imbue(std::locale(strm.getloc(), new std::codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>));
auto settings = logging::parse_settings(strm);
logging::init_from_settings(settings);
logging::add_common_attributes();

auto rec = severityLogger.open_record(keywords::severity = logging::trivial::severity_level::info);
if (rec) { // Unable to go here !!!
    logging::wrecord_ostream strm(rec);
    strm << L"Some message";
    strm.flush();
    severityLogger_.push_record(boost::move(rec));
}

However, it works fine if I add this piece of code:

logging::core::get()->set_filter(
    logging::trivial::severity >= logging::trivial::info);

Here's my configuration file:

[Core]
DisableLogging=false
Filter="%Severity% >= info"

[Sinks.File]
Destination=TextFile
FileName="App_%Y-%m-%d.%N.log"
Format="%TimeStamp% %Severity% - %Message%"
AutoFlush=true
TimeBasedRotation="00:00:00"
RotationSize=10485760

Upvotes: 1

Views: 1061

Answers (1)

Andrey Semashev
Andrey Semashev

Reputation: 10606

The problem is that by default the filter parser does not expect an attribute of type logging::trivial::severity_level. As a result the parsed filter is not able to recognize the severity level and rejects the log record.

You need to indicate the attribute value type by registering a filter factory as described in this section. Basically, this needs to be done for any attributes that have value types other than those mentioned in this section. It is recommended to register all attributes, even those listed, because otherwise the library has to perform a more expensive lookup across all types supported by default when processing the filter.

You may also need to register a formatter factory, similar to the filter factory.

Upvotes: 1

Related Questions