简体   繁体   English

从log4j 1.2迁移到log4j2

[英]Migrating from log4j 1.2 to log4j2

I am in the process of migrating my application from log4j 1.2 to log4j2-2.8.1 version. 我正在将应用程序从log4j 1.2迁移到log4j2-2.8.1版本。 Following is the existing 1.x configuration in log4j.properties file. 以下是log4j.properties文件中的现有1.x配置。

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 : 将此转换为等效的xml配置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. 任何帮助,将不胜感激。

You need to tell the RollingFile appender when to rollover (trigger policy) and what the result of a rollover should look like. 您需要告诉RollingFile附加程序何时进行过渡(触发策略)以及过渡的结果将是什么样。

If you want to rollover at some regular interval (TimeBasedTriggeringPolicy or CronTriggeringPolicy) you need to specify a filePattern containing a SimpleDateFormat -like string. 如果要以某个固定间隔(TimeBasedTriggeringPolicy或CronTriggeringPolicy)进行翻转,则需要指定一个包含filePattern SimpleDateFormat字符串的filePattern If you want to rollover to prevent large files (SizeBasedTriggeringPolicy), you need to specify a filePattern containing %i . 如果要滚动以防止大文件(SizeBasedTriggeringPolicy),则需要指定一个包含%ifilePattern

The filePattern is the relative or absolute path of the location you want the old (rolled over) file to be moved to. filePattern是您希望将旧文件(移过)移至的位置的相对或绝对路径。

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. 上方的cron表达式每天触发一次。

For details and more examples see the RollingFile appender section of the user manual. 有关详细信息和更多示例,请参见用户手册的RollingFile附加器部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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