0
# Logging Levels and Control
1
2
Control what information gets logged with granular level settings, from basic request info to complete request/response bodies.
3
4
## Capabilities
5
6
### LogLevel Enum
7
8
Defines what information should be logged for HTTP requests and responses.
9
10
```kotlin { .api }
11
/**
12
* Logging level enum controlling what information gets logged
13
* @param info Whether to log basic request/response information
14
* @param headers Whether to log request/response headers
15
* @param body Whether to log request/response body content
16
*/
17
enum class LogLevel(
18
val info: Boolean,
19
val headers: Boolean,
20
val body: Boolean
21
) {
22
/** Log everything: info, headers, and body content */
23
ALL(true, true, true),
24
25
/** Log info and headers but not body content */
26
HEADERS(true, true, false),
27
28
/** Log info and body but not headers */
29
BODY(true, false, true),
30
31
/** Log only basic request/response info */
32
INFO(true, false, false),
33
34
/** Disable all logging */
35
NONE(false, false, false)
36
}
37
```
38
39
**Usage Examples:**
40
41
```kotlin
42
import io.ktor.client.*
43
import io.ktor.client.plugins.logging.*
44
45
// Log everything including request/response bodies
46
val client = HttpClient {
47
install(Logging) {
48
level = LogLevel.ALL
49
}
50
}
51
52
// Log only headers, skip body content
53
val client = HttpClient {
54
install(Logging) {
55
level = LogLevel.HEADERS // Default level
56
}
57
}
58
59
// Log only basic request/response info
60
val client = HttpClient {
61
install(Logging) {
62
level = LogLevel.INFO
63
}
64
}
65
66
// Disable logging completely
67
val client = HttpClient {
68
install(Logging) {
69
level = LogLevel.NONE
70
}
71
}
72
```
73
74
### LogLevel Properties
75
76
Each LogLevel enum value provides boolean properties indicating what should be logged.
77
78
```kotlin { .api }
79
/**
80
* Whether to log basic request/response information (URL, method, status)
81
*/
82
val info: Boolean
83
84
/**
85
* Whether to log request/response headers
86
*/
87
val headers: Boolean
88
89
/**
90
* Whether to log request/response body content
91
*/
92
val body: Boolean
93
```
94
95
**Usage Examples:**
96
97
```kotlin
98
// Check what a log level includes
99
val level = LogLevel.HEADERS
100
println("Logs info: ${level.info}") // true
101
println("Logs headers: ${level.headers}") // true
102
println("Logs body: ${level.body}") // false
103
104
// Conditional logging based on level capabilities
105
if (level.headers) {
106
// Process header logging
107
}
108
109
if (level.body) {
110
// Process body logging
111
}
112
```
113
114
## Logging Level Details
115
116
### ALL Level
117
118
Logs complete request and response information including body content.
119
120
**What gets logged:**
121
- Request URL, method, and timing
122
- Request headers (with sanitization)
123
- Request body content (with binary detection)
124
- Response status, timing, and headers
125
- Response body content (with binary detection)
126
127
**Use cases:**
128
- Debugging complex API interactions
129
- Development and testing environments
130
- Troubleshooting request/response payload issues
131
132
### HEADERS Level (Default)
133
134
Logs request/response info and headers but skips body content.
135
136
**What gets logged:**
137
- Request URL, method, and timing
138
- Request headers (with sanitization)
139
- Response status, timing, and headers
140
- Body content is omitted
141
142
**Use cases:**
143
- Production environments where body logging is too verbose
144
- Monitoring API calls and response codes
145
- Header-based debugging (authentication, content-type, etc.)
146
147
### BODY Level
148
149
Logs request/response info and body content but skips headers.
150
151
**What gets logged:**
152
- Request URL, method, and timing
153
- Request body content (with binary detection)
154
- Response status, timing
155
- Response body content (with binary detection)
156
- Headers are omitted
157
158
**Use cases:**
159
- Payload-focused debugging
160
- Content transformation verification
161
- Scenarios where headers contain sensitive information
162
163
### INFO Level
164
165
Logs only basic request/response information.
166
167
**What gets logged:**
168
- Request URL and method
169
- Response status and timing
170
- Headers and body content are omitted
171
172
**Use cases:**
173
- High-level request monitoring
174
- Performance timing analysis
175
- Minimal logging overhead in production
176
177
### NONE Level
178
179
Disables all logging output.
180
181
**What gets logged:**
182
- Nothing (plugin effectively disabled)
183
184
**Use cases:**
185
- Production environments where logging is handled elsewhere
186
- Performance-critical scenarios
187
- Temporary logging disable during debugging