Ktor 1.6.8 Help

Logging

Ktor client provides the capability to log HTTP calls using the Logging plugin (previously known as feature). This plugin uses the SLF4J library for logging.

Add dependencies

To enable logging, you need to include the following artifacts in the build script:

  • An artifact with the required SLF4J implementation, for example, Logback:

    implementation "ch.qos.logback:logback-classic:$logback_version"
    implementation("ch.qos.logback:logback-classic:$logback_version")
    <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback_version}</version> </dependency>

  • The ktor-client-logging artifact:

    implementation "io.ktor:ktor-client-logging:$ktor_version"
    implementation("io.ktor:ktor-client-logging:$ktor_version")
    <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-logging</artifactId> <version>${ktor_version}</version> </dependency>

Install Logging

To install Logging, pass it to the install function inside a client configuration block:

val client = HttpClient(CIO) { install(Logging) }

Configure Logging

The Logging plugin allows you to configure the desired logger using the logger property and the level of logging using the level property:

package com.example import io.ktor.client.* import io.ktor.client.engine.cio.* import io.ktor.client.features.logging.* import io.ktor.client.request.* import io.ktor.client.statement.* import kotlinx.coroutines.runBlocking fun main() { runBlocking { val client = HttpClient(CIO) { install(Logging) { logger = Logger.DEFAULT level = LogLevel.HEADERS } } val response: HttpResponse = client.get("https://ktor.io/") } }
Last modified: 27 May 2021