unable to set MaxFileSize to value “100MB” in log4

2019-07-12 12:20发布

I am using log4j1.2.17 and apache-extras-log4j--1.2.17.jar for logging.

My purpose is to roll and archive the file based on size.Below is the log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="R" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="File" value="logger.log"/> 
    <param name="append" value="true"/>
    <param name="encoding" value="UTF-8"/>      
    <rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
        <param name="FileNamePattern" value="logger.log%i.gz" />            
    </rollingPolicy>
    <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="MaxFileSize" value="${LOG_FILESIZE_LIMIT}"/>
    </triggeringPolicy>
    <layout class="org.apache.log4j.TTCCLayout">
      <param name="ContextPrinting" value="true"/>
      <param name="ThreadPrinting" value="true"/>
      <param name="DateFormat" value="MM/dd/yyyy HH:mm:ss"/>
    </layout>
   </appender>

  <root>
    <priority value ="DEBUG"/>
    <appender-ref ref="R"/>

  </root>

</log4j:configuration>

${LOG_FILESIZE_LIMIT} is 100MB.

But its not setting the MaxFileSize to 100MB and gives below message.

log4j:WARN Failed to set property [maxFileSize] to value "100MB"

Its taking default maxfilesize which is 10MB and archiving the file once it reaches to 10MB.

Thanks, Anjali

标签: java log4j
2条回答
一纸荒年 Trace。
2楼-- · 2019-07-12 12:51

Try setting the ${LOG_FILESIZE_LIMIT} placeholder to the value 104857600 (the equivalent of 100MB in bytes), as the MaxFileSize param accepts a long value.

查看更多
\"骚年 ilove
3楼-- · 2019-07-12 12:51

Check that there are no other versions of log4j on the classpath, I had this problem before where another third party set of jars also had an older version of log4j - removing it fixed the problem.

查看更多
登录 后发表回答