问题
Please find below my log4.xml configuration for rolling file appender
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="instrumentationAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="C:\\Users\\Test\\Downloads\\Testlogs\\instrumentation.log"/>
<param name="Append" value="true" />
<param name="Encoding" value="UTF-8" />
<param name="MaxFileSize" value="100KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] (%C:%L) - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="debugAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="C:\\Users\\Test\\Downloads\\Testlogs\\Test.log"/>
<param name="Append" value="true" />
<param name="Encoding" value="UTF-8" />
<param name="MaxFileSize" value="100KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] (%C:%L) - %m%n"/>
</layout>
<param name="ImmediateFlush" value="true" />
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="errorAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="C:\\Users\\Test\\Downloads\\Testlogs\\Test.log"/>
<param name="Append" value="true" />
<param name="Encoding" value="UTF-8" />
<param name="MaxFileSize" value="100KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] (%C:%L) - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="FATAL" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<category name="com.practice.Test" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="instrumentationAppender" />
<appender-ref ref="debugAppender" />
<appender-ref ref="errorAppender" />
</category>
The issue is instrumentationAppender,debugAppender and errorAppender are refered by com.practice pakaage as defined in above category. In all the appenders maxFileSize is 100kb and configured for Test.log file. The issue Test.log file does not get rollover after 100kb of size. All logs just get appended to same Test.log file. How do configure RollingAppender based on size?
Thanks in advance for the help.
回答1:
# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=100KB
# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=2
If you set it will be keeping backup files upto what ever you mentioned in that property.
回答2:
Hereby answering my own question. Hope it would be useful.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="instrumentationAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="C:\\Users\\Test\\Downloads\\Testlogs\\instrumentation.log"/>
<param name="Append" value="true" />
<param name="Encoding" value="UTF-8" />
<param name="MaxFileSize" value="100KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] (%C:%L) - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<appender name="debugAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="C:\\Users\\Test\\Downloads\\Testlogs\\Test.log"/>
<param name="Append" value="true" />
<param name="Encoding" value="UTF-8" />
<param name="MaxFileSize" value="100KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] (%C:%L) - %m%n"/>
</layout>
<param name="ImmediateFlush" value="true" />
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="DEBUG" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="ERROR" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="WARN" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="FATAL" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<category name="com.practice.Test" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="instrumentationAppender" />
<appender-ref ref="debugAppender" />
</category>
</log4j:configuration>
Solution is instead of multiple appenders of different log levels mapping to same file add LevelMatchFilter as above to match different log levels to same file.
来源:https://stackoverflow.com/questions/26343846/log4j-xml-rolling-appender-based-on-size