Kotlin multiplatform HTTP client logging plugin for LinuxX64 target that provides comprehensive request and response logging capabilities.
—
Platform-specific logger implementations providing integration with system logging frameworks including SLF4J, Android Logcat, and console output.
Base interface for all logging implementations.
/**
* Base interface for HTTP client loggers
*/
interface Logger {
/**
* Log a message to the configured output
* @param message The message to log
*/
fun log(message: String)
companion object
}Platform-specific default logger implementations.
/**
* Platform-specific default logger (expect/actual implementation)
* - JVM: SLF4J-based logger using HttpClient class logger
* - LinuxX64/POSIX: Simple console logger
* - JS/WASM: Simple console logger
*/
val Logger.Companion.DEFAULT: LoggerPlatform-Specific Behavior:
// JVM Implementation (uses SLF4J)
val Logger.Companion.DEFAULT: Logger // -> SLF4J logger for HttpClient class
// LinuxX64/POSIX Implementation (target platform)
val Logger.Companion.DEFAULT: Logger // -> Logger.SIMPLE (console output)
// JS/WASM Implementation
val Logger.Companion.DEFAULT: Logger // -> Logger.SIMPLE (console output)Common logger implementations available on all platforms.
/**
* Simple logger using println for output
* Prefixes messages with "HttpClient: "
*/
val Logger.Companion.SIMPLE: Logger
/**
* No-op logger that discards all messages
* Useful for testing or disabling logging
*/
val Logger.Companion.EMPTY: LoggerUsage Examples:
import io.ktor.client.*
import io.ktor.client.plugins.logging.*
// Use platform default logger
val client = HttpClient {
install(Logging) {
logger = Logger.DEFAULT
}
}
// Use simple console logger
val client = HttpClient {
install(Logging) {
logger = Logger.SIMPLE
}
}
// Disable logging output
val client = HttpClient {
install(Logging) {
logger = Logger.EMPTY
}
}Additional logger implementations available only on JVM platform.
/**
* Android-optimized logger with Logcat support
* - Uses Android Log.i() when available
* - Falls back to SLF4J when not on Android
* - Automatically breaks long messages for Android's 4068 character limit
* Available only on JVM platform
*/
val Logger.Companion.ANDROID: Logger
/**
* Logger that breaks up long messages for compatibility
* Useful for platforms with message length restrictions
* Available only on JVM platform
*
* @param maxLength Maximum message length (default: 4000)
* @param minLength Minimum length before attempting line break (default: 3000)
* @param delegate Underlying logger to use for output (default: Logger.DEFAULT)
*/
class MessageLengthLimitingLogger(
private val maxLength: Int = 4000,
private val minLength: Int = 3000,
private val delegate: Logger = Logger.DEFAULT
) : LoggerJVM Usage Examples:
// Android-optimized logging (JVM only)
val client = HttpClient {
install(Logging) {
logger = Logger.ANDROID
}
}
// Custom message length limiting (JVM only)
val client = HttpClient {
install(Logging) {
logger = MessageLengthLimitingLogger(
maxLength = 2000,
minLength = 1500,
delegate = Logger.SIMPLE
)
}
}Implement the Logger interface for custom logging behavior.
/**
* Custom logger implementation example
*/
class CustomLogger(private val prefix: String) : Logger {
override fun log(message: String) {
// Custom logging logic
println("[$prefix] $message")
}
}Custom Logger Usage:
// File-based logger example
class FileLogger(private val filePath: String) : Logger {
override fun log(message: String) {
File(filePath).appendText("${System.currentTimeMillis()}: $message\n")
}
}
// Structured logger example
class JsonLogger : Logger {
override fun log(message: String) {
val logEntry = mapOf(
"timestamp" to System.currentTimeMillis(),
"level" to "INFO",
"source" to "HttpClient",
"message" to message
)
println(Json.encodeToString(logEntry))
}
}
// Usage
val client = HttpClient {
install(Logging) {
logger = FileLogger("/var/log/http-client.log")
// or
logger = JsonLogger()
}
}On LinuxX64 and other POSIX platforms:
Logger.DEFAULT returns Logger.SIMPLEprintln()On JVM platform:
Logger.DEFAULT uses SLF4J integrationLogger.ANDROID for Android compatibilityMessageLengthLimitingLogger for message length handlingOn JavaScript and WebAssembly platforms:
Logger.DEFAULT returns Logger.SIMPLEInstall with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-logging-linuxx64