在Spring Boot执行器运行状况检查API中启用日志记录

时间:2019-03-04 05:44:31

标签: java spring-boot logging spring-boot-actuator

我正在使用Spring boot Actuator API进行健康检查端点,并通过以下方式启用了它:

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

此处提到:https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-monitoring.html

现在,当上述运行状况检查的状态失败时,我想在我的应用程序日志文件中启用日志,并从该端点打印整个响应。

您能帮忙还是建议实现此目的的正确方法?

2 个答案:

答案 0 :(得分:0)

最好的方法是使用@EndpointWebExtension扩展执行器端点。您可以执行以下操作;

QuoteDate

有关致动器端点延伸here的更多信息,位于 4.8。扩展现有端点

答案 1 :(得分:0)

如果使用 Webflux,这对我在 Kotlin 中的示例有用

@Component
@EndpointWebExtension(endpoint = HealthEndpoint::class)
class LoggingReactiveHealthEndpointWebExtension(
      registry: ReactiveHealthContributorRegistry,
      groups: HealthEndpointGroups
) : ReactiveHealthEndpointWebExtension(registry, groups) {

companion object {
    private val logger = LoggerFactory.getLogger(LoggingReactiveHealthEndpointWebExtension::class.java)
}

override fun health(
    apiVersion: ApiVersion?,
    securityContext: SecurityContext?,
    showAll: Boolean,
    vararg path: String?
): Mono<WebEndpointResponse<out HealthComponent>> {
    val health = super.health(apiVersion, securityContext, showAll, *path)

    return health.doOnNext {
        if (it.body.status == UP) {
            logger.info("Health status: {}, {}", it.body.status, ObjectMapper().writeValueAsString(it.body))
        }
    }
}

}