Hello, how can one create a custom logger plugin l...
# ktor
s
Hello, how can one create a custom logger plugin like logbookt-ktor-client that can be installed like this:
install(LogbookClient)
And which needs to read both request and response body ? Currently it seems a little bit complicated as you can see in this sample: https://github.com/grassehh/issues/blob/main/zalando-logbook-1822/src/test/kotlin/com/grassehh/logbook/LogbookClient.kt The major issue is that for the response, it requires you to read
request.content
which is an internal API. But in general, the fact that you need to deal with ByteReadChannel, handle coroutines etc. makes a lot of boilerplate code. It would be nice also to be able to use the new API for such use case.
m
For client request/response logging ktor itself is providing a lib as well https://ktor.io/docs/client-logging.html
Copy code
HttpClient().config {
    install(Logging) {
        logger = object : Logger {
            override fun log(message: String) {
                // do whatever. e.g.
                KotlinLogging.logger("http.client").info { message }
            }
        }
        level = LogLevel.ALL
    }
}
s
But this feature is very limited and does only provide a message. Logbook instead is a rich and fully configurable library that allows you to do a lot of things. So it needs to be installed as a custom plugin.
a
Instead of using
HttpResponse.content
you can use the
HttpResponse.bodyAsChannel
method to get the response body as a channel. As I can see, the current implementation already exposes the plugin object which can be installed into the client. You can convert the current implementation to use the new plugin API by following the documentation.