KSLog/src/jsMain/kotlin/ActualLogger.kt

49 lines
1.5 KiB
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-08 12:23:11 +00:00
fun KSJSLog(
defaultTag: String,
messageFormatter: MessageFormatter = defaultMessageFormatter,
2022-06-07 15:42:19 +00:00
filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean
) = KSLog { l, t, m, e ->
if (!filter(l, t, m, e)) return@KSLog
2022-06-08 12:23:11 +00:00
val text = messageFormatter(l, t?:defaultTag, 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) {
2022-06-08 15:45:37 +00:00
LogLevel.DEBUG -> console.log(*args)
2022-06-07 13:26:11 +00:00
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)
2022-06-07 13:26:11 +00:00
}
}
2022-06-08 12:23:11 +00:00
fun KSJSLog(
2022-06-07 13:26:11 +00:00
defaultTag: String,
2022-06-08 12:23:11 +00:00
messageFormatter: MessageFormatter = defaultMessageFormatter,
levels: Iterable<LogLevel>
2022-06-07 15:42:19 +00:00
): KSLog {
2022-06-08 12:23:11 +00:00
val levels = levels.toSet()
return KSJSLog (defaultTag, messageFormatter) { l, _, _, _ ->
l in levels
}
}
fun KSJSLog(
defaultTag: String,
messageFormatter: MessageFormatter = defaultMessageFormatter,
firstLevel: LogLevel,
secondLevel: LogLevel,
vararg otherLevels: LogLevel
): KSLog = KSJSLog (defaultTag, messageFormatter, setOf(firstLevel, secondLevel, *otherLevels))
fun KSJSLog(
defaultTag: String,
messageFormatter: MessageFormatter = defaultMessageFormatter,
2022-06-08 15:45:37 +00:00
minLoggingLevel: LogLevel = LogLevel.values().first()
2022-06-08 12:23:11 +00:00
): KSLog = KSJSLog (defaultTag, messageFormatter) { l, _, _, _ ->
minLoggingLevel.ordinal <= l.ordinal
2022-06-07 14:02:59 +00:00
}