KSLog/src/jsMain/kotlin/ActualLogger.kt

31 lines
905 B
Kotlin
Raw Normal View History

2022-06-07 14:02:59 +00:00
package dev.inmo.kslog.common
2022-06-07 13:26:11 +00:00
2022-06-07 15:42:19 +00:00
fun KSLog(
messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String,
filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean
) = KSLog { l, t, m, e ->
if (!filter(l, t, m, e)) return@KSLog
val text = messageFormatter(l,t,m,e)
2022-06-07 15:52:42 +00:00
val args = e ?.let {
arrayOf(text, e)
} ?: arrayOf(text)
2022-06-07 13:26:11 +00:00
when (l) {
LogLevel.VERBOSE,
2022-06-07 15:52:42 +00:00
LogLevel.INFO -> console.info(*args)
LogLevel.WARNING -> console.warn(*args)
2022-06-07 13:26:11 +00:00
LogLevel.ERROR,
2022-06-07 15:52:42 +00:00
LogLevel.ASSERT -> console.error(*args)
LogLevel.DEBUG -> console.log(*args)
2022-06-07 13:26:11 +00:00
}
}
2022-06-07 15:42:19 +00:00
actual fun KSLog(
2022-06-07 13:26:11 +00:00
defaultTag: String,
2022-06-07 15:42:19 +00:00
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean
): KSLog {
return KSLog(
{ l, t, m, _ -> "[$l] ${t ?: defaultTag} - $m" },
{ l, t, m, e -> filter(l, t ?: defaultTag, m, e) }
2022-06-07 13:26:11 +00:00
)
2022-06-07 14:02:59 +00:00
}