如何在log4j.xml中设置过滤器

发布于 2024-10-14 16:53:09 字数 3377 浏览 7 评论 0原文

您好,

在我们的 Web 应用程序中,它打印所有日志,就像 Spring 和 JSF jar 文件一样。这对我们来说不是必需的。如何在 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>

HI,

In our web application it prints all the logs are printed like Spring and JSF jars files. Which not required for us. How can I set in the log4j.xml file to filter only for our application?

Updated

The following is the configuration we are using for the logging. Can you please tell me whether it is correct? Where I have to add the logger element in the file?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢望 2024-10-21 16:53:09

仅记录 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 ...

Log only warning for Spring :

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

Same for JSF with the jsf package name...
javax.faces, org.apache.myfaces, com.sun.faces etc... also, the appender-ref refers to your appenders...

翻身的咸鱼 2024-10-21 16:53:09

您需要为您的应用程序定义自己的记录器和附加器。
例如,如果您需要在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

You need to define the own logger and appender for your application.
For example, if you need to log the trace message in 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

See more at log4j official manual http://logging.apache.org/log4j/1.2/manual.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文