0
# Configuration & Logging
1
2
Application configuration system with structured logging support for debugging and monitoring dependency injection lifecycle. Provides reusable configuration blocks and comprehensive logging capabilities.
3
4
## Capabilities
5
6
### Koin Configuration
7
8
Reusable configuration blocks for sharing application setup across different contexts and environments.
9
10
```kotlin { .api }
11
/**
12
* Configuration container for KoinApplication setup
13
* @param config - Configuration block for KoinApplication
14
*/
15
class KoinConfiguration(val config: KoinApplication.() -> Unit) {
16
/**
17
* Invoke the configuration
18
* @return Configuration function
19
*/
20
operator fun invoke(): KoinApplication.() -> Unit
21
22
/**
23
* Get the application declaration
24
*/
25
val appDeclaration: KoinApplication.() -> Unit
26
}
27
28
/**
29
* Create a reusable Koin configuration
30
* @param declaration - Configuration block
31
* @return KoinConfiguration instance
32
*/
33
fun koinConfiguration(declaration: KoinAppDeclaration): KoinConfiguration
34
```
35
36
**Usage Examples:**
37
38
```kotlin
39
import org.koin.core.context.startKoin
40
import org.koin.dsl.koinConfiguration
41
42
// Create reusable configuration
43
val baseConfig = koinConfiguration {
44
logger(PrintLogger(Level.DEBUG))
45
allowOverride(false)
46
properties(mapOf(
47
"api.baseUrl" to "https://api.example.com",
48
"db.connectionPool" to "10"
49
))
50
}
51
52
val testConfig = koinConfiguration {
53
baseConfig()
54
properties(mapOf(
55
"api.baseUrl" to "https://test-api.example.com"
56
))
57
}
58
59
// Use in different contexts
60
fun startProduction() {
61
startKoin(baseConfig) {
62
modules(productionModules)
63
}
64
}
65
66
fun startTesting() {
67
startKoin(testConfig) {
68
modules(testModules)
69
}
70
}
71
```
72
73
### Configuration Inclusion
74
75
Combine and compose multiple configuration blocks for modular application setup.
76
77
```kotlin { .api }
78
/**
79
* Include configuration declarations in KoinApplication
80
* @param configurations - Variable number of configuration declarations
81
* @return KoinApplication for chaining
82
*/
83
fun KoinApplication.includes(vararg configurations: KoinAppDeclaration?): KoinApplication
84
85
/**
86
* Include KoinConfiguration instances in KoinApplication
87
* @param configurations - Variable number of KoinConfiguration instances
88
* @return KoinApplication for chaining
89
*/
90
fun KoinApplication.includes(vararg configurations: KoinConfiguration?): KoinApplication
91
```
92
93
**Usage Examples:**
94
95
```kotlin
96
import org.koin.core.context.startKoin
97
import org.koin.dsl.koinConfiguration
98
99
val loggingConfig = koinConfiguration {
100
printLogger(Level.INFO)
101
}
102
103
val propertiesConfig = koinConfiguration {
104
properties(mapOf("env" to "production"))
105
}
106
107
val securityConfig: KoinAppDeclaration = {
108
allowOverride(false)
109
}
110
111
// Compose configurations
112
startKoin {
113
includes(loggingConfig, propertiesConfig, securityConfig)
114
modules(appModules)
115
}
116
```
117
118
### Logging System
119
120
Structured logging framework for monitoring dependency injection lifecycle, debugging resolution issues, and tracking performance.
121
122
```kotlin { .api }
123
/**
124
* Abstract base class for Koin logging implementations
125
* @param level - Default logging level
126
*/
127
abstract class Logger(var level: Level = Level.INFO) {
128
/**
129
* Display message at specified level
130
* @param level - Log level for the message
131
* @param msg - Message to display
132
*/
133
abstract fun display(level: Level, msg: MESSAGE)
134
135
/**
136
* Log debug message
137
* @param msg - Debug message
138
*/
139
fun debug(msg: MESSAGE)
140
141
/**
142
* Log info message
143
* @param msg - Info message
144
*/
145
fun info(msg: MESSAGE)
146
147
/**
148
* Log warning message
149
* @param msg - Warning message
150
*/
151
fun warn(msg: MESSAGE)
152
153
/**
154
* Log error message
155
* @param msg - Error message
156
*/
157
fun error(msg: MESSAGE)
158
159
/**
160
* Check if logger is at specified level
161
* @param lvl - Level to check
162
* @return True if logger is at or above the level
163
*/
164
inline fun isAt(lvl: Level): Boolean
165
166
/**
167
* Log message at specified level
168
* @param lvl - Log level
169
* @param msg - Message string
170
*/
171
fun log(lvl: Level, msg: String)
172
173
/**
174
* Log message at specified level with lazy evaluation
175
* @param lvl - Log level
176
* @param msg - Message function (evaluated only if level is active)
177
*/
178
inline fun log(lvl: Level, msg: () -> String)
179
}
180
181
/**
182
* Logging levels for Koin framework
183
*/
184
enum class Level {
185
/** Detailed debugging information */
186
DEBUG,
187
/** General information messages */
188
INFO,
189
/** Warning messages for potential issues */
190
WARNING,
191
/** Error messages for failures */
192
ERROR,
193
/** No logging */
194
NONE
195
}
196
197
typealias MESSAGE = String
198
const val KOIN_TAG = "[Koin]"
199
```
200
201
**Usage Examples:**
202
203
```kotlin
204
import org.koin.core.logger.Logger
205
import org.koin.core.logger.Level
206
import org.koin.core.context.startKoin
207
208
// Custom logger implementation
209
class FileLogger(level: Level = Level.INFO) : Logger(level) {
210
override fun display(level: Level, msg: MESSAGE) {
211
val timestamp = System.currentTimeMillis()
212
val logEntry = "[$timestamp] [${level.name}] $KOIN_TAG $msg"
213
writeToFile(logEntry)
214
}
215
216
private fun writeToFile(entry: String) {
217
// File writing implementation
218
}
219
}
220
221
// Use custom logger
222
startKoin {
223
logger(FileLogger(Level.DEBUG))
224
modules(appModule)
225
}
226
227
// Built-in print logger with different levels
228
startKoin {
229
printLogger(Level.WARNING) // Only warnings and errors
230
modules(appModule)
231
}
232
233
// Conditional logging
234
class ConditionalLogger : Logger() {
235
override fun display(level: Level, msg: MESSAGE) {
236
if (isAt(level)) {
237
println("$KOIN_TAG [$level] $msg")
238
}
239
}
240
}
241
```
242
243
### Built-in Logger
244
245
Platform-specific logging implementation using standard output with configurable levels.
246
247
```kotlin { .api }
248
/**
249
* Built-in logger using standard output
250
* @param level - Logging level (default: INFO)
251
*/
252
class PrintLogger(level: Level = Level.INFO) : Logger(level)
253
254
/**
255
* Configure KoinApplication with print logger
256
* @param level - Logging level (default: INFO)
257
* @return KoinApplication for chaining
258
*/
259
fun KoinApplication.printLogger(level: Level = Level.INFO): KoinApplication
260
```
261
262
**Usage Examples:**
263
264
```kotlin
265
import org.koin.core.logger.Level
266
import org.koin.core.logger.PrintLogger
267
import org.koin.core.context.startKoin
268
269
// Direct logger instantiation
270
val logger = PrintLogger(Level.DEBUG)
271
startKoin {
272
logger(logger)
273
modules(appModule)
274
}
275
276
// Using convenience method
277
startKoin {
278
printLogger(Level.INFO)
279
modules(appModule)
280
}
281
282
// Different levels for different environments
283
fun configureLogging(isDevelopment: Boolean) {
284
val logLevel = if (isDevelopment) Level.DEBUG else Level.WARNING
285
286
startKoin {
287
printLogger(logLevel)
288
modules(appModule)
289
}
290
}
291
```
292
293
## Types
294
295
### Configuration Types
296
297
```kotlin { .api }
298
typealias KoinAppDeclaration = KoinApplication.() -> Unit
299
typealias MESSAGE = String
300
301
const val KOIN_TAG = "[Koin]"
302
```