Logging plugin for Ktor HTTP client that provides comprehensive request and response logging capabilities with configurable loggers, levels, and sanitization for sensitive data
npx @tessl/cli install tessl/maven-io-ktor--ktor-client-logging@3.2.0Ktor Client Logging is a multiplatform plugin that provides comprehensive HTTP request and response logging capabilities for Ktor HTTP clients. It offers configurable logging levels, multiple output formats, header sanitization for security, and platform-specific logger implementations.
implementation("io.ktor:ktor-client-logging:3.2.0")import io.ktor.client.plugins.logging.*For HttpClient configuration:
import io.ktor.client.*
import io.ktor.client.plugins.logging.*import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.logging.*
val client = HttpClient(CIO) {
install(Logging) {
level = LogLevel.HEADERS
logger = Logger.DEFAULT
}
}val client = HttpClient(CIO) {
install(Logging) {
level = LogLevel.ALL
logger = Logger.DEFAULT
format = LoggingFormat.OkHttp
// Filter specific requests
filter { request ->
request.url.host == "api.example.com"
}
// Sanitize sensitive headers
sanitizeHeader { header ->
header == "Authorization" || header == "X-API-Key"
}
}
}The Ktor Client Logging plugin is built around several key components:
ClientPlugin<LoggingConfig>LoggingConfig class with @KtorDsl annotationLogLevel) controlling what information is loggedLogger interface with platform-specific implementationsOutgoingContent typesCore plugin installation and configuration functionality with comprehensive options for customizing logging behavior.
/**
* A client's plugin that provides the capability to log HTTP calls
*/
val Logging: ClientPlugin<LoggingConfig>
/**
* Configures and installs Logging in HttpClient
*/
fun HttpClientConfig<*>.Logging(block: LoggingConfig.() -> Unit = {})Granular control over what information gets logged, from basic request info to full request/response bodies.
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
}Abstracted logging interface with platform-specific implementations supporting SLF4J, Android Logcat, console logging, and custom loggers.
interface Logger {
fun log(message: String)
companion object
}
val Logger.Companion.DEFAULT: Logger
val Logger.Companion.SIMPLE: Logger
val Logger.Companion.EMPTY: LoggerPlatform-specific (JVM only):
val Logger.Companion.ANDROID: Logger
class MessageLengthLimitingLogger(
private val maxLength: Int = 4000,
private val minLength: Int = 3000,
private val delegate: Logger = Logger.DEFAULT
) : LoggerInternal utility functions and components for content handling, binary detection, and log formatting.
internal class HttpClientCallLogger(private val logger: Logger)
internal suspend fun ByteReadChannel.tryReadText(charset: Charset): String?
internal fun Appendable.logHeaders(
headers: Set<Map.Entry<String, List<String>>>,
sanitizedHeaders: List<SanitizedHeader>
)@KtorDsl
class LoggingConfig {
/**
* A general format for logging requests and responses
*/
var format: LoggingFormat
/**
* Specifies a Logger instance
*/
var logger: Logger
/**
* Specifies the logging level
*/
var level: LogLevel
/**
* Allows you to filter log messages for calls matching a predicate
*/
fun filter(predicate: (HttpRequestBuilder) -> Boolean)
/**
* Allows you to sanitize sensitive headers to avoid their values appearing in the logs
*/
fun sanitizeHeader(placeholder: String = "***", predicate: (String) -> Boolean)
}