13

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?

Jacob
  • 1,056
  • 1
  • 12
  • 18

1 Answers1

8

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>  
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • "> You need to create a filter [...]." Sure, but how do I do that? The only examples I found regarding filters refer to JSP, while I need to filter a log from GeoServer, which is a WAR deployed in Tomcat, and I have no idea on where to put that JSP file with my filter. Any ideas? – Jacob Apr 09 '19 at 11:28
  • @Jacob define filter on the path of the jsp – Ori Marko Apr 09 '19 at 13:53
  • Sorry, but I'm not sure I understand. I have no JSP here, only a packaged WAR file that I've deployed on Tomcat. – Jacob Apr 10 '19 at 04:08
  • @Jacob define filter on the path (url) of the html – Ori Marko Apr 11 '19 at 06:49