【问题标题】:Add logs when call /health通话/健康时添加日志
【发布时间】:2017-08-24 12:12:09
【问题描述】:

我开发了一个应用程序 spring-boot 我使用执行器来提供健康指标。问题是我必须开发一个自定义运行状况,当我调用 /health 时,我需要记录该端点的返回。

我创建了一个端点 /health-bis,它调用 /health 并记录它的响应。但问题是当服务不可用时/health返回http代码503

你有什么想法吗?

最好的问候

【问题讨论】:

  • 你试过任何代码吗?!你能分享完整的例外吗?!

标签: spring-boot spring-boot-actuator health-monitoring


【解决方案1】:

我有同样的任务,并通过覆盖原来的 org.springframework.boot.actuate.health.HealthEndpoint 来解决它。我刚刚创建了我的自定义:

@Slf4j
@Component
@Endpoint(id = "health")
public class CustomHealthEndpoint extends HealthEndpoint {

    public CustomHealthEndpoint(HealthAggregator healthAggregator,
                                HealthIndicatorRegistry registry) {
        super(new CompositeHealthIndicator(healthAggregator, registry));
    }

    @Override
    @ReadOperation
    public Health health() {
        Health health = super.health();
        log.debug("Health status details: {}", health);
        return health;
    }
}

现在,当我每次调用 /health 时,我都会在我的日志中看到完整的状态日志消息。

【讨论】:

    【解决方案2】:

    您可以创建一个过滤器并注册它。

    RequestDumperFilter 将记录对过滤端点的请求/响应。它非常冗长,所以也许您想创建自己的过滤器,但这是一个好的开始。

    @Bean
    public FilterRegistrationBean requestDumperFilter()
    {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        Filter requestDumperFilter = new RequestDumperFilter();
        registration.setFilter(requestDumperFilter);
        registration.addUrlPatterns("/*");
        return registration;
    }
    

    【讨论】:

    • 谢谢@jfneis 我尝试了您的解决方案,但问题是过滤器调用了 startrtup。当我调用 /health 时,什么也没有发生
    • @Victor 奇怪,在我的应用程序中,每次调用每个端点都会被记录(正文、标题等)。您是否在配置类中声明了这个 bean?
    • 是的,我像你的例子一样声明为 bean
    • @Victor 你能更新你的问题吗?到目前为止你在日志上得到了什么?我将在这里与我的输出进行比较。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2010-10-13
    • 1970-01-01
    • 2022-06-13
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多