or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

configuration.mdindex.mdloggers.mdutilities.md
tile.json

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

Logging plugin for Ktor HTTP client that provides comprehensive request and response logging capabilities with configurable loggers, levels, and sanitization for sensitive data

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/io.ktor/ktor-client-logging@3.2.x

To install, run

npx @tessl/cli install tessl/maven-io-ktor--ktor-client-logging@3.2.0

index.mddocs/

Ktor Client Logging

Ktor 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.

Package Information

  • Package Name: ktor-client-logging
  • Package Type: maven
  • Language: Kotlin (Multiplatform)
  • Installation: implementation("io.ktor:ktor-client-logging:3.2.0")

Core Imports

import io.ktor.client.plugins.logging.*

For HttpClient configuration:

import io.ktor.client.*
import io.ktor.client.plugins.logging.*

Basic Usage

Simple Setup

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

Advanced Configuration

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

Architecture

The Ktor Client Logging plugin is built around several key components:

  • Plugin System: Integrates with Ktor's client plugin architecture through ClientPlugin<LoggingConfig>
  • Configuration DSL: Type-safe configuration using LoggingConfig class with @KtorDsl annotation
  • Logging Levels: Enum-based level system (LogLevel) controlling what information is logged
  • Logger Interface: Abstracted logging through Logger interface with platform-specific implementations
  • Content Handling: Smart content detection and logging for different OutgoingContent types
  • Pipeline Integration: Hooks into Ktor's request/response pipeline for comprehensive monitoring
  • Security Features: Built-in header sanitization and request filtering capabilities

Capabilities

Plugin Installation and Configuration

Core 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 = {})

Configuration

Logging Levels and Control

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
}

Configuration

Logger Interface and Implementations

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

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

Loggers

Utilities and Internal Components

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

Utilities

Core Types

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