过滤掉没有标记的事件(Filtering out events that have no Marker)

编程入门 行业动态 更新时间:2024-10-27 20:35:43
过滤掉没有标记的事件(Filtering out events that have no Marker)

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>

更多推荐

本文发布于:2023-08-06 11:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448468.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标记   过滤掉   事件   Marker   events

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!