switch case of default kslogger

This commit is contained in:
InsanusMokrassar 2022-06-07 21:52:42 +06:00
parent 7fec433344
commit afad845108
3 changed files with 10 additions and 7 deletions

View File

@ -4,5 +4,5 @@ val Any.logTag
get() = this::class.simpleName ?: error("Unable to retrieve log tag")
val Any.logger
get() = CallbackKSLog { l, t, m, e ->
KSLog.DEFAULT.performLog(l, t ?: logTag, m, e)
KSLog.default.performLog(l, t ?: logTag, m, e)
}

View File

@ -14,7 +14,7 @@ interface KSLog {
fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?)
companion object : KSLog {
private var defaultLogger: KSLog? = null
var DEFAULT: KSLog
var default: KSLog
get() {
return defaultLogger ?: KSLog("app").also {
defaultLogger = it
@ -23,7 +23,7 @@ interface KSLog {
set(value) {
defaultLogger = value
}
override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = DEFAULT.performLog(level, tag, message, throwable)
override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = default.performLog(level, tag, message, throwable)
}
}

View File

@ -6,13 +6,16 @@ fun KSLog(
) = KSLog { l, t, m, e ->
if (!filter(l, t, m, e)) return@KSLog
val text = messageFormatter(l,t,m,e)
val args = e ?.let {
arrayOf(text, e)
} ?: arrayOf(text)
when (l) {
LogLevel.VERBOSE,
LogLevel.INFO -> console.info(text, e)
LogLevel.WARNING -> console.warn(text, e)
LogLevel.INFO -> console.info(*args)
LogLevel.WARNING -> console.warn(*args)
LogLevel.ERROR,
LogLevel.ASSERT -> console.error(text, e)
LogLevel.DEBUG -> console.log(text, e)
LogLevel.ASSERT -> console.error(*args)
LogLevel.DEBUG -> console.log(*args)
}
}