Aftab Shaikh
Aftab Shaikh

Reputation: 11

Configure the logs which happen before application startup

In my application I fetch some data from one of the service before the application startup using spring WebClient.

The spring WebClient is logging these data on the console. I don't want this data to be logged as it is confidential. This data needs to be fetched only on application startup due to some reasons. I want to disable these logs.

This is sample main application code

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

And when I run this application following Response will be logged by the webClient codecs

14:10:16.944 [reactor-http-nio-1] DEBUG org.springframework.http.codec.json.Jackson2JsonDecoder - [4ef27d66] Decoded [Response(data=hello)]
14:10:16.944 [reactor-http-nio-1] DEBUG 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])

I have tried disabling these logs by changing their log level to info in the application yaml as shown below but that does not work because this happens even before the application starts.

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

Does anyone have any other approach of disabling these application startup or the webClient codec logs?

Upvotes: 1

Views: 412

Answers (1)

Aftab Shaikh
Aftab Shaikh

Reputation: 11

Adding a custom codec to the web client worked for me. In this custom codec I am deserializing the response.

Here's the custom codec

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

To register the codec into web client:

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

Here's how the main application looks like after the change:

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

Upvotes: 0

Related Questions