-1

我通过添加依赖项( gradle 项目)完成了工作

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

和插件(因为自动配置需要不少于 2.4.2 的引导版本)

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

这很方便,除了添加依赖项之外我无需再做任何事情。但它不可用,因为我的 gitlab 上的管道失败(我不知道为什么)。

是否有任何示例如何使用不依赖logbook-spring-boot-webflux-autoconfigure的日志来逐步记录 spring-webflux 请求/响应?例如插件(我没问题)

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

谢谢!

4

1 回答 1

0

我解决了我的问题。这里配置类

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!!))
                }
            }
        }
    }
}

应用程序.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}

和依赖项(部分)

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")

现在我可以继续使用旧版本的 spring boot

plugins {
    id("org.springframework.boot") version "2.2.4.RELEASE"
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
}
于 2021-03-30T08:37:56.050 回答