HTTP client logging plugin for Ktor client framework with configurable logging formats, levels, and platform-specific logger integrations
npx @tessl/cli install tessl/maven-io-ktor--ktor-client-logging-jvm@3.2.00
# Ktor Client Logging Plugin
1
2
Ktor Client Logging is a powerful plugin that provides comprehensive HTTP request and response logging capabilities for Ktor client applications. It offers configurable logging formats (Default and OkHttp-compatible), multiple log levels, support for custom loggers with platform-specific optimizations, header sanitization for security, and MDC context propagation for coroutines.
3
4
## Package Information
5
6
- **Package Name**: io.ktor:ktor-client-logging-jvm
7
- **Package Type**: maven
8
- **Language**: Kotlin
9
- **Installation**:
10
```kotlin
11
implementation("io.ktor:ktor-client-logging-jvm:3.2.0")
12
```
13
14
## Core Imports
15
16
```kotlin
17
import io.ktor.client.*
18
import io.ktor.client.plugins.logging.*
19
import io.ktor.client.request.*
20
import io.ktor.http.*
21
```
22
23
## Basic Usage
24
25
```kotlin
26
import io.ktor.client.*
27
import io.ktor.client.plugins.logging.*
28
29
val client = HttpClient {
30
install(Logging) {
31
logger = Logger.DEFAULT
32
level = LogLevel.ALL
33
format = LoggingFormat.Default
34
}
35
}
36
```
37
38
## Architecture
39
40
Ktor Client Logging is built around several key components:
41
42
- **Core Plugin**: The main `Logging` plugin that intercepts HTTP calls and coordinates logging
43
- **Configuration System**: `LoggingConfig` class providing fluent DSL for plugin setup
44
- **Logger Interface**: Pluggable logger system with platform-specific implementations
45
- **Log Levels**: Granular control over what information gets logged (NONE, INFO, HEADERS, BODY, ALL)
46
- **Formatting**: Multiple output formats including Default Ktor format and OkHttp-compatible format
47
- **Header Sanitization**: Security features to redact sensitive headers like Authorization
48
- **Platform Integration**: Native integration with SLF4J on JVM, Android Logcat, and console logging on other platforms
49
50
## Capabilities
51
52
### Core Logging Plugin
53
54
Main plugin installation and configuration functionality. Provides the foundation for all HTTP call logging with extensive customization options.
55
56
```kotlin { .api }
57
val Logging: ClientPlugin<LoggingConfig>
58
59
fun HttpClientConfig<*>.install(plugin: ClientPlugin<LoggingConfig>, configure: LoggingConfig.() -> Unit = {})
60
```
61
62
[Core Plugin Configuration](./configuration.md)
63
64
### Logger Interface and Implementations
65
66
Pluggable logger system with platform-specific implementations including SLF4J integration, Android Logcat support, and message length limiting for platforms with log truncation.
67
68
```kotlin { .api }
69
interface Logger {
70
fun log(message: String)
71
72
companion object {
73
val DEFAULT: Logger
74
val SIMPLE: Logger
75
val EMPTY: Logger
76
val ANDROID: Logger // JVM only
77
}
78
}
79
80
class MessageLengthLimitingLogger(
81
private val maxLength: Int = 4000,
82
private val minLength: Int = 3000,
83
private val delegate: Logger = Logger.DEFAULT
84
) : Logger {
85
override fun log(message: String)
86
}
87
```
88
89
[Logger Implementations](./loggers.md)
90
91
### Log Levels and Formats
92
93
Fine-grained control over logging verbosity and output formatting with support for different logging styles and selective information disclosure.
94
95
```kotlin { .api }
96
enum class LogLevel(
97
val info: Boolean,
98
val headers: Boolean,
99
val body: Boolean
100
) {
101
ALL(true, true, true),
102
HEADERS(true, true, false),
103
BODY(true, false, true),
104
INFO(true, false, false),
105
NONE(false, false, false)
106
}
107
108
enum class LoggingFormat {
109
Default,
110
OkHttp
111
}
112
```
113
114
[Levels and Formats](./levels-formats.md)
115
116
### Advanced Configuration Features
117
118
Security and filtering capabilities including header sanitization to prevent sensitive data leakage and request filtering for selective logging.
119
120
```kotlin { .api }
121
class LoggingConfig {
122
var format: LoggingFormat
123
var logger: Logger
124
var level: LogLevel
125
126
fun filter(predicate: (HttpRequestBuilder) -> Boolean)
127
fun sanitizeHeader(placeholder: String = "***", predicate: (String) -> Boolean)
128
129
internal val filters: MutableList<(HttpRequestBuilder) -> Boolean>
130
internal val sanitizedHeaders: MutableList<(String, String) -> String>
131
}
132
```
133
134
[Advanced Configuration](./advanced-config.md)
135
136
### Platform-Specific Features
137
138
MDC context support and platform-optimized logging implementations for different environments including coroutine context propagation.
139
140
```kotlin { .api }
141
// JVM only
142
fun MDCContext(): CoroutineContext.Element
143
144
// Platform-specific MDC context implementations
145
expect fun MDCContext(): CoroutineContext.Element
146
```
147
148
[Platform Features](./platform-features.md)