【问题标题】:Exclude certain requests from Tomcat's log从 Tomcat 的日志中排除某些请求
【发布时间】:2019-05-13 09:34:30
【问题描述】:

我的 Tomcat 访问日志目前充斥着来自负载平衡器的运行状况检查请求,因此很难真正理解发生了什么。例如,使用 GoAccess 我可以看到一些误导性的统计数据:

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 的标准 Access Log Valve 创建的。阀门应该有一个参数,conditionUnless,我尝试使用它来消除对index.html 的所有请求(这是健康检查的地方,所以我可以安全地过滤掉所有这些请求) .

根据文档,conditionUnless:

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

但我不知道如何使用过滤器过滤出所有对index.html 的请求并以某种方式标记它们。显然,server.xml 中的以下内容是不够的:

<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 的所有请求?

【问题讨论】:

    标签: tomcat logging


    【解决方案1】:

    您需要创建一个filter as suggested in tomcat group,将属性添加为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() 不为空时才会记录请求。例如,如果此值设置为重要,则仅在 ServletRequest.getAttribute("important") != null 时才会记录特定请求。使用过滤器是一种在 ServletRequest 中对许多不同请求设置/取消设置属性的简单方法。

    并将过滤器添加到 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>  
    

    【讨论】:

    • ">您需要创建一个过滤器 [...]。"当然可以,但是我该怎么做呢?我发现的关于过滤器的唯一示例是指 JSP,而我需要从 GeoServer 过滤日志,这是部署在 Tomcat 中的 WAR,我不知道将该 JSP 文件与我的过滤器放在哪里。有什么想法吗?
    • @Jacob 在 jsp 的路径上定义过滤器
    • 抱歉,我不确定我是否理解。我这里没有 JSP,只有我部署在 Tomcat 上的一个打包好的 WAR 文件。
    • @Jacob 在 html 的路径(url)上定义过滤器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 2021-12-04
    • 2013-12-21
    • 1970-01-01
    • 2012-03-28
    相关资源
    最近更新 更多