HTTP client logging plugin for Ktor client framework with configurable logging formats, levels, and platform-specific logger integrations
npx @tessl/cli install tessl/maven-io-ktor--ktor-client-logging-jvm@3.2.0Ktor Client Logging is a powerful plugin that provides comprehensive HTTP request and response logging capabilities for Ktor client applications. It offers configurable logging formats (Default and OkHttp-compatible), multiple log levels, support for custom loggers with platform-specific optimizations, header sanitization for security, and MDC context propagation for coroutines.
implementation("io.ktor:ktor-client-logging-jvm:3.2.0")import io.ktor.client.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.http.*import io.ktor.client.*
import io.ktor.client.plugins.logging.*
val client = HttpClient {
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.ALL
format = LoggingFormat.Default
}
}Ktor Client Logging is built around several key components:
Logging plugin that intercepts HTTP calls and coordinates loggingLoggingConfig class providing fluent DSL for plugin setupMain plugin installation and configuration functionality. Provides the foundation for all HTTP call logging with extensive customization options.
val Logging: ClientPlugin<LoggingConfig>
fun HttpClientConfig<*>.install(plugin: ClientPlugin<LoggingConfig>, configure: LoggingConfig.() -> Unit = {})Pluggable logger system with platform-specific implementations including SLF4J integration, Android Logcat support, and message length limiting for platforms with log truncation.
interface Logger {
fun log(message: String)
companion object {
val DEFAULT: Logger
val SIMPLE: Logger
val EMPTY: Logger
val ANDROID: Logger // JVM only
}
}
class MessageLengthLimitingLogger(
private val maxLength: Int = 4000,
private val minLength: Int = 3000,
private val delegate: Logger = Logger.DEFAULT
) : Logger {
override fun log(message: String)
}Fine-grained control over logging verbosity and output formatting with support for different logging styles and selective information disclosure.
enum class LogLevel(
val info: Boolean,
val headers: Boolean,
val body: Boolean
) {
ALL(true, true, true),
HEADERS(true, true, false),
BODY(true, false, true),
INFO(true, false, false),
NONE(false, false, false)
}
enum class LoggingFormat {
Default,
OkHttp
}Security and filtering capabilities including header sanitization to prevent sensitive data leakage and request filtering for selective logging.
class LoggingConfig {
var format: LoggingFormat
var logger: Logger
var level: LogLevel
fun filter(predicate: (HttpRequestBuilder) -> Boolean)
fun sanitizeHeader(placeholder: String = "***", predicate: (String) -> Boolean)
internal val filters: MutableList<(HttpRequestBuilder) -> Boolean>
internal val sanitizedHeaders: MutableList<(String, String) -> String>
}MDC context support and platform-optimized logging implementations for different environments including coroutine context propagation.
// JVM only
fun MDCContext(): CoroutineContext.Element
// Platform-specific MDC context implementations
expect fun MDCContext(): CoroutineContext.Element