你好,

在我们的Web应用程序中,它打印所有日志,就像Spring和JSF jar文件一样。这不是我们所必需的。如何在log4j.xml文件中设置为仅针对我们的应用程序进行过滤?

更新了

以下是我们用于日志记录的配置。你能告诉我这是否正确吗?我必须在文件中的哪里添加logger元素?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- Log4j Configuration file to log the messages into different logfiles -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="D:/logs/DEBUGFILE.log"/>
     <param name="Append" value="true"/>
     <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
     <param name="LevelMin" value="DEBUG" />
      <param name="LevelMax" value="DEBUG" />
    </filter>
  </appender>

  <appender name="INFO" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="D:/logs/AUDIT_TRAIL.log"/>
    <param name="Append" value="true"/>
     <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="INFO" />
      <param name="LevelMax" value="INFO" />
    </filter>
  </appender>

  <appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="D:/logs/ERRORFILE.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
     <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="ERROR" />
      <param name="LevelMax" value="ERROR" />
    </filter>
  </appender>

  <appender name="FATEL" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="D:/logs/FATELFILE.log"/>
    <param name="Append" value="true"/>
    <param name="MaxFileSize" value="500KB"/>
     <param name="MaxBackupIndex" value="2"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
    </layout>
     <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="FATAL" />
      <param name="LevelMax" value="FATAL" />
    </filter>
  </appender>

  <root>
    <priority value="debug"/>
      <appender-ref ref="DEBUG"/>
    <appender-ref ref="INFO"/>
    <appender-ref ref="ERROR"/>
    <appender-ref ref="FATEL"/>
  </root>

</log4j:configuration>

最佳答案

仅记录Spring警告:

<logger name="org.springframework">
    <level value="warn"/>
    <appender-ref ref="socket"/>
    <appender-ref ref="stdout"/>
</logger>

与jsf软件包名称的JSF相同...javax.facesorg.apache.myfacescom.sun.faces等...此外,appender-ref指的是您的appender ...

关于logging - 如何在log4j.xml中设置过滤器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4825072/

10-10 06:15