Exclude certain requests from Tomcat's log

后端 未结 1 1690
佛祖请我去吃肉
佛祖请我去吃肉 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:

      
        LoggingFilter  
        com.yourpackage.LoggingFilter  
          
            logParam  
            doLog  
          
      
      
        LoggingFilter  
        /geoserver/*
        REQUEST
        INCLUDE
        FORWARD
        ERROR
    
      
    

    0 讨论(0)
提交回复
热议问题