HTTP client logging plugin for Ktor client framework with configurable logging formats, levels, and platform-specific logger integrations
—
The Ktor Client Logging plugin provides flexible configuration options for customizing HTTP request and response logging behavior.
import io.ktor.client.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.request.*
import io.ktor.http.*val Logging: ClientPlugin<LoggingConfig>The main plugin object that provides HTTP call logging capabilities. Install using the install function in your HttpClient configuration.
fun HttpClientConfig<*>.install(plugin: ClientPlugin<LoggingConfig>, configure: LoggingConfig.() -> Unit = {})Extension function for installing and configuring the logging plugin with a DSL block.
Usage Example:
val client = HttpClient {
Logging {
logger = Logger.DEFAULT
level = LogLevel.ALL
format = LoggingFormat.OkHttp
}
}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>
}Configuration DSL class that provides all plugin customization options.
Controls the overall output format for logged requests and responses.
LoggingFormat.DefaultLoggingFormat.Default, LoggingFormat.OkHttpLogging {
format = LoggingFormat.OkHttp
}Specifies the logger implementation to use for output.
Logger.DEFAULTLogger implementationLogging {
logger = Logger.ANDROID // JVM only
// or
logger = Logger.SIMPLE
// or
logger = MessageLengthLimitingLogger(maxLength = 2000)
}Controls the verbosity of logging output.
LogLevel.HEADERSLogLevel.NONE, LogLevel.INFO, LogLevel.HEADERS, LogLevel.BODY, LogLevel.ALLLogging {
level = LogLevel.ALL
}Allows selective logging based on request characteristics.
fun filter(predicate: (HttpRequestBuilder) -> Boolean)Parameters:
predicate: Function that returns true if the request should be loggedUsage Examples:
Logging {
// Only log requests to specific hosts
filter { request ->
request.url.host in listOf("api.example.com", "auth.example.com")
}
// Only log POST requests
filter { request -> request.method == HttpMethod.Post }
// Multiple filters can be added
filter { request -> request.url.pathSegments.contains("api") }
}Sanitizes sensitive headers to prevent them from appearing in logs.
fun sanitizeHeader(placeholder: String = "***", predicate: (String) -> Boolean)Parameters:
placeholder: String to replace sensitive header values (default: "***")predicate: Function that returns true if the header should be sanitizedUsage Examples:
Logging {
// Sanitize Authorization header
sanitizeHeader { header -> header == HttpHeaders.Authorization }
// Sanitize multiple headers with custom placeholder
sanitizeHeader("████") { header ->
header in listOf(HttpHeaders.Authorization, "X-API-Key", "X-Secret-Token")
}
// Sanitize headers matching patterns
sanitizeHeader { header -> header.startsWith("X-Private-") }
}val client = HttpClient(CIO) {
install(Logging) {
// Set format
format = LoggingFormat.OkHttp
// Choose logger
logger = MessageLengthLimitingLogger(
maxLength = 3000,
delegate = Logger.DEFAULT
)
// Set logging level
level = LogLevel.ALL
// Filter requests
filter { request ->
request.url.host == "api.production.com"
}
filter { request ->
request.method != HttpMethod.Options
}
// Sanitize sensitive headers
sanitizeHeader { it == HttpHeaders.Authorization }
sanitizeHeader("████") { it.startsWith("X-API-") }
sanitizeHeader("[REDACTED]") { it.contains("secret", ignoreCase = true) }
}
}Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-logging-jvm