From aad75a66b77970566c69e9342ec24da061d3583e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 7 Jun 2022 20:45:14 +0600 Subject: [PATCH 1/4] fixes --- build.gradle | 26 +------------------------- src/commonMain/kotlin/Extensions.kt | 9 ++++++++- src/commonMain/kotlin/Logger.kt | 6 +++--- 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/build.gradle b/build.gradle index b65051e..fd6c49d 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,7 @@ repositories { kotlin { jvm() - js (IR) { + js(IR) { browser() nodejs() } @@ -47,30 +47,6 @@ kotlin { implementation libs.kotlin } } - commonTest { - dependencies { - implementation libs.kotlin.test.common - implementation libs.kotlin.test.annotations.common - } - } - jvmTest { - dependencies { - implementation libs.kotlin.test.junit - } - } - jsTest { - dependencies { - implementation libs.kotlin.test.js - implementation libs.kotlin.test.junit - } - } - androidTest { - dependencies { - implementation libs.kotlin.test.junit - implementation libs.android.test.junit - implementation libs.android.test.espresso.core - } - } } } diff --git a/src/commonMain/kotlin/Extensions.kt b/src/commonMain/kotlin/Extensions.kt index 9949b6c..c0a3a5f 100644 --- a/src/commonMain/kotlin/Extensions.kt +++ b/src/commonMain/kotlin/Extensions.kt @@ -1,6 +1,8 @@ +@file:Suppress("NOTHING_TO_INLINE") + package dev.inmo.kslog.common -inline fun Logger.log(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(level, messageBuilder(), tag, e) +inline fun Logger.log(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = invoke(level, tag, messageBuilder(), e) inline fun Logger.log(level: LogLevel, message: String, e: Throwable? = null) = log(level, null, e) { message } inline fun Logger.log(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, e) { message } @@ -36,6 +38,11 @@ inline fun Logger.debug(tag: String, message: String, e: Throwable? = null) = lo // ----- Short names + +inline fun Logger.l(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(level, tag, e, messageBuilder) +inline fun Logger.l(level: LogLevel, message: String, e: Throwable? = null) = log(level, message, e) +inline fun Logger.l(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, message, e) + inline fun Logger.v(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = verbose(tag, e, messageBuilder) inline fun Logger.v(message: String, e: Throwable? = null) = verbose(message, e) inline fun Logger.v(tag: String, message: String, e: Throwable? = null) = verbose(tag, message, e) diff --git a/src/commonMain/kotlin/Logger.kt b/src/commonMain/kotlin/Logger.kt index e986e45..15dfd4a 100644 --- a/src/commonMain/kotlin/Logger.kt +++ b/src/commonMain/kotlin/Logger.kt @@ -11,10 +11,10 @@ enum class LogLevel { } interface Logger { - fun log (level: LogLevel, message: String, tag: String? = null, throwable: Throwable? = null) + operator fun invoke(level: LogLevel, tag: String?, message: String, throwable: Throwable?) companion object : Logger { var DEFAULT: Logger = Logger("app") - override fun log(level: LogLevel, message: String, tag: String?, throwable: Throwable?) = DEFAULT.log(level, message, tag, throwable) + override fun invoke(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = DEFAULT(level, tag, message, throwable) operator fun invoke (log: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit) = CallbackLogger(log) } @@ -23,7 +23,7 @@ interface Logger { class CallbackLogger( private val logger: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit ) : Logger { - override fun log(level: LogLevel, message: String, tag: String?, throwable: Throwable?) = logger(level, message, tag, throwable) + override fun invoke(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = logger(level, message, tag, throwable) } From 7fec4333442bda887715f8d781a4e5c1bed4f891 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 7 Jun 2022 21:42:19 +0600 Subject: [PATCH 2/4] final fixes before publish --- src/commonMain/kotlin/AutoLoggers.kt | 8 +++ src/commonMain/kotlin/Extensions.kt | 84 ++++++++++++++-------------- src/commonMain/kotlin/KSLog.kt | 50 +++++++++++++++++ src/commonMain/kotlin/Logger.kt | 40 ------------- src/jsMain/kotlin/ActualLogger.kt | 25 ++++----- src/jvmMain/kotlin/ActualLogger.kt | 62 +++++++++++++------- src/main/kotlin/ActualLogger.kt | 10 ++-- 7 files changed, 158 insertions(+), 121 deletions(-) create mode 100644 src/commonMain/kotlin/AutoLoggers.kt create mode 100644 src/commonMain/kotlin/KSLog.kt delete mode 100644 src/commonMain/kotlin/Logger.kt diff --git a/src/commonMain/kotlin/AutoLoggers.kt b/src/commonMain/kotlin/AutoLoggers.kt new file mode 100644 index 0000000..88d9c0e --- /dev/null +++ b/src/commonMain/kotlin/AutoLoggers.kt @@ -0,0 +1,8 @@ +package dev.inmo.kslog.common + +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) + } diff --git a/src/commonMain/kotlin/Extensions.kt b/src/commonMain/kotlin/Extensions.kt index c0a3a5f..abdf527 100644 --- a/src/commonMain/kotlin/Extensions.kt +++ b/src/commonMain/kotlin/Extensions.kt @@ -2,72 +2,72 @@ package dev.inmo.kslog.common -inline fun Logger.log(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = invoke(level, tag, messageBuilder(), e) -inline fun Logger.log(level: LogLevel, message: String, e: Throwable? = null) = log(level, null, e) { message } -inline fun Logger.log(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, e) { message } +inline fun KSLog.log(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = performLog(level, tag, messageBuilder(), e) +inline fun KSLog.log(level: LogLevel, message: String, e: Throwable? = null) = log(level, null, e) { message } +inline fun KSLog.log(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, e) { message } -inline fun Logger.verbose(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.VERBOSE, tag, e, messageBuilder) -inline fun Logger.verbose(message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, message, e) -inline fun Logger.verbose(tag: String, message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, tag, message, e) +inline fun KSLog.verbose(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.VERBOSE, tag, e, messageBuilder) +inline fun KSLog.verbose(message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, message, e) +inline fun KSLog.verbose(tag: String, message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, tag, message, e) -inline fun Logger.info(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.INFO, tag, e, messageBuilder) -inline fun Logger.info(message: String, e: Throwable? = null) = log(LogLevel.INFO, message, e) -inline fun Logger.info(tag: String, message: String, e: Throwable? = null) = log(LogLevel.INFO, tag, message, e) +inline fun KSLog.info(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.INFO, tag, e, messageBuilder) +inline fun KSLog.info(message: String, e: Throwable? = null) = log(LogLevel.INFO, message, e) +inline fun KSLog.info(tag: String, message: String, e: Throwable? = null) = log(LogLevel.INFO, tag, message, e) -inline fun Logger.warning(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.WARNING, tag, e, messageBuilder) -inline fun Logger.warning(message: String, e: Throwable? = null) = log(LogLevel.WARNING, message, e) -inline fun Logger.warning(tag: String, message: String, e: Throwable? = null) = log(LogLevel.WARNING, tag, message, e) +inline fun KSLog.warning(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.WARNING, tag, e, messageBuilder) +inline fun KSLog.warning(message: String, e: Throwable? = null) = log(LogLevel.WARNING, message, e) +inline fun KSLog.warning(tag: String, message: String, e: Throwable? = null) = log(LogLevel.WARNING, tag, message, e) -inline fun Logger.error(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ERROR, tag, e, messageBuilder) -inline fun Logger.error(message: String, e: Throwable? = null) = log(LogLevel.ERROR, message, e) -inline fun Logger.error(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ERROR, tag, message, e) +inline fun KSLog.error(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ERROR, tag, e, messageBuilder) +inline fun KSLog.error(message: String, e: Throwable? = null) = log(LogLevel.ERROR, message, e) +inline fun KSLog.error(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ERROR, tag, message, e) -inline fun Logger.assert(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ASSERT, tag, e, messageBuilder) -inline fun Logger.assert(message: String, e: Throwable? = null) = log(LogLevel.ASSERT, message, e) -inline fun Logger.assert(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ASSERT, tag, message, e) +inline fun KSLog.assert(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ASSERT, tag, e, messageBuilder) +inline fun KSLog.assert(message: String, e: Throwable? = null) = log(LogLevel.ASSERT, message, e) +inline fun KSLog.assert(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ASSERT, tag, message, e) -inline fun Logger.debug(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.DEBUG, tag, e, messageBuilder) -inline fun Logger.debug(message: String, e: Throwable? = null) = log(LogLevel.DEBUG, message, e) -inline fun Logger.debug(tag: String, message: String, e: Throwable? = null) = log(LogLevel.DEBUG, tag, message, e) +inline fun KSLog.debug(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.DEBUG, tag, e, messageBuilder) +inline fun KSLog.debug(message: String, e: Throwable? = null) = log(LogLevel.DEBUG, message, e) +inline fun KSLog.debug(tag: String, message: String, e: Throwable? = null) = log(LogLevel.DEBUG, tag, message, e) // ----- Short names -inline fun Logger.l(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(level, tag, e, messageBuilder) -inline fun Logger.l(level: LogLevel, message: String, e: Throwable? = null) = log(level, message, e) -inline fun Logger.l(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, message, e) +inline fun KSLog.l(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(level, tag, e, messageBuilder) +inline fun KSLog.l(level: LogLevel, message: String, e: Throwable? = null) = log(level, message, e) +inline fun KSLog.l(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, message, e) -inline fun Logger.v(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = verbose(tag, e, messageBuilder) -inline fun Logger.v(message: String, e: Throwable? = null) = verbose(message, e) -inline fun Logger.v(tag: String, message: String, e: Throwable? = null) = verbose(tag, message, e) +inline fun KSLog.v(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = verbose(tag, e, messageBuilder) +inline fun KSLog.v(message: String, e: Throwable? = null) = verbose(message, e) +inline fun KSLog.v(tag: String, message: String, e: Throwable? = null) = verbose(tag, message, e) -inline fun Logger.i(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = info(tag, e, messageBuilder) -inline fun Logger.i(message: String, e: Throwable? = null) = info(message, e) -inline fun Logger.i(tag: String, message: String, e: Throwable? = null) = info(tag, message, e) +inline fun KSLog.i(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = info(tag, e, messageBuilder) +inline fun KSLog.i(message: String, e: Throwable? = null) = info(message, e) +inline fun KSLog.i(tag: String, message: String, e: Throwable? = null) = info(tag, message, e) -inline fun Logger.w(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = warning(tag, e, messageBuilder) -inline fun Logger.w(message: String, e: Throwable? = null) = warning(message, e) -inline fun Logger.w(tag: String, message: String, e: Throwable? = null) = warning(tag, message, e) +inline fun KSLog.w(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = warning(tag, e, messageBuilder) +inline fun KSLog.w(message: String, e: Throwable? = null) = warning(message, e) +inline fun KSLog.w(tag: String, message: String, e: Throwable? = null) = warning(tag, message, e) -inline fun Logger.e(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = error(tag, e, messageBuilder) -inline fun Logger.e(message: String, e: Throwable? = null) = error(message, e) -inline fun Logger.e(tag: String, message: String, e: Throwable? = null) = error(tag, message, e) +inline fun KSLog.e(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = error(tag, e, messageBuilder) +inline fun KSLog.e(message: String, e: Throwable? = null) = error(message, e) +inline fun KSLog.e(tag: String, message: String, e: Throwable? = null) = error(tag, message, e) -inline fun Logger.wtf(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = assert(tag, e, messageBuilder) -inline fun Logger.wtf(message: String, e: Throwable? = null) = assert(message, e) -inline fun Logger.wtf(tag: String, message: String, e: Throwable? = null) = assert(tag, message, e) +inline fun KSLog.wtf(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = assert(tag, e, messageBuilder) +inline fun KSLog.wtf(message: String, e: Throwable? = null) = assert(message, e) +inline fun KSLog.wtf(tag: String, message: String, e: Throwable? = null) = assert(tag, message, e) -inline fun Logger.d(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = debug(tag, e, messageBuilder) -inline fun Logger.d(message: String, e: Throwable? = null) = debug(message, e) -inline fun Logger.d(tag: String, message: String, e: Throwable? = null) = debug(tag, message, e) +inline fun KSLog.d(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = debug(tag, e, messageBuilder) +inline fun KSLog.d(message: String, e: Throwable? = null) = debug(message, e) +inline fun KSLog.d(tag: String, message: String, e: Throwable? = null) = debug(tag, message, e) diff --git a/src/commonMain/kotlin/KSLog.kt b/src/commonMain/kotlin/KSLog.kt new file mode 100644 index 0000000..4a3741b --- /dev/null +++ b/src/commonMain/kotlin/KSLog.kt @@ -0,0 +1,50 @@ +package dev.inmo.kslog.common + + +enum class LogLevel { + VERBOSE, + INFO, + WARNING, + ERROR, + ASSERT, + DEBUG +} + +interface KSLog { + fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) + companion object : KSLog { + private var defaultLogger: KSLog? = null + var DEFAULT: KSLog + get() { + return defaultLogger ?: KSLog("app").also { + defaultLogger = it + } + } + set(value) { + defaultLogger = value + } + override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = DEFAULT.performLog(level, tag, message, throwable) + } +} + + +operator fun KSLog.invoke(performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit) = CallbackKSLog(performLogCallback) + +class CallbackKSLog( + private val performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit +) : KSLog { + override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = performLogCallback(level, tag, message, throwable) +} + + +expect fun KSLog( + defaultTag: String, + filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean +) : KSLog + +fun KSLog( + defaultTag: String, + levels: Set = LogLevel.values().toSet() +): KSLog = KSLog (defaultTag) { l, _, _, _ -> + l in levels +} diff --git a/src/commonMain/kotlin/Logger.kt b/src/commonMain/kotlin/Logger.kt deleted file mode 100644 index 15dfd4a..0000000 --- a/src/commonMain/kotlin/Logger.kt +++ /dev/null @@ -1,40 +0,0 @@ -package dev.inmo.kslog.common - - -enum class LogLevel { - VERBOSE, - INFO, - WARNING, - ERROR, - ASSERT, - DEBUG -} - -interface Logger { - operator fun invoke(level: LogLevel, tag: String?, message: String, throwable: Throwable?) - companion object : Logger { - var DEFAULT: Logger = Logger("app") - override fun invoke(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = DEFAULT(level, tag, message, throwable) - - operator fun invoke (log: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit) = CallbackLogger(log) - } -} - -class CallbackLogger( - private val logger: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit -) : Logger { - override fun invoke(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = logger(level, message, tag, throwable) -} - - -expect fun Logger( - defaultTag: String, - filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean -) : Logger - -fun Logger( - defaultTag: String, - levels: Set = LogLevel.values().toSet() -): Logger = Logger (defaultTag) { l, _, _, _ -> - l in levels -} diff --git a/src/jsMain/kotlin/ActualLogger.kt b/src/jsMain/kotlin/ActualLogger.kt index 358e16d..e80dd52 100644 --- a/src/jsMain/kotlin/ActualLogger.kt +++ b/src/jsMain/kotlin/ActualLogger.kt @@ -1,11 +1,11 @@ package dev.inmo.kslog.common -fun Logger( - messageFormatter: (l: LogLevel, m: String, t: String?, Throwable?) -> String, - filter: (l: LogLevel, m: String, t: String?, Throwable?) -> Boolean -) = Logger { l, m, t, e -> - if (!filter(l, m, t, e)) return@Logger - val text = messageFormatter(l,m,t,e) +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) when (l) { LogLevel.VERBOSE, LogLevel.INFO -> console.info(text, e) @@ -14,15 +14,14 @@ fun Logger( LogLevel.ASSERT -> console.error(text, e) LogLevel.DEBUG -> console.log(text, e) } - } -actual fun Logger( +actual fun KSLog( defaultTag: String, - filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean -): Logger { - return Logger( - { l, m, t, e -> "[$l] ${t ?: defaultTag} - $m" }, - { l, m, t, e -> filter(l, m, t ?: defaultTag, e) } + 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) } ) } diff --git a/src/jvmMain/kotlin/ActualLogger.kt b/src/jvmMain/kotlin/ActualLogger.kt index e404cd3..1f3e556 100644 --- a/src/jvmMain/kotlin/ActualLogger.kt +++ b/src/jvmMain/kotlin/ActualLogger.kt @@ -2,25 +2,45 @@ package dev.inmo.kslog.common import java.util.logging.Level -actual fun Logger( - defaultTag: String, - filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean -): Logger { - val logger = java.util.logging.Logger.getLogger(defaultTag) - return Logger { l, m, t, e -> - if (!filter(l, m, t ?: defaultTag, e)) return@Logger - val currentLogger = t ?.let { java.util.logging.Logger.getLogger(t) } ?: logger - currentLogger.log( - when(l) { - LogLevel.VERBOSE -> Level.FINEST - LogLevel.INFO -> Level.INFO - LogLevel.WARNING -> Level.WARNING - LogLevel.ERROR -> Level.SEVERE - LogLevel.ASSERT -> Level.OFF - LogLevel.DEBUG -> Level.ALL - }, - m, - e - ) - } +fun KSLog( + logger: java.util.logging.Logger, + filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean, + messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String +) = KSLog { l, t, m, e -> + if (!filter(l, t, m, e)) return@KSLog + val text = messageFormatter(l,t,m,e) + logger.log( + when(l) { + LogLevel.VERBOSE -> Level.FINEST + LogLevel.INFO -> Level.INFO + LogLevel.WARNING -> Level.WARNING + LogLevel.ERROR -> Level.SEVERE + LogLevel.ASSERT -> Level.OFF + LogLevel.DEBUG -> Level.ALL + }, + text, + e + ) +} + +fun KSLog( + defaultTag: String, + logger: java.util.logging.Logger, + filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean = { _, _, _, _ -> true } +): KSLog { + return KSLog( + logger, + { l, t, m, e -> filter(l, t ?: defaultTag, m, e) } + ) { _, t, m, _ -> "${t ?: defaultTag} - $m" } +} + +actual fun KSLog( + defaultTag: String, + filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean +): KSLog { + return KSLog( + defaultTag, + logger = java.util.logging.Logger.getAnonymousLogger(), + filter = { l, t, m, e -> filter(l, t, m, e) } + ) } diff --git a/src/main/kotlin/ActualLogger.kt b/src/main/kotlin/ActualLogger.kt index 67d1e0d..e09626c 100644 --- a/src/main/kotlin/ActualLogger.kt +++ b/src/main/kotlin/ActualLogger.kt @@ -2,13 +2,13 @@ package dev.inmo.kslog.common import android.util.Log -actual fun Logger( +actual fun KSLog( defaultTag: String, - filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean -): Logger { - return Logger { l, m, t, e -> + filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean +): KSLog { + return KSLog { l, t, m, e -> val tag = t ?: defaultTag - if (!filter(l, m, t ?: defaultTag, e)) return@Logger + if (!filter(l, t ?: defaultTag, m, e)) return@KSLog when(l) { LogLevel.VERBOSE -> Log.v(tag, m, e) LogLevel.INFO -> Log.i(tag, m, e) From afad845108261f874562eff661c9cbe5e3aa6abf Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 7 Jun 2022 21:52:42 +0600 Subject: [PATCH 3/4] switch case of default kslogger --- src/commonMain/kotlin/AutoLoggers.kt | 2 +- src/commonMain/kotlin/KSLog.kt | 4 ++-- src/jsMain/kotlin/ActualLogger.kt | 11 +++++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/commonMain/kotlin/AutoLoggers.kt b/src/commonMain/kotlin/AutoLoggers.kt index 88d9c0e..042013e 100644 --- a/src/commonMain/kotlin/AutoLoggers.kt +++ b/src/commonMain/kotlin/AutoLoggers.kt @@ -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) } diff --git a/src/commonMain/kotlin/KSLog.kt b/src/commonMain/kotlin/KSLog.kt index 4a3741b..a8c0487 100644 --- a/src/commonMain/kotlin/KSLog.kt +++ b/src/commonMain/kotlin/KSLog.kt @@ -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) } } diff --git a/src/jsMain/kotlin/ActualLogger.kt b/src/jsMain/kotlin/ActualLogger.kt index e80dd52..1c74fcb 100644 --- a/src/jsMain/kotlin/ActualLogger.kt +++ b/src/jsMain/kotlin/ActualLogger.kt @@ -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) } } From c48ceefd8a2423a0f0f23adfb9f8bebe5a65a5ab Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 7 Jun 2022 22:00:32 +0600 Subject: [PATCH 4/4] add new KSLog factory --- src/commonMain/kotlin/KSLog.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/KSLog.kt b/src/commonMain/kotlin/KSLog.kt index a8c0487..495b152 100644 --- a/src/commonMain/kotlin/KSLog.kt +++ b/src/commonMain/kotlin/KSLog.kt @@ -44,7 +44,14 @@ expect fun KSLog( fun KSLog( defaultTag: String, - levels: Set = LogLevel.values().toSet() + levels: Set ): KSLog = KSLog (defaultTag) { l, _, _, _ -> l in levels } + +fun KSLog( + defaultTag: String, + minLoggingLevel: LogLevel = LogLevel.VERBOSE +): KSLog = KSLog (defaultTag) { l, _, _, _ -> + minLoggingLevel.ordinal <= l.ordinal +}