HTTP client logging plugin for Ktor client framework with configurable logging formats, levels, and platform-specific logger integrations
—
The Ktor Client Logging plugin provides a pluggable logger system with multiple built-in implementations optimized for different platforms and use cases.
import io.ktor.client.*
import io.ktor.client.plugins.logging.*
import org.slf4j.LoggerFactory // JVM only
import android.util.Log // Android onlyinterface Logger {
fun log(message: String)
companion object
}The core logger interface that all logging implementations must implement.
Outputs a log message using the logger's implementation-specific mechanism.
Parameters:
message: The string message to logval Logger.Companion.DEFAULT: LoggerPlatform-specific default logger implementation:
Logger.SIMPLELogger.SIMPLEJVM Implementation Details:
// Uses SLF4J LoggerFactory
private val delegate = LoggerFactory.getLogger(HttpClient::class.java)
override fun log(message: String) {
delegate.info(message)
}Usage:
Logging {
logger = Logger.DEFAULT
}val Logger.Companion.SIMPLE: LoggerBasic logger implementation that outputs to console using println.
Implementation:
override fun log(message: String) {
println("HttpClient: $message")
}Usage:
Logging {
logger = Logger.SIMPLE
}val Logger.Companion.EMPTY: LoggerNo-operation logger for testing purposes. Discards all log messages.
Usage:
Logging {
logger = Logger.EMPTY // For testing
}val Logger.Companion.ANDROID: LoggerAndroid-optimized logger with the following features:
android.util.Log when availableFeatures:
Usage:
Logging {
logger = Logger.ANDROID
}class MessageLengthLimitingLogger(
private val maxLength: Int = 4000,
private val minLength: Int = 3000,
private val delegate: Logger = Logger.DEFAULT
) : LoggerWrapper logger that breaks long messages into multiple log entries. Particularly useful for platforms with message length restrictions.
Constructor Parameters:
maxLength: Maximum allowed length for a single log message (default: 4000)minLength: If message exceeds maxLength, try to break at newline between minLength and maxLength (default: 3000)delegate: Underlying logger to delegate broken-up messages to (default: Logger.DEFAULT)Methods:
override fun log(message: String)Logs the message, breaking it into chunks if it exceeds maxLength. Uses recursive tail-call optimization for processing long messages.
Breaking Algorithm:
Usage Examples:
// Default configuration
Logging {
logger = MessageLengthLimitingLogger()
}
// Custom length limits
Logging {
logger = MessageLengthLimitingLogger(
maxLength = 2000,
minLength = 1500
)
}
// Custom delegate logger
Logging {
logger = MessageLengthLimitingLogger(
maxLength = 3000,
delegate = Logger.SIMPLE
)
}
// Android-optimized with custom limits
Logging {
logger = MessageLengthLimitingLogger(
maxLength = 3500,
minLength = 2500,
delegate = Logger.ANDROID
)
}You can create custom logger implementations by implementing the Logger interface:
class CustomFileLogger(private val filePath: String) : Logger {
override fun log(message: String) {
File(filePath).appendText("${Date()}: $message\n")
}
}
// Usage
Logging {
logger = CustomFileLogger("/path/to/logfile.txt")
}| Use Case | Recommended Logger | Reason |
|---|---|---|
| JVM Production | Logger.DEFAULT | SLF4J integration with your logging framework |
| Android Apps | Logger.ANDROID | Logcat integration + message length handling |
| Development/Testing | Logger.SIMPLE | Simple console output |
| Unit Tests | Logger.EMPTY | No output, faster tests |
| Long Messages | MessageLengthLimitingLogger | Handles message truncation gracefully |
| Custom Requirements | Custom implementation | Full control over output destination and format |
Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-logging-jvm