0
# Logger Implementations
1
2
Platform-specific logger implementations providing integration with system logging frameworks including SLF4J, Android Logcat, and console output.
3
4
## Capabilities
5
6
### Logger Interface
7
8
Base interface for all logging implementations.
9
10
```kotlin { .api }
11
/**
12
* Base interface for HTTP client loggers
13
*/
14
interface Logger {
15
/**
16
* Log a message to the configured output
17
* @param message The message to log
18
*/
19
fun log(message: String)
20
21
companion object
22
}
23
```
24
25
### Default Platform Loggers
26
27
Platform-specific default logger implementations.
28
29
```kotlin { .api }
30
/**
31
* Platform-specific default logger (expect/actual implementation)
32
* - JVM: SLF4J-based logger using HttpClient class logger
33
* - LinuxX64/POSIX: Simple console logger
34
* - JS/WASM: Simple console logger
35
*/
36
val Logger.Companion.DEFAULT: Logger
37
```
38
39
**Platform-Specific Behavior:**
40
41
```kotlin
42
// JVM Implementation (uses SLF4J)
43
val Logger.Companion.DEFAULT: Logger // -> SLF4J logger for HttpClient class
44
45
// LinuxX64/POSIX Implementation (target platform)
46
val Logger.Companion.DEFAULT: Logger // -> Logger.SIMPLE (console output)
47
48
// JS/WASM Implementation
49
val Logger.Companion.DEFAULT: Logger // -> Logger.SIMPLE (console output)
50
```
51
52
### Built-in Logger Implementations
53
54
Common logger implementations available on all platforms.
55
56
```kotlin { .api }
57
/**
58
* Simple logger using println for output
59
* Prefixes messages with "HttpClient: "
60
*/
61
val Logger.Companion.SIMPLE: Logger
62
63
/**
64
* No-op logger that discards all messages
65
* Useful for testing or disabling logging
66
*/
67
val Logger.Companion.EMPTY: Logger
68
```
69
70
**Usage Examples:**
71
72
```kotlin
73
import io.ktor.client.*
74
import io.ktor.client.plugins.logging.*
75
76
// Use platform default logger
77
val client = HttpClient {
78
install(Logging) {
79
logger = Logger.DEFAULT
80
}
81
}
82
83
// Use simple console logger
84
val client = HttpClient {
85
install(Logging) {
86
logger = Logger.SIMPLE
87
}
88
}
89
90
// Disable logging output
91
val client = HttpClient {
92
install(Logging) {
93
logger = Logger.EMPTY
94
}
95
}
96
```
97
98
### JVM-Specific Loggers
99
100
Additional logger implementations available only on JVM platform.
101
102
```kotlin { .api }
103
/**
104
* Android-optimized logger with Logcat support
105
* - Uses Android Log.i() when available
106
* - Falls back to SLF4J when not on Android
107
* - Automatically breaks long messages for Android's 4068 character limit
108
* Available only on JVM platform
109
*/
110
val Logger.Companion.ANDROID: Logger
111
112
/**
113
* Logger that breaks up long messages for compatibility
114
* Useful for platforms with message length restrictions
115
* Available only on JVM platform
116
*
117
* @param maxLength Maximum message length (default: 4000)
118
* @param minLength Minimum length before attempting line break (default: 3000)
119
* @param delegate Underlying logger to use for output (default: Logger.DEFAULT)
120
*/
121
class MessageLengthLimitingLogger(
122
private val maxLength: Int = 4000,
123
private val minLength: Int = 3000,
124
private val delegate: Logger = Logger.DEFAULT
125
) : Logger
126
```
127
128
**JVM Usage Examples:**
129
130
```kotlin
131
// Android-optimized logging (JVM only)
132
val client = HttpClient {
133
install(Logging) {
134
logger = Logger.ANDROID
135
}
136
}
137
138
// Custom message length limiting (JVM only)
139
val client = HttpClient {
140
install(Logging) {
141
logger = MessageLengthLimitingLogger(
142
maxLength = 2000,
143
minLength = 1500,
144
delegate = Logger.SIMPLE
145
)
146
}
147
}
148
```
149
150
### Custom Logger Implementation
151
152
Implement the Logger interface for custom logging behavior.
153
154
```kotlin { .api }
155
/**
156
* Custom logger implementation example
157
*/
158
class CustomLogger(private val prefix: String) : Logger {
159
override fun log(message: String) {
160
// Custom logging logic
161
println("[$prefix] $message")
162
}
163
}
164
```
165
166
**Custom Logger Usage:**
167
168
```kotlin
169
// File-based logger example
170
class FileLogger(private val filePath: String) : Logger {
171
override fun log(message: String) {
172
File(filePath).appendText("${System.currentTimeMillis()}: $message\n")
173
}
174
}
175
176
// Structured logger example
177
class JsonLogger : Logger {
178
override fun log(message: String) {
179
val logEntry = mapOf(
180
"timestamp" to System.currentTimeMillis(),
181
"level" to "INFO",
182
"source" to "HttpClient",
183
"message" to message
184
)
185
println(Json.encodeToString(logEntry))
186
}
187
}
188
189
// Usage
190
val client = HttpClient {
191
install(Logging) {
192
logger = FileLogger("/var/log/http-client.log")
193
// or
194
logger = JsonLogger()
195
}
196
}
197
```
198
199
## Platform-Specific Details
200
201
### LinuxX64/POSIX Platform (Target)
202
203
On LinuxX64 and other POSIX platforms:
204
- `Logger.DEFAULT` returns `Logger.SIMPLE`
205
- Uses standard console output via `println()`
206
- No special Android or JVM-specific features
207
- Simple and lightweight implementation
208
209
### JVM Platform
210
211
On JVM platform:
212
- `Logger.DEFAULT` uses SLF4J integration
213
- Additional `Logger.ANDROID` for Android compatibility
214
- `MessageLengthLimitingLogger` for message length handling
215
- Full SLF4J ecosystem integration
216
217
### JS/WASM Platform
218
219
On JavaScript and WebAssembly platforms:
220
- `Logger.DEFAULT` returns `Logger.SIMPLE`
221
- Uses console output appropriate for the platform
222
- Lightweight implementation for web environments