Exclude certain requests from Tomcat's log

偶尔善良 提交于 2020-07-15 06:57:32

问题


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.

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).

According to documentation, conditionUnless:

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.

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" />

How can I exclude all requests to index.html?


回答1:


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;   
  } 
}

and then check attribute name using conditionIf

conditionIf="doLog"

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.

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>  


来源:https://stackoverflow.com/questions/53740326/exclude-certain-requests-from-tomcats-log

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!