13

你好,

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

更新

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

<?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>
4

2 回答 2

5

仅记录 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 指的是您的附加器...

于 2011-01-28T05:21:36.073 回答
1

您需要为您的应用程序定义自己的记录器和附加程序。例如,如果您需要将跟踪消息记录在com.xyz.http.RequestFilter

log4j.com.xyz.http.RequestFilter=TRACE, TRACE_APPEND

log4j.appender.TRACE_APPEND=org.apache.log4j.RollingFileAppender
log4j.appender.TRACE_APPEND.File=example.log

log4j.appender.TRACE_APPEND.MaxFileSize=100KB
log4j.appender.TRACE_APPEND.MaxBackupIndex=1

log4j.appender.TRACE_APPEND.layout=org.apache.log4j.PatternLayout
log4j.appender.TRACE_APPEND.layout.ConversionPattern=%p %t %c - %m%n

在 log4j 官方手册http://logging.apache.org/log4j/1.2/manual.html中查看更多信息

于 2011-01-28T05:19:33.790 回答