Hi everybody, I’d like to log all requests incommi...
# ktor
h
Hi everybody, I’d like to log all requests incomming in Ktor with header and body. Currently, I use :
Copy code
install(CallLogging) {
        level = <http://Level.INFO|Level.INFO>
        filter { call -> call.request.path().startsWith("/api/v1") }
        callIdMdc()
    }
And I have some logs like :
200 OK: GET - /api/v1/health
but I’d like to have more details like the header and body because in some case I have log like
Unhandled: POST - /api/v1/catalogEdito/reunion/store
whereas this endpoint exists and returns some datas for certain type of client. So I think I need to have more details to debug and understand what is going on ! Do you know how I can do it please ?
j
perhaps use mdc, similar to how you have used callIdMdc() but picking out the features you are interested in:
Copy code
mdc(LoggingKey("header")) {
  call: ApplicationCall -> call.request.header("someHeaderName")
}
you might need to configure your logging framework to include the mdc information
https://logback.qos.ch/manual/layouts.html#mdc if you use logback for example
h
Hi @Jonathan Mew I have tested your proposal and it works perfectly well ! Thanks. Please find below my own implementation to log all the headers of a request.
Copy code
install(CallLogging) {
        level = <http://Level.INFO|Level.INFO>
        filter { call -> call.request.path().startsWith("/api/v1") }
        callIdMdc()
        requestHeadersMdc()
    }

fun CallLogging.Configuration.requestHeadersMdc(name: String = "Headers") {
    mdc(name) { call ->
        call.request.headers.entries()
            .map {
                it.key to it.value.joinToString(",")
            }.joinToString(";") {
                "${it.first}:${it.second}"
            }
    }
}