【问题标题】:How to instantiate logbook in spring boot app如何在 Spring Boot 应用程序中实例化日志
【发布时间】:2021-07-27 04:32:41
【问题描述】:

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

文件说忽略健康检查请求,请像这样连接日志:

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

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

【问题讨论】:

    标签: java spring spring-boot logbook


    【解决方案1】:

    其实很简单。只需在 @Configuration 类中创建一个 Logbook 类型的 bean:

    @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;
        }
    }
    

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

    <dependency>
        <groupId>org.zalando</groupId>
        <artifactId>logbook-spring-boot-starter</artifactId>
    </dependency>
    

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

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

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 2018-06-05
      • 2020-01-09
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 2017-11-22
      相关资源
      最近更新 更多