Exclude certain requests from Tomcat's log

后端 未结 1 1693
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-16 20:35

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

相关标签:
1条回答
  • 2020-12-16 20:53

    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>  
    
    0 讨论(0)
提交回复
热议问题