如何在 Spring Boot 应用程序中实例化日志

Mos*_*afa 6 java spring spring-boot logbook

我有一个简单的 Spring boot 应用程序,logbook-spring-boot-starter依赖于日志库。

该文件指出,要忽略健康检查请求,请像这样连接日志:

Logbook logbook = Logbook.builder()
    .condition(exclude(
        requestTo("/health"),
        requestTo("/admin/**"),
        contentType("application/octet-stream"),
        header("X-Secret", newHashSet("1", "true")::contains)))
    .build();
Run Code Online (Sandbox Code Playgroud)

这听起来可能很天真,但我不知道应该在哪里连接它。我应该将它放在带有特定注释的任何特定类或方法中吗?举个例子会有帮助。

Nik*_*las 10

这真的很简单。Logbook只需在类中创建一个该类型的 bean @Configuration

@Configuration
public class LogbookConfiguration {

    @Bean
    public Logbook logbook() {
        Logbook logbook = Logbook.builder()
            .condition(exclude(
                requestTo("/health"),
                requestTo("/admin/**"),
                contentType("application/octet-stream"),
                header("X-Secret", newHashSet("1", "true")::contains)))
            .build();
        return logbook;
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,特定的库必须存在于类路径中:

<dependency>
    <groupId>org.zalando</groupId>
    <artifactId>logbook-spring-boot-starter</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

README.mdSpring Boot Starter 部分说明了以下内容并提供了可配置集成点的表。

如果需要,每个 bean 都可以被覆盖和定制