0
# Plugin Configuration
1
2
Comprehensive configuration options for the Ktor Client Logging plugin, including installation, level settings, filtering, and header sanitization.
3
4
## Capabilities
5
6
### Plugin Installation
7
8
Install the Logging plugin in HttpClient with optional configuration block.
9
10
```kotlin { .api }
11
/**
12
* Core logging plugin instance for HttpClient
13
*/
14
val Logging: ClientPlugin<LoggingConfig>
15
16
/**
17
* Extension function to install and configure the Logging plugin
18
* @param block Configuration block for LoggingConfig settings
19
*/
20
fun HttpClientConfig<*>.Logging(block: LoggingConfig.() -> Unit = {})
21
```
22
23
**Usage Examples:**
24
25
```kotlin
26
import io.ktor.client.*
27
import io.ktor.client.plugins.logging.*
28
29
// Basic installation with defaults
30
val client = HttpClient {
31
install(Logging)
32
}
33
34
// Installation with configuration
35
val client = HttpClient {
36
install(Logging) {
37
level = LogLevel.HEADERS
38
logger = Logger.DEFAULT
39
format = LoggingFormat.Default
40
}
41
}
42
```
43
44
### Configuration Class
45
46
Main configuration class providing all logging options and settings.
47
48
```kotlin { .api }
49
/**
50
* Configuration class for the Logging plugin
51
*/
52
@KtorDsl
53
class LoggingConfig {
54
/** General format for logging requests and responses */
55
var format: LoggingFormat
56
57
/** Logger instance to use for output */
58
var logger: Logger
59
60
/** Logging level specifying what information to log */
61
var level: LogLevel
62
63
/** Add filter to log only calls matching predicate */
64
fun filter(predicate: (HttpRequestBuilder) -> Boolean)
65
66
/** Sanitize sensitive headers to avoid values appearing in logs */
67
fun sanitizeHeader(placeholder: String = "***", predicate: (String) -> Boolean)
68
}
69
```
70
71
### Request Filtering
72
73
Filter which HTTP requests get logged based on custom criteria.
74
75
```kotlin { .api }
76
/**
77
* Add filter to log only calls matching predicate
78
* @param predicate Function that returns true for requests that should be logged
79
*/
80
fun filter(predicate: (HttpRequestBuilder) -> Boolean)
81
```
82
83
**Usage Examples:**
84
85
```kotlin
86
install(Logging) {
87
// Log only requests to specific hosts
88
filter { request ->
89
request.url.host.contains("api.example.com")
90
}
91
92
// Log only GET requests
93
filter { request ->
94
request.method == HttpMethod.Get
95
}
96
97
// Log requests with specific headers
98
filter { request ->
99
request.headers.contains("X-Debug")
100
}
101
102
// Multiple filters (ALL must match)
103
filter { request -> request.url.host.contains("api") }
104
filter { request -> !request.url.pathSegments.contains("health") }
105
}
106
```
107
108
### Header Sanitization
109
110
Sanitize sensitive header values to prevent them from appearing in logs.
111
112
```kotlin { .api }
113
/**
114
* Sanitize sensitive headers to avoid their values appearing in logs
115
* @param placeholder Replacement text for sanitized values (default: "***")
116
* @param predicate Function that returns true for headers that should be sanitized
117
*/
118
fun sanitizeHeader(placeholder: String = "***", predicate: (String) -> Boolean)
119
```
120
121
**Usage Examples:**
122
123
```kotlin
124
install(Logging) {
125
// Sanitize Authorization header
126
sanitizeHeader { headerName ->
127
headerName == "Authorization"
128
}
129
130
// Sanitize multiple sensitive headers with custom placeholder
131
sanitizeHeader("███") { headerName ->
132
headerName in listOf("Authorization", "Cookie", "X-API-Key")
133
}
134
135
// Sanitize headers matching pattern
136
sanitizeHeader { headerName ->
137
headerName.lowercase().contains("secret") ||
138
headerName.lowercase().contains("token")
139
}
140
}
141
```
142
143
## Configuration Properties
144
145
### Format Property
146
147
```kotlin { .api }
148
/**
149
* General format for logging requests and responses
150
* Default: LoggingFormat.Default
151
*/
152
var format: LoggingFormat
153
```
154
155
### Logger Property
156
157
```kotlin { .api }
158
/**
159
* Logger instance to use for output
160
* Default: Logger.DEFAULT (platform-specific)
161
*/
162
var logger: Logger
163
```
164
165
### Level Property
166
167
```kotlin { .api }
168
/**
169
* Logging level specifying what information to log
170
* Default: LogLevel.HEADERS
171
*/
172
var level: LogLevel
173
```