简体   繁体   English

log4j 属性 BurstFilter 配置

[英]log4j properties BurstFilter configuration

I'm trying to configure BurstFilter for one specific Appender using log4j.properties format.我正在尝试使用log4j.properties格式为一个特定的 Appender 配置 BurstFilter。 The version of log4j is 1 and Apache Kafka unfortunately doesn't have support or log4j2 yet. log4j 的版本是 1 和 Apache Kafka 不幸的是还没有支持或 log4j2。 https://home.apache.org/~dongjin/post/apache-kafka-log4j2-support/ . https://home.apache.org/~dongjin/post/apache-kafka-log4j2-support/

I'm not able to find out any .properties format example for burstfilter on internet.我无法在互联网上找到 Burstfilter 的任何.properties格式示例。

This is the first part of my configuration.这是我配置的第一部分。 Where I'd like to add BurstFilter.我想在哪里添加 BurstFilter。 This configuration works well.这种配置效果很好。

log4j.rootLogger=INFO, graylog2
log4j.logger.org.reflections=ERROR

log4j.appender.graylog2=org.graylog2.log.GelfAppender
log4j.appender.graylog2.graylogHost=1.1.1.1
log4j.appender.graylog2.graylogPort=12201
log4j.appender.graylog2.originHost=my-hostname
log4j.appender.graylog2.facility=gelf-java
log4j.appender.graylog2.layout=org.apache.log4j.PatternLayout
log4j.appender.graylog2.layout.ConversionPattern=%d{ISO8601} %-5p  %X{dbz.connectorType}|%X{dbz.connectorName}|%X{dbz.connectorContext}  %m   [%c]%n
log4j.appender.graylog2.extractStacktrace=true
log4j.appender.graylog2.addExtendedInformation=true
log4j.appender.graylog2.additionalFields={'hostname': 'my-hostname'}

What I want to add is something like:我想补充的是:

log4j.appender.graylog2.filter=org.apache.logging.log4j.core.filter.BurstFilter
log4j.appender.graylog2.filter.level=INFO
log4j.appender.graylog2.filter.rate=16
log4j.appender.graylog2.filter.maxBurst=100

but when I use some properties to XML converter such as http://logback.qos.ch/translator/ I'm not able to get the desired outcome as seen here: https://logging.apache.org/log4j/2.x/manual/filters.html but when I use some properties to XML converter such as http://logback.qos.ch/translator/ I'm not able to get the desired outcome as seen here: https://logging.apache.org/log4j/2 .x/manual/filters.html

I haven't really tried to use this configuration on my Java server and I want to avoid trial and error method.我还没有真正尝试在我的 Java 服务器上使用此配置,我想避免试错法。 Any help appreciated.任何帮助表示赞赏。

Umm, your first properties file example is for Log4j 1, not Log4j 2. If it is working it either means you are still using Log4j 1 or are in compatibility mode.嗯,您的第一个属性文件示例适用于 Log4j 1,而不是 Log4j 2。如果它正在工作,则意味着您仍在使用 Z7CF7986F63A2746F6C4529B81862526D。 Since you didn't mention it I doubt that is the case.既然你没有提到它,我怀疑是这样的。 If you really are using Log4j 1 and don't want to use Log4j 2 then you will have to reimplement the BurstFilter for Log4j 1 on your own.如果您确实在使用 Log4j 1 并且不想使用 Log4j 2 那么您将不得不自己重新实现 Z7CF7986F63A2746F6C4529B81862526 的 BurstFilter。 Log4j 1 reached end of life in August 2015, has a couple of security vulnerabilities reported against it that will never be fixed and has a bug in Java 9+ having to do with detecting the JDK release which will also never be fixed. Log4j 1 已于 2015 年 8 月结束生命周期,报告了几个永远无法修复的安全漏洞,并且在 Java 9+ 中存在与检测 JDK 版本有关的错误,该版本也将永远无法修复。

If you do want to use Log4j 2 and the BurstFilter then continue reading, otherwise stop here.如果您确实想使用 Log4j 2 和 BurstFilter,请继续阅读,否则请在此处停止。

Is there a reason you want to use properties instead of XML, JSON or YAML?您是否有理由要使用属性而不是 XML、JSON 或 YAML? Although I added the properties support for Log4j 2 I really don't like it.虽然我添加了对 Log4j 2 的属性支持,但我真的不喜欢它。 Log4j's configuration is really a hierarchy, which is easily expressed in XML, JSON or YAML. Log4j 的配置确实是一个层次结构,很容易用 XML、JSON 或 YAML 表示。 Emulating a hierarchy with properties just feels cumbersome.用属性模拟层次结构感觉很麻烦。

When translating the easiest thing to do is to look at the XML and realize that every attribute and element has to be represented as a child property.翻译时最简单的做法是查看 XML 并意识到每个属性和元素都必须表示为子属性。 So below you will see that the properties for the burst filter all have to appear under appender.StdOut.filter.burst so that the property can be identified with the Appender named StdOut and with the BurstFilter whose attributes are all grouped under the "burst element".所以在下面你会看到突发过滤器的属性都必须出现在 appender.StdOut.filter.burst 下,以便可以使用名为 StdOut 的 Appender 和 BurstFilter 来标识该属性,其属性都分组在“burst 元素”下”。

appender.Stdout.type = Console
appender.Stdout.name = StdOut
appender.Stdout.target = SYSTEM_OUT
appender.Stdout.layout.type = PatternLayout
appender.Stdout.layout.pattern = %d [%t] %-5level: %msg%n%throwable
appender.Stdout.filter.burst.type = BurstFilter
appender.Stdout.filter.burst.onMatch = DENY
appender.Stdout.filter.burst.onMisMatch = NEUTRAL
appender.Stdout.filter.burst.level = INFO
appender.Stdout.filter.burst.rate = 16
appender.Stdout.filter.burst.maxBurst = 100

By way of contrast, The XML for this would be相比之下,XML 将是

<Console name="StdOut" target="SYSTEM_OUT">
  <PatternLayout pattern="%d [%t] %-5level: %msg%n%throwable"/>
  <BurstFilter level="INFO" rate="16" maxBurst="100" onMatch="DENY" onMisMatch="NEUTRAL"/>
</Console>

I could also compare it to JSON and YAML as well, but hopefully you get the point.我也可以将它与 JSON 和 YAML 进行比较,但希望你明白这一点。 The properties format is simply longer and IMO harder to understand.属性格式更长,IMO 更难理解。 All of that said, If you can't have XML or a JSON or YAML library in your app then using properties will always work.综上所述,如果您的应用程序中没有 XML 或 JSON 或 YAML 库,那么使用属性将始终有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM