【问题标题】:Configure the logs which happen before application startup配置在应用程序启动之前发生的日志
【发布时间】:2021-03-07 00:40:47
【问题描述】:

在我的应用程序中,我使用 spring WebClient 在应用程序启动之前从其中一项服务中获取一些数据。

spring WebClient 正在控制台上记录这些数据。我不希望记录这些数据,因为它是机密的。由于某些原因,仅在应用程序启动时才需要获取此数据。我想禁用这些日志。

这是示例主应用程序代码

@SpringBootApplication
@ConfigurationPropertiesScan
class DemoApplication {

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {

            val webClient = WebClient.builder().build()

            val data = webClient.get()
                .uri("http://localhost:8080/home")
                .retrieve()
                .bodyToMono(Response::class.java)
                .block()?.data

            SpringApplicationBuilder(DemoApplication::class.java).run(*args)
        }
    }
}

data class Response(val data: String)

当我运行此应用程序时,webClient 编解码器将记录以下响应

14:10:16.944 [reactor-http-nio-1] 调试 org.springframework.http.codec.json.Jackson2JsonDecoder - [4ef27d66] 解码 [Response(data=hello)] 14:10:16.944 [reactor-http-nio-1] 调试 reactor.netty.resources.PooledConnectionProvider - [id: 0x9a13da08, L:/127.0.0.1:62737 - R:localhost/127.0.0.1:8080] onStateChange(GET {uri=/home, connection=PooledConnection{channel=[id: 0x9a13da08, L:/127.0.0.1:62737 - R:localhost/127.0.0.1:8080]}}, [response_completed])

我已尝试通过将它们的日志级别更改为应用程序 yaml 中的信息来禁用这些日志,如下所示,但这不起作用,因为这甚至在应用程序启动之前就发生了。

logging:
  level:
    org:
      springframework:
        web: info
        http:
          codec:
            json: info

有没有人有任何其他方法来禁用这些应用程序启动或 webClient 编解码器日志?

【问题讨论】:

    标签: spring spring-boot spring-webflux


    【解决方案1】:

    向 Web 客户端添加自定义编解码器对我有用。在这个自定义编解码器中,我正在反序列化响应。

    这是自定义编解码器

    class MyMessageReader: HttpMessageReader<Response> {
        override fun getReadableMediaTypes(): MutableList<MediaType> {
            return mutableListOf()
        }
    
        override fun canRead(elementType: ResolvableType, mediaType: MediaType?): Boolean {
            return true
        }
    
        override fun read(elementType: ResolvableType, message: ReactiveHttpInputMessage, hints: MutableMap<String, Any>): Flux<Response> {
            return Flux.just(Jackson2JsonDecoder().objectMapper.readValue(message.toString(), Response::class.java))
        }
    
        override fun readMono(elementType: ResolvableType, message: ReactiveHttpInputMessage, hints: MutableMap<String, Any>): Mono<Response> {
            return message.body.map {
                val count = it.readableByteCount()
                val str = ByteArray(count)
                var count1 = 0
                while (count1 < count) {
                    str[count1] = it.read()
                    count1++
                }
    
                Jackson2JsonDecoder().objectMapper.readValue(str, Response::class.java)
            }.toMono()
        }
    }
    

    要将编解码器注册到 Web 客户端:

    WebClient.builder().codecs {
                    it.customCodecs().register(MyMessageReader())
                }.build()
    

    以下是更改后主应用程序的外观:

    @SpringBootApplication
    @ConfigurationPropertiesScan
    class DemoApplication {
    
        companion object {
            @JvmStatic
            fun main(args: Array<String>) {
    
                val webClient = WebClient.builder().codecs {
                    it.customCodecs().register(MyMessageReader())
                }.build()
    
                val data = webClient.get()
                    .uri("http://localhost:8080/home")
                    .retrieve()
                    .bodyToMono(Response::class.java)
                    .block()
    
                SpringApplicationBuilder(DemoApplication::class.java).run(*args)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 2017-09-02
      • 2018-09-07
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多