0
# Output Formatting
1
2
Multiple output format options for HTTP request/response logging, including Ktor's default format and OkHttp-compatible formatting for tool integration.
3
4
## Capabilities
5
6
### LoggingFormat Enum
7
8
Defines the output formatting style for logged HTTP requests and responses.
9
10
```kotlin { .api }
11
/**
12
* Output formatting options for HTTP logging
13
*/
14
enum class LoggingFormat {
15
/** Standard Ktor logging format with detailed request/response structure */
16
Default,
17
18
/**
19
* OkHttp-compatible logging format for tool integration
20
* Writes only application-level logs because low-level HTTP communication
21
* is hidden within engine implementations
22
*/
23
OkHttp
24
}
25
```
26
27
**Usage Examples:**
28
29
```kotlin
30
import io.ktor.client.*
31
import io.ktor.client.plugins.logging.*
32
33
// Use default Ktor format
34
val client = HttpClient {
35
install(Logging) {
36
format = LoggingFormat.Default // Default value
37
level = LogLevel.ALL
38
}
39
}
40
41
// Use OkHttp-compatible format
42
val client = HttpClient {
43
install(Logging) {
44
format = LoggingFormat.OkHttp
45
level = LogLevel.ALL
46
}
47
}
48
```
49
50
## Format Details
51
52
### Default Format
53
54
The standard Ktor logging format provides structured, detailed output with clear separation between request and response sections.
55
56
**Request Format Example:**
57
```
58
REQUEST: https://api.example.com/users
59
METHOD: POST
60
COMMON HEADERS
61
-> Content-Type: application/json
62
-> Authorization: ***
63
CONTENT HEADERS
64
-> Content-Length: 156
65
BODY Content-Type: application/json
66
BODY START
67
{"name": "John Doe", "email": "john@example.com"}
68
BODY END
69
```
70
71
**Response Format Example:**
72
```
73
RESPONSE: 201 Created
74
METHOD: POST
75
FROM: https://api.example.com/users
76
COMMON HEADERS
77
-> Content-Type: application/json
78
-> Location: /users/123
79
BODY Content-Type: application/json
80
BODY START
81
{"id": 123, "name": "John Doe", "email": "john@example.com"}
82
BODY END
83
```
84
85
**Default Format Features:**
86
- Clear section headers (REQUEST/RESPONSE, METHOD, HEADERS, BODY)
87
- Structured header presentation with `->` prefix
88
- Separate COMMON HEADERS and CONTENT HEADERS sections
89
- Binary content detection and omission
90
- Content-Type and body length information
91
- Header sanitization with configurable placeholders
92
93
### OkHttp Format
94
95
OkHttp-compatible format mimics the popular OkHttp logging interceptor output for consistency with existing tools and workflows.
96
97
**Request Format Example:**
98
```
99
--> POST https://api.example.com/users
100
Content-Type: application/json
101
Authorization: ██
102
Content-Length: 156
103
104
{"name": "John Doe", "email": "john@example.com"}
105
--> END POST (156-byte body)
106
```
107
108
**Response Format Example:**
109
```
110
<-- 201 Created https://api.example.com/users (245ms)
111
Content-Type: application/json
112
Location: /users/123
113
114
{"id": 123, "name": "John Doe", "email": "john@example.com"}
115
<-- END HTTP (245ms, 89-byte body)
116
```
117
118
**OkHttp Format Features:**
119
- Single-line request/response start indicators (`-->` and `<--`)
120
- Inline timing information in response headers
121
- Compact header presentation (no prefixes)
122
- Body size information in end markers
123
- Timing details showing request duration
124
- Binary and encoded content handling
125
- Streaming response detection
126
127
## Format Comparison
128
129
| Feature | Default Format | OkHttp Format |
130
|---------|---------------|---------------|
131
| **Readability** | Highly structured, verbose | Compact, tool-friendly |
132
| **Tool Integration** | Ktor-specific | OkHttp-compatible |
133
| **Header Display** | Prefixed with `->` | Plain format |
134
| **Timing Info** | Separate timing logs | Inline with response |
135
| **Body Handling** | BODY START/END markers | Inline with size info |
136
| **Binary Detection** | Detailed detection info | Simple omission notice |
137
| **Use Cases** | Development, debugging | CI/CD, log analysis tools |
138
139
## Content Handling
140
141
Both formats provide intelligent content handling:
142
143
### Binary Content Detection
144
145
```kotlin
146
// Both formats detect and handle binary content
147
--> POST /upload (binary 2048-byte body omitted) // OkHttp format
148
BODY END (binary body omitted) // Default format
149
```
150
151
### Content Encoding Detection
152
153
```kotlin
154
// Compressed content handling
155
--> POST /data (encoded 1024-byte body omitted) // OkHttp format
156
BODY END (encoded body omitted) // Default format
157
```
158
159
### Streaming Response Handling
160
161
```kotlin
162
// Streaming content detection
163
<-- 200 OK /events (streaming) // OkHttp format
164
RESPONSE: 200 OK (streaming response) // Default format
165
```
166
167
## Configuration Examples
168
169
### Development Setup
170
171
```kotlin
172
// Verbose logging for development
173
val devClient = HttpClient {
174
install(Logging) {
175
format = LoggingFormat.Default
176
level = LogLevel.ALL
177
logger = Logger.SIMPLE
178
}
179
}
180
```
181
182
### CI/CD Integration
183
184
```kotlin
185
// Tool-friendly logging for automated systems
186
val ciClient = HttpClient {
187
install(Logging) {
188
format = LoggingFormat.OkHttp
189
level = LogLevel.HEADERS
190
logger = Logger.DEFAULT
191
}
192
}
193
```
194
195
### Production Monitoring
196
197
```kotlin
198
// Minimal logging for production
199
val prodClient = HttpClient {
200
install(Logging) {
201
format = LoggingFormat.OkHttp
202
level = LogLevel.INFO
203
logger = Logger.DEFAULT
204
205
// Filter sensitive endpoints
206
filter { request ->
207
!request.url.pathSegments.contains("auth")
208
}
209
}
210
}
211
```