2
votes

I am in the process of migrating my application from log4j 1.2 to log4j2-2.8.1 version. Following is the existing 1.x configuration in log4j.properties file.

log4j.appender.JSERRORFILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.JSERRORFILE.File=${log4j.loglocation}/jserror.log
log4j.appender.JSERRORFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.JSERRORFILE.layout.ConversionPattern=%d %-5p %c - %m%n

log4j.logger.com.app.JavascriptLogger=ERROR,JSERRORFILE
log4j.additivity.com.app.JavascriptLogger=false

Converted this to equivalent xml configuration log4j2.xml :

<RollingFile name="JSERRORFILE" fileName="${log-path}/jserror.log">
<PatternLayout pattern="%d %-5p %c - %m%n" />
</RollingFile>

<Logger name="com.app.JavascriptLogger" level="ERROR" additivity="false">
<AppenderRef ref="JSERRORFILE"/>
</Logger>

After conversion,I keep getting the following error :

 org.apache.logging.log4j.core.config.ConfigurationException: Arguments given for element RollingFile are invalid

Any help would be appreciated.

1

1 Answers

1
votes

You need to tell the RollingFile appender when to rollover (trigger policy) and what the result of a rollover should look like.

If you want to rollover at some regular interval (TimeBasedTriggeringPolicy or CronTriggeringPolicy) you need to specify a filePattern containing a SimpleDateFormat-like string. If you want to rollover to prevent large files (SizeBasedTriggeringPolicy), you need to specify a filePattern containing %i.

The filePattern is the relative or absolute path of the location you want the old (rolled over) file to be moved to.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log"
                 filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
      </PatternLayout>
      <Policies>
        <CronTriggeringPolicy schedule="0 0 0 * * ?"/>
        <SizeBasedTriggeringPolicy size="250 MB"/>
      </Policies>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="RollingFile"/>
    </Root>
  </Loggers>
</Configuration>

The cron expression above fires once a day.

For details and more examples see the RollingFile appender section of the user manual.