Log the client requests

Estimated reading time: 1 minute

You might want to log client requests: and the Call Logging feature does just that. It uses the ApplicationEnvironment.log (LoggerFactory.getLogger("Application")) that uses slf4j so you can easily configure the output. For more information on logging in Ktor, please check the logging in ktor page.

This feature is defined in the class io.ktor.features.CallLogging and no additional artifacts are required.

Basic usage

The basic unconfigured feature logs every request using the level TRACE:



This feature allows configuring the log level and filtering the requests that are being logged:

install(CallLogging) {
    level = Level.INFO
    filter { call -> call.request.path().startsWith("/section1") }
    filter { call -> call.request.path().startsWith("/section2") }
    // ...

The filter methods keeps is a whitelist list of filters. If no filters are defined, everything is logged. And if there are filters, if any of them returns true, the call will be logged.

In the example, it will log both: /section1/* and /section2/* requests.