CtrlK
BlogDocsLog inGet started
Tessl Logo

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

HTTP client logging plugin for Ktor client framework with configurable logging formats, levels, and platform-specific logger integrations

Pending
Overview
Eval results
Files

loggers.mddocs/

Logger Implementations

The Ktor Client Logging plugin provides a pluggable logger system with multiple built-in implementations optimized for different platforms and use cases.

Required Imports

import io.ktor.client.*
import io.ktor.client.plugins.logging.*
import org.slf4j.LoggerFactory // JVM only
import android.util.Log // Android only

Logger Interface

interface Logger {
    fun log(message: String)
    
    companion object
}

The core logger interface that all logging implementations must implement.

Methods

log(message)

Outputs a log message using the logger's implementation-specific mechanism.

Parameters:

  • message: The string message to log

Built-in Logger Implementations

Default Logger

val Logger.Companion.DEFAULT: Logger

Platform-specific default logger implementation:

  • JVM: Uses SLF4J with HttpClient class logger
  • JavaScript/WASM: Uses Logger.SIMPLE
  • Native/Posix: Uses Logger.SIMPLE

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

Simple Logger

val Logger.Companion.SIMPLE: Logger

Basic logger implementation that outputs to console using println.

Implementation:

override fun log(message: String) {
    println("HttpClient: $message")
}

Usage:

Logging {
    logger = Logger.SIMPLE
}

Empty Logger

val Logger.Companion.EMPTY: Logger

No-operation logger for testing purposes. Discards all log messages.

Usage:

Logging {
    logger = Logger.EMPTY // For testing
}

JVM-Specific Loggers

Android Logger

val Logger.Companion.ANDROID: Logger

Android-optimized logger with the following features:

  • Logcat Integration: Uses Android's android.util.Log when available
  • SLF4J Fallback: Falls back to SLF4J logging when Logcat is unavailable
  • Message Length Limiting: Automatically breaks long messages for Android's log limits
  • Lazy Initialization: Logger is created on first access

Features:

  • Maximum message length: 4000 characters
  • Minimum length for newline breaking: 3000 characters
  • Tag: "Ktor Client"
  • Graceful fallback to SLF4J if Android Log class is not found

Usage:

Logging {
    logger = Logger.ANDROID
}

Message Length Limiting Logger

class MessageLengthLimitingLogger(
    private val maxLength: Int = 4000,
    private val minLength: Int = 3000,
    private val delegate: Logger = Logger.DEFAULT
) : Logger

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

  1. If message ≤ maxLength: log directly
  2. If message > maxLength:
    • Take substring from 0 to maxLength
    • Look for last newline between minLength and maxLength
    • If newline found: break there, skip the newline character
    • If no newline: break at maxLength
    • Log the first part, recursively process remainder

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

Custom Logger Implementation

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

Platform-Specific Considerations

JVM Platform

  • SLF4J Integration: Default logger uses SLF4J, respects your logging configuration
  • Android Support: Special Android logger handles Logcat integration and message limits
  • Thread Safety: All built-in loggers are thread-safe

JavaScript/WASM Platform

  • Console Output: Uses browser console or Node.js console
  • Simple Implementation: Falls back to basic println-style logging

Native/Posix Platform

  • Standard Output: Uses platform's standard output mechanisms
  • Simple Implementation: Falls back to basic println-style logging

Logger Selection Guide

Use CaseRecommended LoggerReason
JVM ProductionLogger.DEFAULTSLF4J integration with your logging framework
Android AppsLogger.ANDROIDLogcat integration + message length handling
Development/TestingLogger.SIMPLESimple console output
Unit TestsLogger.EMPTYNo output, faster tests
Long MessagesMessageLengthLimitingLoggerHandles message truncation gracefully
Custom RequirementsCustom implementationFull control over output destination and format

Install with Tessl CLI

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

docs

advanced-config.md

configuration.md

index.md

levels-formats.md

loggers.md

platform-features.md

tile.json