在Spring Boot执行器运行状况检查API中启用日志记录 [英] Enable logging in spring boot actuator health check API
本文介绍了在Spring Boot执行器运行状况检查API中启用日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为具有健康检查端点的项目使用Spring boot Actuator API,并通过以下方式启用了该功能:
I am using Spring boot Actuator API for my project the having a health check endpoint, and enabled it by :
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck
被提及此处
现在,我要在/healthcheck
以上的状态失败时启用我的应用程序日志文件中的日志,并从此端点打印整个响应.
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?
推荐答案
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);
}
}
有关执行器端点扩展的更多信息,请此处,位于 4.8.扩展现有端点
More about actuator endpoint extending here, at 4.8. Extending Existing Endpoints
这篇关于在Spring Boot执行器运行状况检查API中启用日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文