Enable logging in spring boot actuator health check API

我与影子孤独终老i 提交于 2020-01-02 05:00:10

问题


I am using Spring boot Actuator API for the having a health check endpoint, and enabled it by :

management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck

Mentioned here

Now I want to enable log in my application log file when ever the status of this above /healthcheck fails and print the entire response from this end point.

What is the correct way to achieve this?


回答1:


Best way is to extend the actuator endpoint with @EndpointWebExtension. You can do the following;

@Component
@EndpointWebExtension(endpoint = HealthEndpoint.class)
public class HealthEndpointWebExtension {

    private HealthEndpoint healthEndpoint;
    private HealthStatusHttpMapper statusHttpMapper;

    // Constructor

    @ReadOperation
    public WebEndpointResponse<Health> health() {
        Health health = this.healthEndpoint.health();
        Integer status = this.statusHttpMapper.mapStatus(health.getStatus());
        // log here depending on health status.
        return new WebEndpointResponse<>(health, status);
    }
}

More about actuator endpoint extending here, at 4.8. Extending Existing Endpoints



来源:https://stackoverflow.com/questions/54977273/enable-logging-in-spring-boot-actuator-health-check-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!