-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
KLoggerFactory.kt
42 lines (38 loc) · 1.54 KB
/
KLoggerFactory.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package io.github.oshai.kotlinlogging.internal
import io.github.oshai.kotlinlogging.DarwinKLogger
import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration
import kotlin.native.concurrent.AtomicReference
import platform.darwin.OS_LOG_DEFAULT
import platform.darwin.os_log_create
/** factory methods to obtain a [KLogger] */
internal actual object KLoggerFactory {
private val constantLogger: AtomicReference<KLogger?> = AtomicReference(null)
private val constantOsDefaultLogger: KLogger = DarwinKLogger("", OS_LOG_DEFAULT)
/** get logger by explicit name */
internal actual fun logger(name: String): KLogger {
val subsystemConfigured = KotlinLoggingConfiguration.subsystem.value
val categoryConfigured = KotlinLoggingConfiguration.category.value
return when {
subsystemConfigured != null && categoryConfigured != null -> {
constantLogger.value
?: DarwinKLogger(name, os_log_create(subsystemConfigured, categoryConfigured)).also {
constantLogger.value = it
}
}
subsystemConfigured != null || categoryConfigured != null -> {
DarwinKLogger(name, os_log_create(subsystemConfigured ?: name, categoryConfigured ?: name))
}
name.isBlank() -> constantOsDefaultLogger
name.contains(".") -> {
DarwinKLogger(
name,
os_log_create(name.substringBeforeLast("."), name.substringAfterLast("."))
)
}
else -> {
DarwinKLogger(name, os_log_create(name, ""))
}
}
}
}