How to instantiate logbook in spring boot app

10.5k views Asked by At

I have a simple Spring boot app with logbook-spring-boot-starter dependency of the logbook library.

The document says for ignoring health check request, wire up the logbook like this:

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

It may sound naive, But I don't know where should wire it up. Should I put it in any specific class or method with specific annotation? An example would help.

1

There are 1 answers

0
Nikolas Charalambidis On BEST ANSWER

It is really simple. Just create a bean of the Logbook type in a @Configuration class:

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

Of course, the specific library must be present on the classpath:

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

The README.md, section Spring Boot Starter says the following and provides a table of configurable integration points.

Every bean can be overridden and customized if needed