Logback中的标记对于根据其上下文(由标记提供)过滤事件非常有用。 通常我使用TurboFilter来摆脱具有(或没有)与之关联的特定标记的日志记录事件:
<turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter"> <Marker>Data</Marker> <OnMatch>DENY</OnMatch> </turboFilter>现在,我有一个特殊的用例,我希望过滤掉所有没有与之关联的Marker的日志记录事件。 可能通过为每个使用的标记提供一系列TurboFilter,允许匹配并传递(如果不是),但这可能最终会导致相当大且繁琐的配置,每次新标记都需要更新介绍。
长话短说:有没有一种简单的方法来过滤掉Marker 没有附带的所有Logback日志记录事件?
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?
最满意答案
由于没有办法实现这个开箱即用,我最终实现了自己的Filter,这对于这个用例来说非常简单:
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; }然后可以在以下配置中使用它:
<turboFilter class="com.example.NoMarkerFilter"> <OnMatch>DENY</OnMatch> </turboFilter>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>更多推荐
发布评论