从Tomcat的日志中排除某些请求 [英] Exclude certain requests from Tomcat's log

查看:76
本文介绍了从Tomcat的日志中排除某些请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Tomcat访问日志当前被来自负载均衡器的运行状况检查请求弄乱了,因此很难真正了解正在发生的事情.例如,使用GoAccess,我可以看到一些令人误解的统计信息:

My Tomcat access log is currently cluttered with health check requests from a load balancer, so it's rather hard to actually understand what's going on. For example, using GoAccess I can see some misleading statistics:

Hits      h% Vis.    v%   Bandwidth Mtd Proto    Data
 ----- ------ ---- ----- ----------- --- -------- ----
 46221 81.20%    2 0.02%   30.72 MiB GET HTTP/1.1 /geoserver/index.html
 16     0.03%    1 0.01%   50.23 KiB GET HTTP/1.1 /geoserver/wms?SERVICE=WMS&VERSION=1.1.0&REQUEST=GetMap&FORMAT=image/jpeg.
 16     0.03%    1 0.01%  338.80 KiB GET HTTP/1.1 /geoserver/wms?SERVICE=WMS&VERSION=1.1.0&REQUEST=GetMap&FORMAT=image/png.

该日志是使用Tomcat的标准访问日志阀.该阀门应该具有一个参数 conditionUnless ,我尝试使用该参数来消除对 index.html 发出的所有请求(在此处进行健康检查),所以我可以安全地过滤掉所有这些).

The log is created using Tomcat's standard Access Log Valve. The valve is supposed to have a parameter, conditionUnless, which I try to use in order to get rid of all those requests being made to index.html (that's where health check goes, so I can safely filter out all of them).

根据文档,条件除非:

打开条件日志记录.如果设置,则仅在以下情况下记录请求 ServletRequest.getAttribute() null .例如,如果此值为设置为垃圾邮件,则只有在以下情况下才会记录特定的请求: ServletRequest.getAttribute("junk")== null .使用过滤器是一种设置/取消ServletRequest中的属性的简便方法不同的请求.

Turns on conditional logging. If set, requests will be logged only if ServletRequest.getAttribute() is null. For example, if this value is set to junk, then a particular request will only be logged if ServletRequest.getAttribute("junk") == null. The use of Filters is an easy way to set/unset the attribute in the ServletRequest on many different requests.

但是我不知道如何使用过滤器过滤出所有对 index.html 的请求,并在其中进行标记.显然, server.xml 中的以下内容是不够的:

But I can't figure out how to use Filters to filter out all requests to index.htmland flag them in some what. Obviously, the following in server.xml is not enough:

<Valve  className="org.apache.catalina.valves.AccessLogValve" 
        directory="/var/log/tomcat8/accesslogs"
        prefix="node1" suffix=".log"
        pattern="combined"
        renameOnRotate="true"
        conditionUnless="index.html" />

如何排除对 index.html 的所有请求?

How can I exclude all requests to index.html?

推荐答案

您需要创建一个

You need to create a filter as suggested in tomcat group that adds an attribute as doLog

public final class LoggingFilter implements Filter { 

  private FilterConfig filterConfig = null; 

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
    throws IOException, ServletException { 

    request.setAttribute(filterConfig.getInitParameter("doLog"), "true");         
    chain.doFilter(request, response); 
  } 
  public void destroy() { 
    this.filterConfig = null; 
  } 
  public void init(FilterConfig filterConfig) { 
    this.filterConfig = filterConfig;   
  } 
}

,然后使用 conditionIf

conditionIf="doLog"

条件如果
打开条件日志记录.如果设置,则仅当ServletRequest.getAttribute()不为null时才记录请求.例如,如果此值设置为重要,则仅当ServletRequest.getAttribute("important")!= null时才记录特定请求.过滤器的使用是一种在 ServletRequest 中针对许多不同请求设置/取消设置属性的简单方法.

conditionIf
Turns on conditional logging. If set, requests will be logged only if ServletRequest.getAttribute() is not null. For example, if this value is set to important, then a particular request will only be logged if ServletRequest.getAttribute("important") != null. The use of Filters is an easy way to set/unset the attribute in the ServletRequest on many different requests.

并将过滤器添加到web.xml:

and add filter to web.xml:

<filter>  
    <filter-name>LoggingFilter</filter-name>  
    <filter-class>com.yourpackage.LoggingFilter</filter-class>  
    <init-param>  
        <param-name>logParam</param-name>  
        <param-value>doLog</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>LoggingFilter</filter-name>  
    <url-pattern>/geoserver/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping>  

这篇关于从Tomcat的日志中排除某些请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆