HTTP client logging plugin for Ktor client framework with configurable logging formats, levels, and platform-specific logger integrations
—
The Ktor Client Logging plugin provides fine-grained control over what information gets logged and how it's formatted.
import io.ktor.client.*
import io.ktor.client.plugins.logging.*enum class LogLevel(
val info: Boolean,
val headers: Boolean,
val body: Boolean
) {
ALL(true, true, true),
HEADERS(true, true, false),
BODY(true, false, true),
INFO(true, false, false),
NONE(false, false, false)
}The LogLevel enum controls the verbosity of logging output with boolean flags indicating what information categories are included.
Each log level has three boolean properties that determine what gets logged:
info: Boolean - Basic request/response information (URL, method, status, timing)headers: Boolean - HTTP headers for requests and responsesbody: Boolean - Request and response body contentLogLevel.ALL(true, true, true)Logs everything: basic info, headers, and body content. Most verbose option.
Example Output:
REQUEST: https://api.example.com/users
METHOD: POST
COMMON HEADERS
-> Content-Type: application/json
-> Authorization: ***
CONTENT HEADERS
-> Content-Length: 156
BODY Content-Type: application/json
BODY START
{"name":"John Doe","email":"john@example.com"}
BODY END
RESPONSE: 201 Created
METHOD: POST
FROM: https://api.example.com/users
COMMON HEADERS
-> Content-Type: application/json
-> Server: nginx/1.18.0
BODY Content-Type: application/json
BODY START
{"id":123,"name":"John Doe","email":"john@example.com","created":"2023-01-01T00:00:00Z"}
BODY ENDLogLevel.HEADERS(true, true, false)Logs basic info and headers but not body content. Good balance for debugging without exposing sensitive payloads.
Example Output:
REQUEST: https://api.example.com/users
METHOD: POST
COMMON HEADERS
-> Content-Type: application/json
-> Authorization: ***
CONTENT HEADERS
-> Content-Length: 156
RESPONSE: 201 Created
METHOD: POST
FROM: https://api.example.com/users
COMMON HEADERS
-> Content-Type: application/json
-> Server: nginx/1.18.0LogLevel.BODY(true, false, true)Logs basic info and body content but not headers. Useful when headers are not relevant but payload content is important.
Example Output:
REQUEST: https://api.example.com/users
METHOD: POST
BODY Content-Type: application/json
BODY START
{"name":"John Doe","email":"john@example.com"}
BODY END
RESPONSE: 201 Created
METHOD: POST
FROM: https://api.example.com/users
BODY Content-Type: application/json
BODY START
{"id":123,"name":"John Doe","email":"john@example.com"}
BODY ENDLogLevel.INFO(true, false, false)Logs only basic request/response information. Minimal logging for production environments.
Example Output:
REQUEST: https://api.example.com/users
METHOD: POST
RESPONSE: 201 Created
METHOD: POST
FROM: https://api.example.com/usersLogLevel.NONE(false, false, false)Disables all logging. No output is generated.
// Maximum verbosity
Logging {
level = LogLevel.ALL
}
// Production-friendly logging
Logging {
level = LogLevel.INFO
}
// Debug headers without exposing payloads
Logging {
level = LogLevel.HEADERS
}
// Disable logging entirely
Logging {
level = LogLevel.NONE
}enum class LoggingFormat {
Default,
OkHttp
}The LoggingFormat enum controls the overall structure and style of logged output.
LoggingFormat.DefaultStandard Ktor logging format with structured request/response sections and detailed information layout.
Characteristics:
Example:
REQUEST: https://api.example.com/users/123
METHOD: GET
COMMON HEADERS
-> Authorization: ***
-> User-Agent: Ktor-Client
RESPONSE: 200 OK
METHOD: GET
FROM: https://api.example.com/users/123
COMMON HEADERS
-> Content-Type: application/json
-> Content-Length: 87
BODY START
{"id":123,"name":"John Doe","email":"john@example.com"}
BODY ENDLoggingFormat.OkHttpOkHttp-compatible logging format that mimics the popular OkHttp logging interceptor output style. More concise than Default format.
Characteristics:
Example:
--> POST /users (156-byte body)
Content-Type: application/json
Authorization: ██
{"name":"John Doe","email":"john@example.com"}
--> END POST
<-- 201 Created /users (342ms, 87-byte body)
Content-Type: application/json
Server: nginx/1.18.0
{"id":123,"name":"John Doe","email":"john@example.com","created":"2023-01-01T00:00:00Z"}
<-- END HTTP (342ms, 87-byte body)--> for outgoing, <-- for incomingBoth formats automatically detect and handle binary content:
Text Content:
BODY START
{"message": "Hello, World!"}
BODY ENDBinary Content:
--> END POST (binary 1024-byte body omitted)Encoded Content:
<-- END HTTP (342ms, encoded 2048-byte body omitted)| Use Case | Recommended Format | Reason |
|---|---|---|
| Development & Debugging | Default | More detailed, structured output |
| Integration with OkHttp tools | OkHttp | Compatible with existing tooling |
| Performance monitoring | OkHttp | Includes timing information |
| Production logging | OkHttp | More compact output |
| Log analysis | Default | Easier to parse structured sections |
Logging {
// Detailed logging for development
level = LogLevel.ALL
format = LoggingFormat.Default
}
// vs
Logging {
// Production-friendly logging
level = LogLevel.INFO
format = LoggingFormat.OkHttp
}Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-logging-jvm