0
# Core Plugin Configuration
1
2
The Ktor Client Logging plugin provides flexible configuration options for customizing HTTP request and response logging behavior.
3
4
## Required Imports
5
6
```kotlin
7
import io.ktor.client.*
8
import io.ktor.client.plugins.logging.*
9
import io.ktor.client.request.*
10
import io.ktor.http.*
11
```
12
13
## Plugin Installation
14
15
```kotlin { .api }
16
val Logging: ClientPlugin<LoggingConfig>
17
```
18
19
The main plugin object that provides HTTP call logging capabilities. Install using the `install` function in your HttpClient configuration.
20
21
### Configuration Extension
22
23
```kotlin { .api }
24
fun HttpClientConfig<*>.install(plugin: ClientPlugin<LoggingConfig>, configure: LoggingConfig.() -> Unit = {})
25
```
26
27
Extension function for installing and configuring the logging plugin with a DSL block.
28
29
**Usage Example:**
30
31
```kotlin
32
val client = HttpClient {
33
Logging {
34
logger = Logger.DEFAULT
35
level = LogLevel.ALL
36
format = LoggingFormat.OkHttp
37
}
38
}
39
```
40
41
## Configuration Class
42
43
```kotlin { .api }
44
class LoggingConfig {
45
var format: LoggingFormat
46
var logger: Logger
47
var level: LogLevel
48
49
fun filter(predicate: (HttpRequestBuilder) -> Boolean)
50
fun sanitizeHeader(placeholder: String = "***", predicate: (String) -> Boolean)
51
52
internal val filters: MutableList<(HttpRequestBuilder) -> Boolean>
53
internal val sanitizedHeaders: MutableList<(String, String) -> String>
54
}
55
```
56
57
Configuration DSL class that provides all plugin customization options.
58
59
### Properties
60
61
#### format: LoggingFormat
62
Controls the overall output format for logged requests and responses.
63
64
- **Default Value**: `LoggingFormat.Default`
65
- **Options**: `LoggingFormat.Default`, `LoggingFormat.OkHttp`
66
67
```kotlin
68
Logging {
69
format = LoggingFormat.OkHttp
70
}
71
```
72
73
#### logger: Logger
74
Specifies the logger implementation to use for output.
75
76
- **Default Value**: `Logger.DEFAULT`
77
- **Options**: Any `Logger` implementation
78
79
```kotlin
80
Logging {
81
logger = Logger.ANDROID // JVM only
82
// or
83
logger = Logger.SIMPLE
84
// or
85
logger = MessageLengthLimitingLogger(maxLength = 2000)
86
}
87
```
88
89
#### level: LogLevel
90
Controls the verbosity of logging output.
91
92
- **Default Value**: `LogLevel.HEADERS`
93
- **Options**: `LogLevel.NONE`, `LogLevel.INFO`, `LogLevel.HEADERS`, `LogLevel.BODY`, `LogLevel.ALL`
94
95
```kotlin
96
Logging {
97
level = LogLevel.ALL
98
}
99
```
100
101
### Methods
102
103
#### filter(predicate)
104
Allows selective logging based on request characteristics.
105
106
```kotlin { .api }
107
fun filter(predicate: (HttpRequestBuilder) -> Boolean)
108
```
109
110
**Parameters:**
111
- `predicate`: Function that returns `true` if the request should be logged
112
113
**Usage Examples:**
114
115
```kotlin
116
Logging {
117
// Only log requests to specific hosts
118
filter { request ->
119
request.url.host in listOf("api.example.com", "auth.example.com")
120
}
121
122
// Only log POST requests
123
filter { request -> request.method == HttpMethod.Post }
124
125
// Multiple filters can be added
126
filter { request -> request.url.pathSegments.contains("api") }
127
}
128
```
129
130
#### sanitizeHeader(placeholder, predicate)
131
Sanitizes sensitive headers to prevent them from appearing in logs.
132
133
```kotlin { .api }
134
fun sanitizeHeader(placeholder: String = "***", predicate: (String) -> Boolean)
135
```
136
137
**Parameters:**
138
- `placeholder`: String to replace sensitive header values (default: "***")
139
- `predicate`: Function that returns `true` if the header should be sanitized
140
141
**Usage Examples:**
142
143
```kotlin
144
Logging {
145
// Sanitize Authorization header
146
sanitizeHeader { header -> header == HttpHeaders.Authorization }
147
148
// Sanitize multiple headers with custom placeholder
149
sanitizeHeader("████") { header ->
150
header in listOf(HttpHeaders.Authorization, "X-API-Key", "X-Secret-Token")
151
}
152
153
// Sanitize headers matching patterns
154
sanitizeHeader { header -> header.startsWith("X-Private-") }
155
}
156
```
157
158
## Complete Configuration Example
159
160
```kotlin
161
val client = HttpClient(CIO) {
162
install(Logging) {
163
// Set format
164
format = LoggingFormat.OkHttp
165
166
// Choose logger
167
logger = MessageLengthLimitingLogger(
168
maxLength = 3000,
169
delegate = Logger.DEFAULT
170
)
171
172
// Set logging level
173
level = LogLevel.ALL
174
175
// Filter requests
176
filter { request ->
177
request.url.host == "api.production.com"
178
}
179
filter { request ->
180
request.method != HttpMethod.Options
181
}
182
183
// Sanitize sensitive headers
184
sanitizeHeader { it == HttpHeaders.Authorization }
185
sanitizeHeader("████") { it.startsWith("X-API-") }
186
sanitizeHeader("[REDACTED]") { it.contains("secret", ignoreCase = true) }
187
}
188
}
189
```