如何在Spring Boot框架下过滤tomcat生成的访问日志 [英] How to filter access logs generated by tomcat under spring boot framework

查看:388
本文介绍了如何在Spring Boot框架下过滤tomcat生成的访问日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用spring boot框架,通过嵌入式tomcat生成访问日志,访问日志的格式遵循以下属性:

We are using spring boot framework, generating access logs by embedded tomcat, the format of access logs is following the property below :

server.tomcat.access-log-enabled=true
server.tomcat.access-log-pattern="%h %l %u %t '%r' %s %b %D"
server.tomcat.basedir=/data/logs/Waijule/SchoolService/access

幸运的是,生成的访问日志成功,我们得到了肯定的结果:

Fortunately, access logs generated succeed, we got a positive result :

"127.0.0.1 - - [01/Mar/2016:11:25:47 +0800] 'POST /school-svc/index-summary HTTP/1.1' 200 127 21"
"127.0.0.1 - - [01/Mar/2016:11:25:47 +0800] 'POST /school-svc/wechat/signature/get HTTP/1.1' 200 238 9"
"127.0.0.1 - - [01/Mar/2016:11:25:47 +0800] 'POST /school-svc/wechat/ticket/get HTTP/1.1' 200 225 148"

为了确保服务运行正常,我们每5秒就会运行一次运行状况检查,我们要过滤请求以使我们能够获得清晰的访问日志,运行状况检查url类似于示例:

To make sure the service healthy, we have kept running health check every 5 seconds, the requests which we want to filter so that we could get clear access logs, health check url is like the sample :

"127.0.0.1 - - [01/Mar/2016:12:04:10 +0800] 'GET /school-svc/isHealth HTTP/1.1' 200 6 104"

如何过滤健康检查请求?

How to filter health check requests?

感谢您的帮助.

根据有关访问日志的文档 ,我尝试使用 conditionIf 解决问题,在我们的spring框架中,我尝试覆盖EmbeddedServletContainerCustomizer类中的custom函数,以便conditionIf可以设置,我们的实现类似于以下示例:

According to documentation on access loging, I tried to use conditionIf to solve the problems, in our spring framework, I tried to override customize function in EmbeddedServletContainerCustomizer class so that conditionIf could set up, our realization likes the following sample :

 @Configuration
 public class Application implements EmbeddedServletContainerCustomizer {
     @Override
     public void customize(ConfigurableEmbeddedServletContainer container)
     {
         if (container instanceof TomcatEmbeddedServletContainerFactory)
            {
                log.debug("It is TomcatEmbeddedServletContainerFactory");
                TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) container;
                AccessLogValve accessLogValve = new AccessLogValve();                    
                accessLogValve.setConditionIf("ignore");
                factory.addContextValves(accessLogValve);
            }
            else
            {
                log.error("WARNING! this customizer does not support your configured container");
            }
      }    
}

在我的理解中,下一步我们需要做的是设置属性参数,因此我在运行状况检查控制器的上面编写了拦截器,实现如下:

In my comprehension, the next step we need to do is set attribute parameter, so I wrote interceptor on the above of health check controller, realization likes that:

@Component
@Aspect
public class HealthCheckInterceptor
{
    @Before("execution(* com.waijule.home.controller.HealthCheckController.process(..))")
    private void beforeGetHomeDetailByHomeIdWithTry(JoinPoint joinPoint)
    {
        try
        {
            Object[] args = joinPoint.getArgs();
            Prediction.checkTrue(args[0] instanceof HttpServletRequest);
            HttpServletRequest request = (HttpServletRequest) args[0];
            request.setAttribute("ignore", "true");
        }
        catch (Exception e)
        {
            log.error(e.getMessage());
        }
    }
}

最后,检查RequestServlet的attribute("ignore")是否得到评估.

In the end, checked if attribute("ignore") of RequestServlet evaluated.

@RestController
@RequestMapping(value = { "/home-svc/isHealth" }, method = { RequestMethod.GET, RequestMethod.HEAD })
public class HealthCheckController
{
    private String HEALTH = "health";

    @RequestMapping
    public String process(HttpServletRequest request)
    {
        log.debug("ignore attribute is {}", request.getAttribute("ignore"));
        return HEALTH;
    }
}

输出:ignore属性为true

output: ignore attribute is true

但是,仍然可以在访问日志中找到健康检查的访问通知:

However, access informs of health check still be found in access logs:

"127.0.0.1 - - [03/Mar/2016:11:34:00 +0800] 'GET /home-svc/isHealth HTTP/1.1' 200 6 120"

我应该在过程的后期设置属性参数"ignore",何时以及如何为HttpServletRequest设置setAttribute?

I supposed attribute parameter 'ignore' set up late in the process, when and how to setAttribute for HttpServletRequest?

如果我们的假设不正确,是什么使操作无法进行?

If our supposition not correct, what makes the operations not work?

感谢您的帮助.

推荐答案

根据响应 https://stackoverflow.com/关于此问题的a/55569199/3346298 从Tomcat的日志中排除某些请求,您应该使用过滤器,然后将此过滤器添加到filterChain中,而不要通过AOP添加它.

As suggested by the response https://stackoverflow.com/a/55569199/3346298 on this question Exclude certain requests from Tomcat's log, you should use a filter and add this filter to the filterChain instead of adding it by AOP.

出于同样的原因,似乎在执行AOP方面之前已检查了日志/非日志功能.

For same reason seems like the log/not log feature is checked before the execution of you AOP aspect.

PD:对不起,您的回复很晚

PD: Sorry for the late response

这篇关于如何在Spring Boot框架下过滤tomcat生成的访问日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆