本文介绍了基于大小的log4j.xml滚动Appender的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面的我的log4.xml配置中找到滚动文件添加程序

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>

问题是InstrumentationAppender,debugAppender和errorAppender由上述类别中的com.practice pakaage引用.在所有附加程序中,maxFileSize为100kb并已为Test.log文件配置.大小为100kb后,问题Test.log文件不会翻转.所有日志仅被附加到相同的Test.log文件中.如何根据尺寸配置RollingAppender?

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?

预先感谢您的帮助.

推荐答案

# 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.

这篇关于基于大小的log4j.xml滚动Appender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:37