I'm using Boost Log in my project and I want to be able to filter the scopes that I'm currently interested in the console sink, within the settings file.
I have the names scope attribute added using:
BOOST_LOG_ATTRIBUTE_KEYWORD(scope_attr, "Scope", boost::log::attributes::named_scope::value_type)
boost::log::core::get()->add_global_attribute("Scope", boost::log::attributes::named_scope());
In code I'm using the BOOST_LOG_NAMED_SCOPE macro, and passing the following settings with boost::log::init_from_stream:
[Core]
DisableLogging = false
Filter = "%Severity% >= DEBUG"
[Sinks.1]
Destination = Console
Format = "[%Severity%] [%TimeStamp%] [%Scope%] [%Tag%]: %Message%"
Filter = "%Scope% = \"TargetScope\" & %Severity% >= DEBUG"
[Sinks.2]
Destination = TextFile
FileName = textfile.log
AutoFlush = true
Format = "[%Severity%] [%TimeStamp%] [%Scope%] [%Tag%]: %Message%"
Without the filter activated the "Scope" attribute works fine and prints properly adding "->" between scopes and there are logs where only the target scope get printed as "... [TargetScope] ...". But if I activate the filter nothing gets printed.
Filtering a named scope is supported by default? or one should write an extension for the filter parsing system?
docs
Taking from Boost logging, filter by named scope you can get a filter function (simplified by me):
Parsing The Filter
Here we have to Extend library settings support.
Adding support for user-defined types to the filter parser gives you some steps to follow.
I opted for the custom (non-simple) filter factory, and combining it with generalized matching functions like
my_filterjust shown, namedallandany:You register it with
register_filter_factory, so e.g.:Then we can test with a few functions like
Full Listing
Live On Coliru