简体   繁体   中英

log4j properties BurstFilter configuration

I'm trying to configure BurstFilter for one specific Appender using log4j.properties format. The version of log4j is 1 and Apache Kafka unfortunately doesn't have support or log4j2 yet. 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.

This is the first part of my configuration. Where I'd like to add 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

I haven't really tried to use this configuration on my Java server and I want to avoid trial and error method. 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. 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 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.

If you do want to use Log4j 2 and the BurstFilter then continue reading, otherwise stop here.

Is there a reason you want to use properties instead of XML, JSON or YAML? Although I added the properties support for Log4j 2 I really don't like it. Log4j's configuration is really a hierarchy, which is easily expressed in XML, JSON or 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. 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.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

<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. The properties format is simply longer and IMO harder to understand. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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