请在下面找到我的log4.xml配置,用于滚动文件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>
问题是instrumentationAppender,debugAppender和errorAppender由com.practice pakaage引用,如上面所定义。在所有appender中,maxFileSize为100kb并配置为Test.log文件。问题Test.log文件在100kb大小后不会翻转。所有日志都会附加到同一个Test.log文件中。如何根据大小配置RollingAppender?
提前感谢您的帮助。
答案 0 :(得分:2)
特此回答我自己的问题。希望它会有用。
<?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>
解决方案是代替不同日志级别的多个appender映射到同一个文件,添加LevelMatchFilter,如上所述,以匹配不同的日志级别到同一个文件。
答案 1 :(得分:1)
# Set the maximum file size before rollover
log4j.appender.FILE.MaxFileSize=100KB
# Set the the backup index
log4j.appender.FILE.MaxBackupIndex=2
如果你设置它将保留备份文件,直到你在该属性中提到的。