通过属性文件为log4j2中的特定记录器配置复合过滤器

发布于 2025-01-18 13:22:09 字数 2976 浏览 0 评论 0原文

Log4j2 提供了各种类型的过滤器 [1],这些过滤器可以在四个级别进行配置:

  1. 上下文范围的
  2. Logger
  3. Appender
  4. Appender 参考

在我的用例中,我想在 设置一个 CompositeFilter使用属性文件语法指定 Logger(上面的级别 2)。

为了举例说明,我们假设我们要设置一个由两个匹配 .*SQL.*.*JPQL.*< 的 RegexFilter 组成的复合过滤器。 /code> 分别。假设我们有以下事件:

logger.info("Messase with no keyword");
logger.info("Messase with SQL keyword");
logger.info("Messase with JPQL keyword");
logger.info("Messase with both SQL and JPQL keywords");

只有最后一个事件应该从复合过滤器传递。

我进行了多次尝试 [2](也在下面概述)来获得所需的输出,但我无法弄清楚如何使用属性语法正确设置 CompositeFilter,因此我们将不胜感激。

尝试 0

logger.t1.filter.f1.type = RegexFilter
logger.t1.filter.f1.regex = .*SQL.*
logger.t1.filter.f1.onMatch = NEUTRAL
logger.t1.filter.f1.onMismatch = DENY
logger.t1.filter.f2.type = RegexFilter
logger.t1.filter.f2.regex = .*JPQL.*
logger.t1.filter.f2.onMatch = ACCEPT
logger.t1.filter.f2.onMismatch = DENY
logger.t1.appenderRefs = a
logger.t1.appenderRef.a.ref = L0

尝试 1

logger.t1.filters = f1, f2
logger.t1.filter.f1.type = RegexFilter
logger.t1.filter.f1.regex = .*SQL.*
logger.t1.filter.f1.onMatch = NEUTRAL
logger.t1.filter.f1.onMismatch = DENY
logger.t1.filter.f2.type = RegexFilter
logger.t1.filter.f2.regex = .*JPQL.*
logger.t1.filter.f2.onMatch = ACCEPT
logger.t1.filter.f2.onMismatch = DENY

尝试 2

logger.t1.filters.f1.type = RegexFilter
logger.t1.filters.f1.regex = .*SQL.*
logger.t1.filters.f1.onMatch = NEUTRAL
logger.t1.filters.f1.onMismatch = DENY
logger.t1.filters.f2.type = RegexFilter
logger.t1.filters.f2.regex = .*JPQL.*
logger.t1.filters.f2.onMatch = ACCEPT
logger.t1.filters.f2.onMismatch = DENY
logger.t1.appenderRefs = a
logger.t1.appenderRef.a.ref = L0

尝试 3

logger.t1.filter.M.type = Filters
logger.t1.filter.M.f1.type = RegexFilter
logger.t1.filter.M.f1.regex = .*SQL.*
logger.t1.filter.M.f1.onMatch = NEUTRAL
logger.t1.filter.M.f1.onMismatch = DENY
logger.t1.filter.M.f2.type = RegexFilter
logger.t1.filter.M.f2.regex = .*JPQL.*
logger.t1.filter.M.f2.onMatch = ACCEPT
logger.t1.filter.M.f2.onMismatch = DENY
logger.t1.appenderRefs = a
logger.t1.appenderRef.a.ref = L0

一些进一步的说明:

  • 我对设置 CompositeFilter,不是 编写一个自定义过滤器,而不是使用单个组合的 RegexFilter 等。
  • 我有兴趣了解它是如何使用属性语法完成的,而不是 XML、JSON 或其他。

[1] https://logging.apache.org/log4j/2 .x/manual/filters.html

[2] https://github.com/zabetak/logging-log4j2/commit/a9944885100db42f7e3ba1b3ff81d59b743d0ab7

Log4j2 provides various kind of filters [1] and these can be configured at four levels:

  1. Context-wide
  2. Logger
  3. Appender
  4. Appender Reference

In my use-case, I would like to setup a CompositeFilter at a specified Logger (level 2 from above) using the property file syntax.

For the sake of the example let's assume that we want to setup a composite filter composed from two RegexFilter matching .*SQL.* and .*JPQL.* respectively. Assuming that we have the following events:

logger.info("Messase with no keyword");
logger.info("Messase with SQL keyword");
logger.info("Messase with JPQL keyword");
logger.info("Messase with both SQL and JPQL keywords");

Only the last event should pass from the composite filter.

I made several attempts [2] (also outlined below) to get the desired output but I wasn't able to figure out how to properly setup a CompositeFilter using the property syntax so any help would be greatly appreciated.

Attempt 0

logger.t1.filter.f1.type = RegexFilter
logger.t1.filter.f1.regex = .*SQL.*
logger.t1.filter.f1.onMatch = NEUTRAL
logger.t1.filter.f1.onMismatch = DENY
logger.t1.filter.f2.type = RegexFilter
logger.t1.filter.f2.regex = .*JPQL.*
logger.t1.filter.f2.onMatch = ACCEPT
logger.t1.filter.f2.onMismatch = DENY
logger.t1.appenderRefs = a
logger.t1.appenderRef.a.ref = L0

Attempt 1

logger.t1.filters = f1, f2
logger.t1.filter.f1.type = RegexFilter
logger.t1.filter.f1.regex = .*SQL.*
logger.t1.filter.f1.onMatch = NEUTRAL
logger.t1.filter.f1.onMismatch = DENY
logger.t1.filter.f2.type = RegexFilter
logger.t1.filter.f2.regex = .*JPQL.*
logger.t1.filter.f2.onMatch = ACCEPT
logger.t1.filter.f2.onMismatch = DENY

Attempt 2

logger.t1.filters.f1.type = RegexFilter
logger.t1.filters.f1.regex = .*SQL.*
logger.t1.filters.f1.onMatch = NEUTRAL
logger.t1.filters.f1.onMismatch = DENY
logger.t1.filters.f2.type = RegexFilter
logger.t1.filters.f2.regex = .*JPQL.*
logger.t1.filters.f2.onMatch = ACCEPT
logger.t1.filters.f2.onMismatch = DENY
logger.t1.appenderRefs = a
logger.t1.appenderRef.a.ref = L0

Attempt 3

logger.t1.filter.M.type = Filters
logger.t1.filter.M.f1.type = RegexFilter
logger.t1.filter.M.f1.regex = .*SQL.*
logger.t1.filter.M.f1.onMatch = NEUTRAL
logger.t1.filter.M.f1.onMismatch = DENY
logger.t1.filter.M.f2.type = RegexFilter
logger.t1.filter.M.f2.regex = .*JPQL.*
logger.t1.filter.M.f2.onMatch = ACCEPT
logger.t1.filter.M.f2.onMismatch = DENY
logger.t1.appenderRefs = a
logger.t1.appenderRef.a.ref = L0

Some further clarifications:

  • I am strictly interested for setting up a CompositeFilter, not
    write a custom filter, not use a single combined RegexFilter etc.
  • I am interested to see how it is done using the property syntax, not
    XML, JSON, or other.

[1] https://logging.apache.org/log4j/2.x/manual/filters.html

[2] https://github.com/zabetak/logging-log4j2/commit/a9944885100db42f7e3ba1b3ff81d59b743d0ab7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2025-01-25 13:22:09

There are two problems with your configuration:

  1. A LoggerConfig accepts only a single filter. If you want to use multiple filters, configure a CompositeFilter explicitly (which has a plugin name "Filters),
  2. The order of filters in a properties configuration file is basically random, since Properties is a Hashtable and does not have a predefined order.

Considering the two facts above a variation of your attempt 3 should work:

logger.t1.filter.M.type = Filters
logger.t1.filter.M.f1.type = RegexFilter
logger.t1.filter.M.f1.regex = .*SQL.*
logger.t1.filter.M.f1.onMatch = NEUTRAL
logger.t1.filter.M.f1.onMismatch = DENY
logger.t1.filter.M.f2.type = RegexFilter
logger.t1.filter.M.f2.regex = .*JPQL.*
logger.t1.filter.M.f2.onMatch = NEUTRAL
logger.t1.filter.M.f2.onMismatch = DENY

Both filters return NEUTRAL, when the pattern matches,保证过滤器顺序的反转不会打破逻辑。

There are two problems with your configuration:

  1. A LoggerConfig accepts only a single filter. If you want to use multiple filters, configure a CompositeFilter explicitly (which has a plugin name "Filters),
  2. The order of filters in a properties configuration file is basically random, since Properties is a Hashtable and does not have a predefined order.

Considering the two facts above a variation of your attempt 3 should work:

logger.t1.filter.M.type = Filters
logger.t1.filter.M.f1.type = RegexFilter
logger.t1.filter.M.f1.regex = .*SQL.*
logger.t1.filter.M.f1.onMatch = NEUTRAL
logger.t1.filter.M.f1.onMismatch = DENY
logger.t1.filter.M.f2.type = RegexFilter
logger.t1.filter.M.f2.regex = .*JPQL.*
logger.t1.filter.M.f2.onMatch = NEUTRAL
logger.t1.filter.M.f2.onMismatch = DENY

Both filters return NEUTRAL, when the pattern matches, which guarantees that an inversion of the order of filters does not break the logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文