2

I enabled access log in my Spring Boot application on the properties file server.tomcat.accesslog.enabled=true but the problem is that now my access log file is bombarded with health check endpoint (provided by spring actuator).

Is there a way to enable access log but filter out the health check ones?

GantengX
  • 1,471
  • 2
  • 16
  • 28
  • Maybe this [Filter Access Logs](https://stackoverflow.com/questions/35714626/how-to-filter-access-logs-generated-by-tomcat-under-spring-boot-framework) will help you. – Muhammad Waqas Feb 01 '19 at 03:13
  • @MuhammadWaqas I saw the post but it doesn't have any solution either – GantengX Feb 01 '19 at 03:18
  • @GantengX were you able to solve it ? – Dhyey Feb 04 '20 at 09:56
  • @Dhyey more of a workaround, since I used google stackdriver logging I just do an exclusion that matches the pattern for health check endpoint and it won't be stored there – GantengX Feb 06 '20 at 01:56

1 Answers1

2

Finally found out a way.

You need to add a filter and a prop to application.properties:

AccessLogFilter.java

@WebFilter(urlPatterns = "/actuator/health")
public class AccessLogFilter implements Filter {

    @Value("${server.tomcat.accesslog.condition-unless}")
    private String conditionUnless;

    @Override
    public void doFilter(
            ServletRequest request,
            ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException

    {
        request.setAttribute(conditionUnless, "NO_LOG");
        chain.doFilter(request, response);
    }
}

application.properties

server.tomcat.accesslog.condition-unless=accessLog

Add @ServletComponentScan annotation to main SpringApplication class for @WebFilter to work.

@SpringBootApplication
@ServletComponentScan
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
Dhyey
  • 4,275
  • 3
  • 26
  • 33