通过属性文件为log4j2中的特定记录器配置复合过滤器
Log4j2 提供了各种类型的过滤器 [1],这些过滤器可以在四个级别进行配置:
- 上下文范围的
- Logger
- Appender
- 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:
- Context-wide
- Logger
- Appender
- 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 combinedRegexFilter
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
There are two problems with your configuration:
LoggerConfig
accepts only a single filter. If you want to use multiple filters, configure aCompositeFilter
explicitly (which has a plugin name "Filters),Properties
is aHashtable
and does not have a predefined order.Considering the two facts above a variation of your attempt 3 should work:
Both filters return
NEUTRAL
, when the pattern matches,保证过滤器顺序的反转不会打破逻辑。There are two problems with your configuration:
LoggerConfig
accepts only a single filter. If you want to use multiple filters, configure aCompositeFilter
explicitly (which has a plugin name "Filters),Properties
is aHashtable
and does not have a predefined order.Considering the two facts above a variation of your attempt 3 should work:
Both filters return
NEUTRAL
, when the pattern matches, which guarantees that an inversion of the order of filters does not break the logic.