KSLog/src/jsMain/kotlin/ActualLogger.kt

28 lines
840 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 13:26:11 +00:00
when (l) {
LogLevel.VERBOSE,
LogLevel.INFO -> console.info(text, e)
LogLevel.WARNING -> console.warn(text, e)
LogLevel.ERROR,
LogLevel.ASSERT -> console.error(text, e)
LogLevel.DEBUG -> console.log(text, e)
}
}
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
}