Logbook on spring web-flux

2.8k Views Asked by At

I made work it through add dependency ( gradle project)

dependencies {
...
    implementation("org.zalando:logbook-spring-boot-webflux-autoconfigure:2.5.0")
}

and plugins (because that autoconfigure requires not less than 2.4.2 boot version)

id("org.springframework.boot") version "2.4.2" apply false
id("io.spring.dependency-management") version "1.0.11.RELEASE"

It is convenient and nothing more I must to do except add dependencies. But it is not usable because of failing pipelines on my gitlab(I have no idea why).

Is there any example how to use logbook without logbook-spring-boot-webflux-autoconfigure dependency to log spring-webflux requests/responses step by step? for example for plugins (with them I am ok)

plugins {
    id("org.springframework.boot") version "2.2.4.RELEASE"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
}

Thank you!

2

There are 2 best solutions below

0
KROGOT88 On

I solved my problem. Here config class

package config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
import org.springframework.boot.web.embedded.netty.NettyServerCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.zalando.logbook.Logbook
import org.zalando.logbook.netty.LogbookServerHandler
import reactor.netty.Connection
import reactor.netty.http.server.HttpServer
import reactor.netty.tcp.TcpServer

@Configuration(proxyBeanMethods = false)
class LogbookFluxConfig {

    companion object {
        const val CUSTOMIZER_NAME = "logbookNettyServerCustomizer"
    }

    @Bean
    @ConditionalOnProperty(name = ["logbook.filter.enabled"], havingValue = "true", matchIfMissing = true)
    @ConditionalOnMissingBean(name = [CUSTOMIZER_NAME])
    @ConditionalOnClass(HttpServer::class)
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
    fun logbookNettyServerCustomizer(logbook: Logbook?): NettyServerCustomizer {
        return NettyServerCustomizer { httpServer: HttpServer ->
            httpServer.tcpConfiguration { tcpServer: TcpServer ->
                tcpServer.doOnConnection { connection: Connection ->
                    connection.addHandlerLast(LogbookServerHandler(logbook!!))
                }
            }
        }
    }
}

application.yml

logging:
  level:        
    org:
      zalando:
        logbook: TRACE
logbook:
  include: /api/**
  filter.enabled: ${LOGBOOK_FILTER_ENABLED:true}
  format.style: http
  obfuscate.write.category: http.wire-log
  write:
    chunk-size: ${LOGBOOK_CHUNK_SIZE:1000}
    max-body-size: ${LOGBOOK_WRITE_MAX_BODY_SIZE:100000}
    level: ${LOGBOOK_WRITE_LEVEL:INFO}

and dependencies (part)

implementation("org.zalando:logbook-spring-boot-autoconfigure:2.5.0") {
        exclude(group = "javax.servlet", module = "javax.servlet-api")
    }
    implementation("org.zalando:logbook-netty:2.5.0")

and now I can stay on the old version of spring boot

plugins {
    id("org.springframework.boot") version "2.2.4.RELEASE"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
}
0
cane On

add dependencies:

implementation("org.zalando:logbook-spring-boot-webflux-autoconfigure:2.14.0")
implementation("org.zalando:logbook-netty:2.14.0")

and set the log level trace for default slf4j logger category org.zalando.logbook.Logbook%

logging.level.org.zalando.logbook: TRACE