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

Ama*_*ngh 5 java logging spring-boot spring-boot-actuator

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

management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck
Run Code Online (Sandbox Code Playgroud)

这里提到

现在,当上述状态/healthcheck失败时,我想在我的应用程序日志文件中启用日志,并从此端点打印整个响应。

实现此目的的正确方法是什么?

bur*_*ete 3

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

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

有关执行器端点扩展的更多信息,请参见4.8 。扩展现有端点

  • 在“spring-boot-actuator 2.2.5”中,“HealthEndpoint.class”的扩展似乎已经在“org.springframework.boot.actuate.autoconfigure.health.HealthEndpointWebExtensionConfiguration”中存在/定义为“org.springframework.class”类型的Bean。 springframework.boot.actuate.health.HealthEndpointWebExtension` 。不允许同一“HealthEndpoint”有多个扩展。Spring Boot 应用程序无法启动。这段代码对任何人有用吗? (3认同)