Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-2021

Complex sifting/routing appender configuration

    XMLWordPrintableJSON

Details

    • Question
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.6
    • None
    • Appenders
    • None

    Description

      We're currently in the process of upgrading some of our components, including upgrading from Log4J to Log4J2.

      We previously had this configuration for splitting different logs to different files:

      log4j.appender.File=org.apache.log4j.sift.MDCSiftingAppender
      log4j.appender.File.key=instanceId
      log4j.appender.File.default=Unspecified
      log4j.appender.File.appender=org.apache.log4j.sift.MDCSiftingAppender
      log4j.appender.File.appender.key=bundleName
      log4j.appender.File.appender.default=unspecified
      log4j.appender.File.appender.appender=org.apache.log4j.RollingFileAppender
      log4j.appender.File.appender.appender.layout=org.apache.log4j.PatternLayout
      log4j.appender.File.appender.appender.layout.ConversionPattern=%d

      {ISO8601} | %-5.5p | %-16.16t | %-32.32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n
      log4j.appender.File.appender.appender.file=${karaf.data}/log/$\\{instanceId\\}/$
      {bundleName\\}.log
      log4j.appender.File.appender.appender.append=true
      log4j.appender.File.appender.appender.maxFileSize=1MB
      log4j.appender.File.appender.appender.maxBackupIndex=10

      This means we get different folders for different customers (instanceId) and different files for different OSGi bundles (bundleName) within those folders. This is a pretty sweet setup. To do the same in Log4J2, with XML configuration, I had to do this:

      <?xml version="1.0" encoding="UTF-8"?>
      <Configuration status="INFO">
      <Appenders>
      <Routing name="Routing">
      <Routes pattern="$${ctx:instanceId}">
      <!-- Has the 'instanceId' key -->
      <Route>
      <Routing name="RoutingWithInstance">
      <Routes pattern="$${ctx:bundleName}">
      <!-- Has the 'bundleName' key -->
      <Route>
      <RollingFile name="Rolling-${ctx:bundle.name}" fileName="${sys:karaf.data}/log/${ctx:instanceId}/${ctx:bundleName}.log" filePattern="${sys:karaf.data}/log/${ctx:bundleName}.log.%i">
      <PatternLayout>
      <pattern>%d{ISO8601}

      | %-5p | %X

      {instanceId} | %-16t | %-32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n</pattern>
      </PatternLayout>
      <Policies>
      <SizeBasedTriggeringPolicy size="1MB" />
      </Policies>
      <DefaultRolloverStrategy max="9" />
      </RollingFile>
      </Route>

      <!-- Missing the 'bundleName' key -->
      <Route key="$${ctx:bundleName}">
      <RollingFile name="Rolling-unspecified" fileName="${sys:karaf.data}/log/${ctx:instanceId}/unspecified.log" filePattern="${sys:karaf.data}/log/unspecified.log.%i">
      <PatternLayout>
      <pattern>%d{ISO8601} | %-5p | %X{instanceId}

      | %-16t | %-32c

      {1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n</pattern>
      </PatternLayout>
      <Policies>
      <SizeBasedTriggeringPolicy size="1MB" />
      </Policies>
      <DefaultRolloverStrategy max="9" />
      </RollingFile>
      </Route>
      </Routes>
      </Routing>
      </Route>

      <!-- Missing the 'instanceId' key -->
      <Route key="$${ctx:instanceId}">
      <Routing name="RoutingWithoutInstance">
      <Routes pattern="$${ctx:bundleName}">
      <!-- Has the 'bundleName' key -->
      <Route>
      <RollingFile name="Rolling-${ctx:bundle.name}" fileName="${sys:karaf.data}/log/Unspecified/${ctx:bundleName}.log" filePattern="${sys:karaf.data}/log/${ctx:bundleName}.log.%i">
      <PatternLayout>
      <pattern>%d{ISO8601} | %-5p | %X{instanceId} | %-16t | %-32c{1}

      | %X

      {bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n</pattern>
      </PatternLayout>
      <Policies>
      <SizeBasedTriggeringPolicy size="1MB" />
      </Policies>
      <DefaultRolloverStrategy max="9" />
      </RollingFile>
      </Route>

      <!-- Missing the 'bundleName' key -->
      <Route key="$${ctx:bundleName}">
      <RollingFile name="Rolling-unspecified" fileName="${sys:karaf.data}/log/Unspecified/unspecified.log" filePattern="${sys:karaf.data}/log/unspecified.log.%i">
      <PatternLayout>
      <pattern>%d{ISO8601} | %-5p | %X{instanceId} | %-16t | %-32c{1} | %X{bundle.id}

      - %X

      {bundle.name}

      - %X

      {bundle.version}

      | %m%n</pattern>
      </PatternLayout>
      <Policies>
      <SizeBasedTriggeringPolicy size="1MB" />
      </Policies>
      <DefaultRolloverStrategy max="9" />
      </RollingFile>
      </Route>
      </Routes>
      </Routing>
      </Route>
      </Routes>
      </Routing>
      </Appenders>
      <Loggers>
      <Logger name="com.viskan" level="DEBUG" />
      <Root level="INFO">
      <AppenderRef ref="Routing" />
      </Root>
      </Loggers>
      </Configuration>

      This looks much more complex, and I have lots of redundant configurations (policies, strategies, layouts).

      Is there an easier way to do this that I've missed?

      Attachments

        Activity

          People

            Unassigned Unassigned
            anton-johansson Anton Johansson
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: