javalog4j2properties-file

What is the correct Java properties file translation of this XML configuration?


Context

I've been searching for documentation specific to configuration log4j2 using Java properties files, but all I can find are a few rather basic examples. Most documentation I find, if it even exists, shows how to do log4j2 configuration in an XML file. More specifically I need to configure a DynamicThresholdFilter filter in a Java properties file.

I found this example DynamicThresholdFilter XML configuration in the log4j2 manual.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
  <DynamicThresholdFilter key="loginId" defaultThreshold="ERROR"
                          onMatch="ACCEPT" onMismatch="NEUTRAL">
    <KeyValuePair key="User1" value="DEBUG"/>
  </DynamicThresholdFilter>
  <Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log"
                 filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
      <BurstFilter level="INFO" rate="16" maxBurst="100"/>
      <PatternLayout>
        <pattern>%d %p %c{1.} [%t] %m%n</pattern>
      </PatternLayout>
      <TimeBasedTriggeringPolicy />
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="RollingFile"/>
    </Root>
  </Loggers>
</Configuration>

I attempted to translate this configuration to Java properties syntax, but I haven't got it quite right.

status = warn
name = MyApp
packages =

dynamicThresholdFilter.key = loginId
dynamicThresholdFilter.defaultThreshold = ERROR
dynamicThresholdFilter.onMatch = ACCEPT
dynamicThresholdFilter.onMismatch = NEUTRAL
dynamicThresholdFilter.Pairs.0.key = User1
dynamicThresholdFilter.Pairs.0.value = DEBUG

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = logs/app.log
appender.rolling.filePattern = logs/app-%d{MM-dd-yyyy}.log.gz
appender.rolling.filter.burst.type = BurstFilter
appender.rolling.filter.burst.level = INFO
appender.rolling.filter.burst.rate = 16
appender.rolling.filter.burst.maxBurst = 100
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %p %c{1.} [%t] %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy

rootLogger.level = error
rootLogger.appenderRef.rolling.ref = RollingFile

Question

What is the correct Java properties translation of the above XML configuration?


Solution

  • The properties configuration factory in 2.x requires you to specify a type property for each sub-component (corresponding to a complex XML element). At the same time the prefix you use for a sub-component does not matter:

    filter.dynamic.type = DynamicThresholdFilter
    filter.dynamic.key = loginId
    filter.dynamic.defaultThreshold = ERROR
    filter.dynamic.onMatch = ACCEPT
    filter.dynamic.onMismatch = NEUTRAL
    filter.dynamic.p0.type = KeyValuePair
    filter.dynamic.p0.key = User1
    filter.dynamic.p0.value = DEBUG
    

    Remark: in 3.x we'll switch to jackson-dataformat-properties to provide a more "standard" mapping between XML and properties (cf. PR#2170) at the cost of additional dependencies for fans of the properties format.

    In this case the mapping will be more straightforward and you only need to add a type property for empty elements:

    DynamicThresholdFilter.key = loginId
    DynamicThresholdFilter.defaultThreshold = ERROR
    DynamicThresholdFilter.onMatch = ACCEPT
    DynamicThresholdFilter.onMismatch = NEUTRAL
    DynamicThresholdFilter.KeyValuePair[1].key = User1
    DynamicThresholdFilter.KeyValuePair[1].value = DEBUG
    

    The log4j-config-properties artifact will be available starting with version 3.0.0-beta2.