CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-client-logging-linuxx64

Kotlin multiplatform HTTP client logging plugin for LinuxX64 target that provides comprehensive request and response logging capabilities.

Pending
Overview
Eval results
Files

logger-implementations.mddocs/

Logger Implementations

Platform-specific logger implementations providing integration with system logging frameworks including SLF4J, Android Logcat, and console output.

Capabilities

Logger Interface

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
}

Default Platform Loggers

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: Logger

Platform-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)

Built-in Logger Implementations

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: Logger

Usage 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
    }
}

JVM-Specific Loggers

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
) : Logger

JVM 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
        )
    }
}

Custom Logger Implementation

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()
    }
}

Platform-Specific Details

LinuxX64/POSIX Platform (Target)

On LinuxX64 and other POSIX platforms:

  • Logger.DEFAULT returns Logger.SIMPLE
  • Uses standard console output via println()
  • No special Android or JVM-specific features
  • Simple and lightweight implementation

JVM Platform

On JVM platform:

  • Logger.DEFAULT uses SLF4J integration
  • Additional Logger.ANDROID for Android compatibility
  • MessageLengthLimitingLogger for message length handling
  • Full SLF4J ecosystem integration

JS/WASM Platform

On JavaScript and WebAssembly platforms:

  • Logger.DEFAULT returns Logger.SIMPLE
  • Uses console output appropriate for the platform
  • Lightweight implementation for web environments

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-client-logging-linuxx64

docs

configuration.md

index.md

levels-and-control.md

logger-implementations.md

output-formatting.md

tile.json