Markers in Logback can be very useful to filter events by their context (provided by the marker). Usually I use a TurboFilter to get rid of logging events that have (or don't have) a certain Marker associated with them:
<turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
<Marker>Data</Marker>
<OnMatch>DENY</OnMatch>
</turboFilter>
Now though, I have a special use case in which I want to filter out all logging events that have no Marker associated with them. It is probably possible by providing a chain of TurboFilters for each of the Markers used that allow on match and pass on if not, but that might end up a rather large and tedious bit of configuration that has to be updated each time a new Marker is introduced.
Long story short: Is there a simple way to filter out all Logback logging events that do not come with a Marker?
Since there is no way to achieve this out of the box, I ended up implementing my own Filter, which is simple enough for this use case:
public class NoMarkerFilter extends MatchingFilter {
@Override
public FilterReply decide(final Marker marker, final Logger logger, final Level level, final String format,
final Object[] params, final Throwable t) {
if (!isStarted()) {
return FilterReply.NEUTRAL;
}
if (marker == null) {
return onMatch;
}
return onMismatch;
}
It can then be used in the configuration like this:
<turboFilter class="com.example.NoMarkerFilter">
<OnMatch>DENY</OnMatch>
</turboFilter>