如何限制在application.log文件中打印的调试消息

时间:2011-12-06 12:06:20

标签: log4j

我有一个log4j.xml

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
   <appender name="appenderConsole" class="org.apache.log4j.ConsoleAppender">
      <param name="Target" value="System.out"/>
      <param name="Threshold" value="INFO"/>
      <layout class="org.apache.log4j.PatternLayout"> 
         <!-- The default pattern: Date Priority [Category] Message\n -->
       <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

      <appender name="appenderFile" class="org.apache.log4j.RollingFileAppender">
           <param name="File" value="log\\AuditComp.log"/>
           <param name="Append" value="false"/>
            <param name="Threshold" value="INFO"/>
           <param name="MaxFileSize" value="15MB"/>
           <param name="MaxBackupIndex" value="5"/>
           <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] %m%n"/>
           </layout>
       </appender>
   <root>
    <level value="info"/>
   <!-- <appender-ref ref="appenderConsole" />  --> 
     <appender-ref ref="appenderFile"/>   
  </root>   
</log4j:configuration>

我只需要在程序代码中出现的Info消息应该打印在日志文件中。但是在执行程序时,它会打印所有消息(调试,信息,警告)。我怎么做?

1 个答案:

答案 0 :(得分:0)

尝试在Threshold元素下使用此元素而不是appender参数:

<filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="INFO"/>
    <param name="LevelMax" value="OFF"/>
</filter>