Log4j警告:FileAppender无maxFileSize等属性的含义、原因及解决方法
Let’s break down these warnings clearly, including what they mean, why they happen, and how to resolve them:
What Do These Warnings Mean?
The warnings log4j:WARN No such property [maxFileSize] in org.apache.log4j.FileAppender and log4j:WARN No such property [maxBackupIndex] in org.apache.log4j.FileAppender are Log4j’s way of telling you:
- You’ve tried to configure properties called
maxFileSizeandmaxBackupIndexon aFileAppenderinstance. - But the basic
FileAppenderclass doesn’t support these properties—they belong to a different appender type designed for rolling log files.
Specifically, maxFileSize defines the maximum size a log file can reach before being rolled over, and maxBackupIndex sets how many old rolled log files to keep. These are features of RollingFileAppender, not the standard FileAppender (which only writes to a single file indefinitely with no rollover logic).
Why Do These Warnings Occur?
The most common cause is a configuration mistake:
You added maxFileSize and maxBackupIndex to your log4j config file (either .properties or .xml) but accidentally specified org.apache.log4j.FileAppender as the appender class instead of org.apache.log4j.RollingFileAppender.
Log4j tries to apply the properties to the appender you defined, but since FileAppender has no such attributes, it throws these warnings.
How to Fix the Warnings
Fixing this is straightforward—you just need to correct your log4j configuration to use the right appender class:
Step 1: Update the Appender Class
Change the appender’s class from org.apache.log4j.FileAppender to org.apache.log4j.RollingFileAppender in your config file:
Example for .properties file:
# Before (wrong) log4j.appender.MyAppender.class=org.apache.log4j.FileAppender # After (correct) log4j.appender.MyAppender.class=org.apache.log4j.RollingFileAppender
Example for .xml file:
<!-- Before (wrong) --> <appender name="MyAppender" class="org.apache.log4j.FileAppender"> <!-- After (correct) --> <appender name="MyAppender" class="org.apache.log4j.RollingFileAppender">
Step 2: Verify the Full Configuration
Ensure your properties are correctly set under the RollingFileAppender here’s a complete example for both config types:
Complete .properties Example:
log4j.rootLogger=INFO, RollingFile log4j.appender.RollingFile=org.apache.log4j.RollingFileAppender log4j.appender.RollingFile.File=logs/myapp.log log4j.appender.RollingFile.MaxFileSize=10MB log4j.appender.RollingFile.MaxBackupIndex=5 log4j.appender.RollingFile.layout=org.apache.log4j.PatternLayout log4j.appender.RollingFile.layout.ConversionPattern=%d{ISO8601} %-5p %c{1}:%L - %m%n
Complete .xml Example:
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="RollingFile" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="logs/myapp.log"/> <param name="MaxFileSize" value="10MB"/> <param name="MaxBackupIndex" value="5"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ISO8601} %-5p %c{1}:%L - %m%n"/> </layout> </appender> <root> <priority value="INFO"/> <appender-ref ref="RollingFile"/> </root> </log4j:configuration>
Step 3: Double-Check for Typos
Make sure property names are spelled correctly (capitalization matters for XML param names: MaxFileSize not maxFileSize). For .properties files, the keys are case-insensitive, but sticking to standard capitalization avoids confusion.
Content of the question originates from Stack Exchange, question author Bishal Basak

