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

Cryptic error message when misconfigured with File and RollingRandomAccessFile appenders targeting same file

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.9.0
    • Component/s: Configurators, Core
    • Labels:
      None

      Description

      When a configuration contains a FileAppender and a RollingRandomAccessFileAppender that target the same file, the below error occurs.

      Admittedly the configuration is wrong, but the error message is misleading or at least not helpful for users. Without intimate understanding of the Log4j internals (how FileManagers work) it is almost impossible to determine that the below error is caused by a misconfiguration, what the problem is and how to fix it.

      The configuration:

      <?xml version="1.0" encoding="UTF-8"?>
      <Configuration status="WARN">
        <properties>
          <property name="pattern">%d{ISO8601} %r [%t] %-5level %logger{1.} - %msg%n</property>
        </properties>
        <Appenders>
          <Console name="CONSOLE" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${pattern}" />
          </Console>
          <File name="FILE" fileName="logs/app.log">
            <PatternLayout pattern="${pattern}" />
          </File>
          <RollingRandomAccessFile name="ROLLING"
                 fileName="logs/app.log"
                 filePattern="logs/%d{yyyyMMdd}/app-%i.log.gz">
            <PatternLayout pattern="${pattern}" />
            <Policies>
              <OnStartupTriggeringPolicy/>
              <SizeBasedTriggeringPolicy size="1 GB" />
              <TimeBasedTriggeringPolicy/>
            </Policies>
            <DefaultRolloverStrategy/>
          </RollingRandomAccessFile>
        </Appenders>
        <Loggers>
          <Root level="DEBUG">
            <AppenderRef ref="CONSOLE" level="WARN"/>
            <AppenderRef ref="FILE" />
          </Root>
        </Loggers>
      </Configuration>
      

      Gives this error:

      2017-05-11 15:52:55,959 main ERROR Unable to inject fields into builder class for plugin type class org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender, element RollingRandomAccessFile. java.lang.ClassCastException: org.apache.logging.log4j.core.appender.FileManager cannot be cast to org.apache.logging.log4j.core.appender.rolling.RollingRandomAccessFileManager
              at org.apache.logging.log4j.core.appender.rolling.RollingRandomAccessFileManager.getRollingRandomAccessFileManager(RollingRandomAccessFileManager.java:87)
              at org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender$Builder.build(RollingRandomAccessFileAppender.java:115)
              at org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender$Builder.build(RollingRandomAccessFileAppender.java:52)
              at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:122)
              at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:952)
              at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:892)
              at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:884)
              at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:508)
              at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:232)
              at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:244)
              at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:545)
              at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
              at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
              at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
              at org.apache.logging.log4j.core.async.AsyncLoggerContext.start(AsyncLoggerContext.java:76)
              at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
              at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
              at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
              at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:122)
              at org.apache.logging.log4j.jul.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:34)
              at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
              at org.apache.logging.log4j.jul.LogManager.getLogger(LogManager.java:89)
              at java.util.logging.LogManager.demandLogger(LogManager.java:551)
              at java.util.logging.Logger.demandLogger(Logger.java:455)
              at java.util.logging.Logger.getLogger(Logger.java:502)
              at com.lmax.disruptor.FatalExceptionHandler.<clinit>(FatalExceptionHandler.java:27)
              at com.lmax.disruptor.dsl.ExceptionHandlerWrapper.<init>(ExceptionHandlerWrapper.java:8)
              at com.lmax.disruptor.dsl.Disruptor.<init>(Disruptor.java:66)
              at com.lmax.disruptor.dsl.Disruptor.<init>(Disruptor.java:105)
              at org.apache.logging.log4j.core.async.AsyncLoggerDisruptor.start(AsyncLoggerDisruptor.java:92)
              at org.apache.logging.log4j.core.async.AsyncLoggerContext.start(AsyncLoggerContext.java:75)
              at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
              at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
              at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
              at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:122)
              at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:43)
              at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
              at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
              at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
              at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
              at com.company.project.Main.main(Main.java:1030)
      

        Attachments

          Activity

            People

            • Assignee:
              remkop@yahoo.com Remko Popma
              Reporter:
              remkop@yahoo.com Remko Popma
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: