From 1d16902e696a6f397627f383a559dfb31b67ed1c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 30 Jul 2022 13:51:06 +0600 Subject: [PATCH 1/7] start 0.3.3 --- CHANGELOG.md | 2 ++ gradle.properties | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb65706..158aac4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +## 0.3.3 + ## 0.3.2 * Add duplication functions for all extensions which allow to send log without tag but with exception and message builder diff --git a/gradle.properties b/gradle.properties index 75af47d..ab835ff 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,5 +9,5 @@ android.enableJetifier=true # Project data group=dev.inmo -version=0.3.2 -android_code_version=9 +version=0.3.3 +android_code_version=10 From 9c8d8899b11933c9fa755f93eed0f6ace2b0946c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 30 Jul 2022 13:51:22 +0600 Subject: [PATCH 2/7] update gradle wrapper --- gradle/wrapper/gradle-wrapper.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index aa991fc..8049c68 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists From 7fa929fb7a5642654628e6ee24eba640633beabf Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 30 Jul 2022 14:03:11 +0600 Subject: [PATCH 3/7] change version to 0.4.0 --- CHANGELOG.md | 2 +- gradle.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 158aac4..78199fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## 0.3.3 +## 0.4.0 ## 0.3.2 diff --git a/gradle.properties b/gradle.properties index ab835ff..02a7873 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,5 +9,5 @@ android.enableJetifier=true # Project data group=dev.inmo -version=0.3.3 +version=0.4.0 android_code_version=10 From 437b1675e23b72f17da15620f46d7a3113c97214 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 30 Jul 2022 14:12:45 +0600 Subject: [PATCH 4/7] accept string messages -> accept any object as message --- CHANGELOG.md | 4 + src/commonMain/kotlin/CallbackKSLog.kt | 4 +- src/commonMain/kotlin/DefaultKSLog.kt | 8 +- src/commonMain/kotlin/Extensions.kt | 168 +++++++++++----------- src/commonMain/kotlin/KSLog.kt | 22 +-- src/commonMain/kotlin/KSLogTypealiases.kt | 2 +- src/commonMain/kotlin/TagLogger.kt | 2 +- src/jsMain/kotlin/ActualLogger.kt | 2 +- src/jvmMain/kotlin/ActualLogger.kt | 6 +- src/main/kotlin/ActualLogger.kt | 15 +- 10 files changed, 120 insertions(+), 113 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78199fa..fdac7ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.4.0 +**THIS UPDATE CONTAINS BREAKING CHANGES** + +* **BREAKING CHANGE** Any `KSLog` now accept any object. It was made to allow to work with structured logging + ## 0.3.2 * Add duplication functions for all extensions which allow to send log without tag but with exception and message builder diff --git a/src/commonMain/kotlin/CallbackKSLog.kt b/src/commonMain/kotlin/CallbackKSLog.kt index 13c496f..f801116 100644 --- a/src/commonMain/kotlin/CallbackKSLog.kt +++ b/src/commonMain/kotlin/CallbackKSLog.kt @@ -1,7 +1,7 @@ package dev.inmo.kslog.common class CallbackKSLog( - private val performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit + private val performLogCallback: (level: LogLevel, tag: String?, message: Any, throwable: Throwable?) -> Unit ) : KSLog { - override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = performLogCallback(level, tag, message, throwable) + override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) = performLogCallback(level, tag, message, throwable) } diff --git a/src/commonMain/kotlin/DefaultKSLog.kt b/src/commonMain/kotlin/DefaultKSLog.kt index c59fb53..094b83e 100644 --- a/src/commonMain/kotlin/DefaultKSLog.kt +++ b/src/commonMain/kotlin/DefaultKSLog.kt @@ -4,9 +4,9 @@ class DefaultKSLog( private val defaultTag: String, val filter: MessageFilter = { _, _, _ -> true }, private val messageFormatter: MessageFormatter = defaultMessageFormatter, - private val logging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = defaultLogging + private val logging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = defaultLogging ) : KSLog { - override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) { + override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) { val tag = tag ?: defaultTag if (filter(level, tag, throwable)) { @@ -15,7 +15,7 @@ class DefaultKSLog( } } - override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> String) { + override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> Any) { val tag = tag ?: defaultTag if (filter(level, tag, throwable)) { @@ -28,7 +28,7 @@ class DefaultKSLog( level: LogLevel, tag: String?, throwable: Throwable?, - messageBuilder: suspend () -> String + messageBuilder: suspend () -> Any ) { val tag = tag ?: defaultTag diff --git a/src/commonMain/kotlin/Extensions.kt b/src/commonMain/kotlin/Extensions.kt index 8591225..e39376b 100644 --- a/src/commonMain/kotlin/Extensions.kt +++ b/src/commonMain/kotlin/Extensions.kt @@ -2,114 +2,114 @@ package dev.inmo.kslog.common -inline fun KSLog.log(level: LogLevel, tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = performLog(level, tag, e, messageBuilder) -suspend inline fun KSLog.logS(level: LogLevel, tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = performLogS(level, tag, e, messageBuilder) -inline fun KSLog.log(level: LogLevel, e: Throwable? = null, noinline messageBuilder: () -> String) = performLog(level, null, e, messageBuilder) -suspend inline fun KSLog.logS(level: LogLevel, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = performLogS(level, null, e, messageBuilder) -inline fun KSLog.log(level: LogLevel, message: String, e: Throwable? = null) = performLog(level, message, e) -inline fun KSLog.log(level: LogLevel, tag: String, message: String, e: Throwable? = null) = performLog(level, tag, message, e) +inline fun KSLog.log(level: LogLevel, tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = performLog(level, tag, e, messageBuilder) +suspend inline fun KSLog.logS(level: LogLevel, tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = performLogS(level, tag, e, messageBuilder) +inline fun KSLog.log(level: LogLevel, e: Throwable? = null, noinline messageBuilder: () -> Any) = performLog(level, null, e, messageBuilder) +suspend inline fun KSLog.logS(level: LogLevel, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = performLogS(level, null, e, messageBuilder) +inline fun KSLog.log(level: LogLevel, message: Any, e: Throwable? = null) = performLog(level, message, e) +inline fun KSLog.log(level: LogLevel, tag: String, message: Any, e: Throwable? = null) = performLog(level, tag, message, e) -inline fun KSLog.debug(e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.DEBUG, null, e, messageBuilder) -inline fun KSLog.debug(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.DEBUG, tag, e, messageBuilder) -suspend inline fun KSLog.debugS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.DEBUG, null, e, messageBuilder) -suspend inline fun KSLog.debugS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(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) +inline fun KSLog.debug(e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.DEBUG, null, e, messageBuilder) +inline fun KSLog.debug(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.DEBUG, tag, e, messageBuilder) +suspend inline fun KSLog.debugS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.DEBUG, null, e, messageBuilder) +suspend inline fun KSLog.debugS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.DEBUG, tag, e, messageBuilder) +inline fun KSLog.debug(message: Any, e: Throwable? = null) = log(LogLevel.DEBUG, message, e) +inline fun KSLog.debug(tag: String, message: Any, e: Throwable? = null) = log(LogLevel.DEBUG, tag, message, e) -inline fun KSLog.verbose(e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.VERBOSE, null, e, messageBuilder) -inline fun KSLog.verbose(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.VERBOSE, tag, e, messageBuilder) -suspend inline fun KSLog.verboseS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.VERBOSE, null, e, messageBuilder) -suspend inline fun KSLog.verboseS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(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 KSLog.verbose(e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.VERBOSE, null, e, messageBuilder) +inline fun KSLog.verbose(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.VERBOSE, tag, e, messageBuilder) +suspend inline fun KSLog.verboseS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.VERBOSE, null, e, messageBuilder) +suspend inline fun KSLog.verboseS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.VERBOSE, tag, e, messageBuilder) +inline fun KSLog.verbose(message: Any, e: Throwable? = null) = log(LogLevel.VERBOSE, message, e) +inline fun KSLog.verbose(tag: String, message: Any, e: Throwable? = null) = log(LogLevel.VERBOSE, tag, message, e) -inline fun KSLog.info(e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.INFO, null, e, messageBuilder) -inline fun KSLog.info(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.INFO, tag, e, messageBuilder) -suspend inline fun KSLog.infoS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.INFO, null, e, messageBuilder) -suspend inline fun KSLog.infoS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(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 KSLog.info(e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.INFO, null, e, messageBuilder) +inline fun KSLog.info(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.INFO, tag, e, messageBuilder) +suspend inline fun KSLog.infoS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.INFO, null, e, messageBuilder) +suspend inline fun KSLog.infoS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.INFO, tag, e, messageBuilder) +inline fun KSLog.info(message: Any, e: Throwable? = null) = log(LogLevel.INFO, message, e) +inline fun KSLog.info(tag: String, message: Any, e: Throwable? = null) = log(LogLevel.INFO, tag, message, e) -inline fun KSLog.warning(e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.WARNING, null, e, messageBuilder) -inline fun KSLog.warning(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.WARNING, tag, e, messageBuilder) -suspend inline fun KSLog.warningS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.WARNING, null, e, messageBuilder) -suspend inline fun KSLog.warningS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(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 KSLog.warning(e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.WARNING, null, e, messageBuilder) +inline fun KSLog.warning(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.WARNING, tag, e, messageBuilder) +suspend inline fun KSLog.warningS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.WARNING, null, e, messageBuilder) +suspend inline fun KSLog.warningS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.WARNING, tag, e, messageBuilder) +inline fun KSLog.warning(message: Any, e: Throwable? = null) = log(LogLevel.WARNING, message, e) +inline fun KSLog.warning(tag: String, message: Any, e: Throwable? = null) = log(LogLevel.WARNING, tag, message, e) -inline fun KSLog.error(e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.ERROR, null, e, messageBuilder) -inline fun KSLog.error(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.ERROR, tag, e, messageBuilder) -suspend inline fun KSLog.errorS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.ERROR, null, e, messageBuilder) -suspend inline fun KSLog.errorS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(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 KSLog.error(e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.ERROR, null, e, messageBuilder) +inline fun KSLog.error(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.ERROR, tag, e, messageBuilder) +suspend inline fun KSLog.errorS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.ERROR, null, e, messageBuilder) +suspend inline fun KSLog.errorS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.ERROR, tag, e, messageBuilder) +inline fun KSLog.error(message: Any, e: Throwable? = null) = log(LogLevel.ERROR, message, e) +inline fun KSLog.error(tag: String, message: Any, e: Throwable? = null) = log(LogLevel.ERROR, tag, message, e) -inline fun KSLog.assert(e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.ASSERT, null, e, messageBuilder) -inline fun KSLog.assert(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.ASSERT, tag, e, messageBuilder) -suspend inline fun KSLog.assertS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.ASSERT, null, e, messageBuilder) -suspend inline fun KSLog.assertS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(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 KSLog.assert(e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.ASSERT, null, e, messageBuilder) +inline fun KSLog.assert(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = log(LogLevel.ASSERT, tag, e, messageBuilder) +suspend inline fun KSLog.assertS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.ASSERT, null, e, messageBuilder) +suspend inline fun KSLog.assertS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(LogLevel.ASSERT, tag, e, messageBuilder) +inline fun KSLog.assert(message: Any, e: Throwable? = null) = log(LogLevel.ASSERT, message, e) +inline fun KSLog.assert(tag: String, message: Any, e: Throwable? = null) = log(LogLevel.ASSERT, tag, message, e) // ----- Short names -inline fun KSLog.l(level: LogLevel, tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = log(level, tag, e, messageBuilder) -inline fun KSLog.l(level: LogLevel, e: Throwable? = null, noinline messageBuilder: () -> String) = log(level, e, messageBuilder) -suspend inline fun KSLog.lS(level: LogLevel, tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(level, tag, e, messageBuilder) -suspend inline fun KSLog.lS(level: LogLevel, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(level, 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 KSLog.l(level: LogLevel, tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = log(level, tag, e, messageBuilder) +inline fun KSLog.l(level: LogLevel, e: Throwable? = null, noinline messageBuilder: () -> Any) = log(level, e, messageBuilder) +suspend inline fun KSLog.lS(level: LogLevel, tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(level, tag, e, messageBuilder) +suspend inline fun KSLog.lS(level: LogLevel, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = logS(level, e, messageBuilder) +inline fun KSLog.l(level: LogLevel, message: Any, e: Throwable? = null) = log(level, message, e) +inline fun KSLog.l(level: LogLevel, tag: String, message: Any, e: Throwable? = null) = log(level, tag, message, e) -inline fun KSLog.d(e: Throwable? = null, noinline messageBuilder: () -> String) = debug(e, messageBuilder) -inline fun KSLog.d(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = debug(tag, e, messageBuilder) -suspend inline fun KSLog.dS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = debugS(e, messageBuilder) -suspend inline fun KSLog.dS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = debugS(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) +inline fun KSLog.d(e: Throwable? = null, noinline messageBuilder: () -> Any) = debug(e, messageBuilder) +inline fun KSLog.d(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = debug(tag, e, messageBuilder) +suspend inline fun KSLog.dS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = debugS(e, messageBuilder) +suspend inline fun KSLog.dS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = debugS(tag, e, messageBuilder) +inline fun KSLog.d(message: Any, e: Throwable? = null) = debug(message, e) +inline fun KSLog.d(tag: String, message: Any, e: Throwable? = null) = debug(tag, message, e) -inline fun KSLog.v(e: Throwable? = null, noinline messageBuilder: () -> String) = verbose(e, messageBuilder) -inline fun KSLog.v(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = verbose(tag, e, messageBuilder) -suspend inline fun KSLog.vS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = verboseS(e, messageBuilder) -suspend inline fun KSLog.vS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = verboseS(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 KSLog.v(e: Throwable? = null, noinline messageBuilder: () -> Any) = verbose(e, messageBuilder) +inline fun KSLog.v(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = verbose(tag, e, messageBuilder) +suspend inline fun KSLog.vS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = verboseS(e, messageBuilder) +suspend inline fun KSLog.vS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = verboseS(tag, e, messageBuilder) +inline fun KSLog.v(message: Any, e: Throwable? = null) = verbose(message, e) +inline fun KSLog.v(tag: String, message: Any, e: Throwable? = null) = verbose(tag, message, e) -inline fun KSLog.i(e: Throwable? = null, noinline messageBuilder: () -> String) = info(e, messageBuilder) -inline fun KSLog.i(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = info(tag, e, messageBuilder) -suspend inline fun KSLog.iS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = infoS(e, messageBuilder) -suspend inline fun KSLog.iS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = infoS(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 KSLog.i(e: Throwable? = null, noinline messageBuilder: () -> Any) = info(e, messageBuilder) +inline fun KSLog.i(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = info(tag, e, messageBuilder) +suspend inline fun KSLog.iS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = infoS(e, messageBuilder) +suspend inline fun KSLog.iS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = infoS(tag, e, messageBuilder) +inline fun KSLog.i(message: Any, e: Throwable? = null) = info(message, e) +inline fun KSLog.i(tag: String, message: Any, e: Throwable? = null) = info(tag, message, e) -inline fun KSLog.w(e: Throwable? = null, noinline messageBuilder: () -> String) = warning(e, messageBuilder) -inline fun KSLog.w(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = warning(tag, e, messageBuilder) -suspend inline fun KSLog.wS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = warningS(e, messageBuilder) -suspend inline fun KSLog.wS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = warningS(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 KSLog.w(e: Throwable? = null, noinline messageBuilder: () -> Any) = warning(e, messageBuilder) +inline fun KSLog.w(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = warning(tag, e, messageBuilder) +suspend inline fun KSLog.wS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = warningS(e, messageBuilder) +suspend inline fun KSLog.wS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = warningS(tag, e, messageBuilder) +inline fun KSLog.w(message: Any, e: Throwable? = null) = warning(message, e) +inline fun KSLog.w(tag: String, message: Any, e: Throwable? = null) = warning(tag, message, e) -inline fun KSLog.e(e: Throwable? = null, noinline messageBuilder: () -> String) = error(e, messageBuilder) -inline fun KSLog.e(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = error(tag, e, messageBuilder) -suspend inline fun KSLog.eS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = errorS(e, messageBuilder) -suspend inline fun KSLog.eS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = errorS(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 KSLog.e(e: Throwable? = null, noinline messageBuilder: () -> Any) = error(e, messageBuilder) +inline fun KSLog.e(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = error(tag, e, messageBuilder) +suspend inline fun KSLog.eS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = errorS(e, messageBuilder) +suspend inline fun KSLog.eS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = errorS(tag, e, messageBuilder) +inline fun KSLog.e(message: Any, e: Throwable? = null) = error(message, e) +inline fun KSLog.e(tag: String, message: Any, e: Throwable? = null) = error(tag, message, e) -inline fun KSLog.wtf(e: Throwable? = null, noinline messageBuilder: () -> String) = assert(e, messageBuilder) -inline fun KSLog.wtf(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> String) = assert(tag, e, messageBuilder) -suspend inline fun KSLog.wtfS(e: Throwable? = null, noinline messageBuilder: suspend () -> String) = assertS(e, messageBuilder) -suspend inline fun KSLog.wtfS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = assertS(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 KSLog.wtf(e: Throwable? = null, noinline messageBuilder: () -> Any) = assert(e, messageBuilder) +inline fun KSLog.wtf(tag: String?, e: Throwable? = null, noinline messageBuilder: () -> Any) = assert(tag, e, messageBuilder) +suspend inline fun KSLog.wtfS(e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = assertS(e, messageBuilder) +suspend inline fun KSLog.wtfS(tag: String?, e: Throwable? = null, noinline messageBuilder: suspend () -> Any) = assertS(tag, e, messageBuilder) +inline fun KSLog.wtf(message: Any, e: Throwable? = null) = assert(message, e) +inline fun KSLog.wtf(tag: String, message: Any, e: Throwable? = null) = assert(tag, message, e) diff --git a/src/commonMain/kotlin/KSLog.kt b/src/commonMain/kotlin/KSLog.kt index c387adf..695e127 100644 --- a/src/commonMain/kotlin/KSLog.kt +++ b/src/commonMain/kotlin/KSLog.kt @@ -11,20 +11,22 @@ enum class LogLevel { } interface KSLog { - fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) - fun performLog(level: LogLevel, message: String, throwable: Throwable?) = performLog(level, null, message, throwable) + fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) + fun performLog(level: LogLevel, message: Any, throwable: Throwable?) = performLog(level, null, message, throwable) fun performLog( level: LogLevel, tag: String?, throwable: Throwable?, - messageBuilder: () -> String + messageBuilder: () -> Any ) = performLog(level, tag, messageBuilder(), throwable) suspend fun performLogS( level: LogLevel, tag: String?, throwable: Throwable?, - messageBuilder: suspend () -> String + messageBuilder: suspend () -> Any ) = performLog(level, tag, messageBuilder(), throwable) + + companion object : KSLog { private var defaultLogger: KSLog? = null var default: KSLog @@ -36,27 +38,27 @@ 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, message: String, throwable: Throwable?) = default.performLog(level, message, throwable) + override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) = default.performLog(level, tag, message, throwable) + override fun performLog(level: LogLevel, message: Any, throwable: Throwable?) = default.performLog(level, message, throwable) override fun performLog( level: LogLevel, tag: String?, throwable: Throwable?, - messageBuilder: () -> String + messageBuilder: () -> Any ) = default.performLog(level, tag, throwable, messageBuilder) override suspend fun performLogS( level: LogLevel, tag: String?, throwable: Throwable?, - messageBuilder: suspend () -> String + messageBuilder: suspend () -> Any ) = default.performLogS(level, tag, throwable, messageBuilder) } } -operator fun KSLog.invoke(performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit) = CallbackKSLog(performLogCallback) +operator fun KSLog.invoke(performLogCallback: (level: LogLevel, tag: String?, message: Any, throwable: Throwable?) -> Unit) = CallbackKSLog(performLogCallback) -internal expect val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit +internal expect val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit fun KSLog( defaultTag: String, diff --git a/src/commonMain/kotlin/KSLogTypealiases.kt b/src/commonMain/kotlin/KSLogTypealiases.kt index a9ad757..d36b5a1 100644 --- a/src/commonMain/kotlin/KSLogTypealiases.kt +++ b/src/commonMain/kotlin/KSLogTypealiases.kt @@ -1,6 +1,6 @@ package dev.inmo.kslog.common -typealias MessageFormatter = (l: LogLevel, t: String, m: String, Throwable?) -> String +typealias MessageFormatter = (l: LogLevel, t: String, m: Any, Throwable?) -> String typealias MessageFilter = (l: LogLevel, t: String?, Throwable?) -> Boolean val defaultMessageFormatter: MessageFormatter = { l, t, m, _ -> "[$l] $t - $m" } diff --git a/src/commonMain/kotlin/TagLogger.kt b/src/commonMain/kotlin/TagLogger.kt index 9fc39ac..123c5dd 100644 --- a/src/commonMain/kotlin/TagLogger.kt +++ b/src/commonMain/kotlin/TagLogger.kt @@ -4,7 +4,7 @@ import kotlin.jvm.JvmInline @JvmInline value class TagLogger(val tag: String) : KSLog { - override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) { + override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) { KSLog.performLog(level, tag ?: this.tag, message, throwable) } } diff --git a/src/jsMain/kotlin/ActualLogger.kt b/src/jsMain/kotlin/ActualLogger.kt index 0ec2204..c647d75 100644 --- a/src/jsMain/kotlin/ActualLogger.kt +++ b/src/jsMain/kotlin/ActualLogger.kt @@ -1,6 +1,6 @@ package dev.inmo.kslog.common -internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = { l, t, m, e -> +internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = { l, t, m, e -> val args = e ?.let { arrayOf(m, e) } ?: arrayOf(m) diff --git a/src/jvmMain/kotlin/ActualLogger.kt b/src/jvmMain/kotlin/ActualLogger.kt index 1164043..4f4d9ee 100644 --- a/src/jvmMain/kotlin/ActualLogger.kt +++ b/src/jvmMain/kotlin/ActualLogger.kt @@ -20,8 +20,8 @@ private fun Logger.doLog( m, e ) -internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = { l, t, m, e -> - defaultKSLogLogger.doLog(l, t, m, e) +internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = { l, t, m, e -> + defaultKSLogLogger.doLog(l, t, m.toString(), e) } fun KSLog( @@ -31,7 +31,7 @@ fun KSLog( messageFormatter: MessageFormatter = defaultMessageFormatter ) = KSLog { l, t, m, e -> if (!filter(l, t, e)) return@KSLog - val text = messageFormatter(l,t ?: defaultTag,m,e) + val text = messageFormatter(l,t ?: defaultTag,m.toString(),e) logger.doLog(l, t ?: defaultTag, text, e) } diff --git a/src/main/kotlin/ActualLogger.kt b/src/main/kotlin/ActualLogger.kt index 03f9b03..8a37e03 100644 --- a/src/main/kotlin/ActualLogger.kt +++ b/src/main/kotlin/ActualLogger.kt @@ -3,13 +3,14 @@ package dev.inmo.kslog.common import android.util.Log -internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = { l, t, m, e -> +internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = { l, t, m, e -> + val messageString = m.toString() when(l) { - LogLevel.DEBUG -> Log.d(t, m, e) - LogLevel.VERBOSE -> Log.v(t, m, e) - LogLevel.INFO -> Log.i(t, m, e) - LogLevel.WARNING -> Log.w(t, m, e) - LogLevel.ERROR -> Log.e(t, m, e) - LogLevel.ASSERT -> Log.wtf(t, m, e) + LogLevel.DEBUG -> Log.d(t, messageString, e) + LogLevel.VERBOSE -> Log.v(t, messageString, e) + LogLevel.INFO -> Log.i(t, messageString, e) + LogLevel.WARNING -> Log.w(t, messageString, e) + LogLevel.ERROR -> Log.e(t, messageString, e) + LogLevel.ASSERT -> Log.wtf(t, messageString, e) } } From be48c2559edd3ea19f34bb60c5df5be0e93696bd Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 30 Jul 2022 14:15:55 +0600 Subject: [PATCH 5/7] taggedLogger --- CHANGELOG.md | 1 + src/commonMain/kotlin/AutoLoggers.kt | 2 ++ 2 files changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdac7ce..ac7e6ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ **THIS UPDATE CONTAINS BREAKING CHANGES** * **BREAKING CHANGE** Any `KSLog` now accept any object. It was made to allow to work with structured logging +* New factory for `TagLogger`: fun `taggedLogger` ## 0.3.2 diff --git a/src/commonMain/kotlin/AutoLoggers.kt b/src/commonMain/kotlin/AutoLoggers.kt index e1803e7..99128a2 100644 --- a/src/commonMain/kotlin/AutoLoggers.kt +++ b/src/commonMain/kotlin/AutoLoggers.kt @@ -4,3 +4,5 @@ val Any.logTag get() = this::class.simpleName ?: error("Unable to retrieve log tag") val Any.logger: KSLog get() = TagLogger(logTag) + +fun taggedLogger(tagBase: Any): KSLog = tagBase.logger From 4d98b7df18f6e150441d7f22c2c1fa8021384259 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 30 Jul 2022 14:56:26 +0600 Subject: [PATCH 6/7] rewrite way of filters work --- CHANGELOG.md | 3 ++ src/commonMain/kotlin/CallbackKSLog.kt | 4 ++- src/commonMain/kotlin/DefaultKSLog.kt | 31 +++++++++------- src/commonMain/kotlin/KSLog.kt | 24 +++++++++---- src/commonMain/kotlin/filter/FilterKSLog.kt | 36 +++++++++++++++++++ src/commonMain/kotlin/typed/TypedKSLog.kt | 23 ++++++++++++ .../kotlin/typed/TypedKSLogBuilder.kt | 28 +++++++++++++++ src/jvmMain/kotlin/ActualLogger.kt | 17 +++++++-- 8 files changed, 142 insertions(+), 24 deletions(-) create mode 100644 src/commonMain/kotlin/filter/FilterKSLog.kt create mode 100644 src/commonMain/kotlin/typed/TypedKSLog.kt create mode 100644 src/commonMain/kotlin/typed/TypedKSLogBuilder.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index ac7e6ed..57081bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ * **BREAKING CHANGE** Any `KSLog` now accept any object. It was made to allow to work with structured logging * New factory for `TagLogger`: fun `taggedLogger` +* New logger has been added: `TypedLogger` +* New logger has been added: `FilterLogger` +* **BREAKING CHANGE** Logger `DefaultKSLog` lost its constructor with `filter`. Currently, it is still available as factory function ## 0.3.2 diff --git a/src/commonMain/kotlin/CallbackKSLog.kt b/src/commonMain/kotlin/CallbackKSLog.kt index f801116..7d64495 100644 --- a/src/commonMain/kotlin/CallbackKSLog.kt +++ b/src/commonMain/kotlin/CallbackKSLog.kt @@ -1,7 +1,9 @@ package dev.inmo.kslog.common +typealias SimpleKSLogCallback = (level: LogLevel, tag: String?, message: Any, throwable: Throwable?) -> Unit + class CallbackKSLog( - private val performLogCallback: (level: LogLevel, tag: String?, message: Any, throwable: Throwable?) -> Unit + private val performLogCallback: SimpleKSLogCallback ) : KSLog { override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) = performLogCallback(level, tag, message, throwable) } diff --git a/src/commonMain/kotlin/DefaultKSLog.kt b/src/commonMain/kotlin/DefaultKSLog.kt index 094b83e..823fd3d 100644 --- a/src/commonMain/kotlin/DefaultKSLog.kt +++ b/src/commonMain/kotlin/DefaultKSLog.kt @@ -1,27 +1,24 @@ package dev.inmo.kslog.common +import dev.inmo.kslog.common.filter.filtered + class DefaultKSLog( private val defaultTag: String, - val filter: MessageFilter = { _, _, _ -> true }, private val messageFormatter: MessageFormatter = defaultMessageFormatter, private val logging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = defaultLogging ) : KSLog { override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) { val tag = tag ?: defaultTag - if (filter(level, tag, throwable)) { - val text = messageFormatter(level, tag, message, throwable) - logging(level, tag, text, throwable) - } + val text = messageFormatter(level, tag, message, throwable) + logging(level, tag, text, throwable) } override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> Any) { val tag = tag ?: defaultTag - if (filter(level, tag, throwable)) { - val text = messageFormatter(level, tag, messageBuilder(), throwable) - logging(level, tag, text, throwable) - } + val text = messageFormatter(level, tag, messageBuilder(), throwable) + logging(level, tag, text, throwable) } override suspend fun performLogS( @@ -32,9 +29,17 @@ class DefaultKSLog( ) { val tag = tag ?: defaultTag - if (filter(level, tag, throwable)) { - val text = messageFormatter(level, tag, messageBuilder(), throwable) - logging(level, tag, text, throwable) - } + val text = messageFormatter(level, tag, messageBuilder(), throwable) + logging(level, tag, text, throwable) } } + +@Deprecated("Filtering should be replaced with FilterKSLog") +fun DefaultKSLog( + defaultTag: String, + filter: MessageFilter = { _, _, _ -> true }, + messageFormatter: MessageFormatter = defaultMessageFormatter, + logging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = defaultLogging +) = DefaultKSLog( + defaultTag, messageFormatter, logging +).filtered(filter) diff --git a/src/commonMain/kotlin/KSLog.kt b/src/commonMain/kotlin/KSLog.kt index 695e127..be76ca4 100644 --- a/src/commonMain/kotlin/KSLog.kt +++ b/src/commonMain/kotlin/KSLog.kt @@ -1,5 +1,7 @@ package dev.inmo.kslog.common +import dev.inmo.kslog.common.filter.filtered + enum class LogLevel { DEBUG, @@ -62,7 +64,16 @@ internal expect val defaultLogging: (level: LogLevel, tag: String, message: Any, fun KSLog( defaultTag: String, - filter: MessageFilter = { _, _, _ -> true }, + messageFormatter: MessageFormatter = defaultMessageFormatter +): KSLog = DefaultKSLog( + defaultTag, + messageFormatter +) + +@Deprecated("Filtering should be replaced with FilterKSLog") +fun KSLog( + defaultTag: String, + filter: MessageFilter, messageFormatter: MessageFormatter = defaultMessageFormatter ): KSLog = DefaultKSLog( defaultTag, @@ -76,9 +87,9 @@ fun KSLog( messageFormatter: MessageFormatter = defaultMessageFormatter ): KSLog { val levels = levels.toSet() - return KSLog (defaultTag, { l, _, _ -> + return KSLog (defaultTag, messageFormatter).filtered { l, _, _ -> l in levels - }, messageFormatter) + } } fun KSLog( @@ -95,8 +106,7 @@ fun KSLog( messageFormatter: MessageFormatter = defaultMessageFormatter, ): KSLog = KSLog ( defaultTag, - { l, _, _ -> - minLoggingLevel.ordinal <= l.ordinal - }, messageFormatter -) +).filtered { l, _, _ -> + minLoggingLevel.ordinal <= l.ordinal +} diff --git a/src/commonMain/kotlin/filter/FilterKSLog.kt b/src/commonMain/kotlin/filter/FilterKSLog.kt new file mode 100644 index 0000000..9f1ce1a --- /dev/null +++ b/src/commonMain/kotlin/filter/FilterKSLog.kt @@ -0,0 +1,36 @@ +package dev.inmo.kslog.common.filter + +import dev.inmo.kslog.common.* + +class FilterLogger( + private val fallbackLogger: KSLog, + private val messageFilter: MessageFilter +) : KSLog { + override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) { + if (messageFilter(level, tag, throwable)) { + fallbackLogger.performLog(level, tag, message, throwable) + } + } + + override fun performLog(level: LogLevel, message: Any, throwable: Throwable?) { + if (messageFilter(level, null, throwable)) { + fallbackLogger.performLog(level, message, throwable) + } + } + + override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> Any) { + if (messageFilter(level, tag, throwable)) { + fallbackLogger.performLog(level, tag, throwable, messageBuilder) + } + } + + override suspend fun performLogS(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: suspend () -> Any) { + if (messageFilter(level, tag, throwable)) { + fallbackLogger.performLogS(level, tag, throwable, messageBuilder) + } + } +} + +fun KSLog.filtered( + filter: MessageFilter +) = FilterLogger(this, filter) diff --git a/src/commonMain/kotlin/typed/TypedKSLog.kt b/src/commonMain/kotlin/typed/TypedKSLog.kt new file mode 100644 index 0000000..eab822f --- /dev/null +++ b/src/commonMain/kotlin/typed/TypedKSLog.kt @@ -0,0 +1,23 @@ +package dev.inmo.kslog.common.typed + +import dev.inmo.kslog.common.* +import kotlin.reflect.KClass + +class TypedKSLog( + private val typedLoggers: Map?, KSLog> +) : KSLog { + override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) { + (typedLoggers[message::class] ?: typedLoggers[null]) ?.performLog(level, tag, message, throwable) + } + override fun performLog(level: LogLevel, message: Any, throwable: Throwable?) { + (typedLoggers[message::class] ?: typedLoggers[null]) ?.performLog(level, message, throwable) + } + override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> Any) { + val message = messageBuilder() + (typedLoggers[message::class] ?: typedLoggers[null]) ?.performLog(level, tag, message, throwable) + } + override suspend fun performLogS(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: suspend () -> Any) { + val message = messageBuilder() + (typedLoggers[message::class] ?: typedLoggers[null]) ?.performLog(level, tag, message, throwable) + } +} diff --git a/src/commonMain/kotlin/typed/TypedKSLogBuilder.kt b/src/commonMain/kotlin/typed/TypedKSLogBuilder.kt new file mode 100644 index 0000000..ee93e4a --- /dev/null +++ b/src/commonMain/kotlin/typed/TypedKSLogBuilder.kt @@ -0,0 +1,28 @@ +package dev.inmo.kslog.common.typed + +import dev.inmo.kslog.common.* +import kotlin.reflect.KClass + +class TypedKSLogBuilder( + private val preset: Map?, KSLog> = emptyMap() +) { + private val loggers = mutableMapOf?, KSLog>().apply { + putAll(preset) + } + + fun on(kClass: KClass<*>?, with: KSLog) = loggers.put(kClass, with) + fun on(kClass: KClass<*>?, with: SimpleKSLogCallback) = loggers.put(kClass, CallbackKSLog(with)) + + fun default(with: KSLog) = on(null, with) + fun default(with: SimpleKSLogCallback) = on(null, with) + + inline fun on(with: KSLog) = on(T::class, with) + inline fun on(noinline with: SimpleKSLogCallback) = on(T::class, with) + + fun build() = TypedKSLog(loggers.toMap()) +} + +inline fun buildTypedLogger( + preset: Map?, KSLog> = emptyMap(), + block: TypedKSLogBuilder.() -> Unit +) = TypedKSLogBuilder(preset).apply(block).build() diff --git a/src/jvmMain/kotlin/ActualLogger.kt b/src/jvmMain/kotlin/ActualLogger.kt index 4f4d9ee..aa3ac13 100644 --- a/src/jvmMain/kotlin/ActualLogger.kt +++ b/src/jvmMain/kotlin/ActualLogger.kt @@ -1,5 +1,6 @@ package dev.inmo.kslog.common +import dev.inmo.kslog.common.filter.filtered import java.util.logging.Level import java.util.logging.Logger @@ -24,10 +25,11 @@ internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, defaultKSLogLogger.doLog(l, t, m.toString(), e) } +@Deprecated("Filtering should be replaced with FilterKSLog") fun KSLog( defaultTag: String, logger: Logger, - filter: MessageFilter = { _, _, _ -> true }, + filter: MessageFilter, messageFormatter: MessageFormatter = defaultMessageFormatter ) = KSLog { l, t, m, e -> if (!filter(l, t, e)) return@KSLog @@ -35,6 +37,15 @@ fun KSLog( logger.doLog(l, t ?: defaultTag, text, e) } +fun KSLog( + defaultTag: String, + logger: Logger, + messageFormatter: MessageFormatter = defaultMessageFormatter +) = KSLog { l, t, m, e -> + val text = messageFormatter(l,t ?: defaultTag,m.toString(),e) + logger.doLog(l, t ?: defaultTag, text, e) +} + fun KSLog( defaultTag: String, logger: Logger, @@ -42,7 +53,7 @@ fun KSLog( messageFormatter: MessageFormatter = defaultMessageFormatter ): KSLog { val levels = levels.toSet() - return KSLog (defaultTag, logger, { l, _, _ -> l in levels }, messageFormatter) + return KSLog (defaultTag, logger, messageFormatter).filtered { l, _, _ -> l in levels } } fun KSLog( @@ -59,4 +70,4 @@ fun KSLog( logger: Logger, minLoggingLevel: LogLevel = LogLevel.values().first(), messageFormatter: MessageFormatter = defaultMessageFormatter -): KSLog = KSLog (defaultTag, logger, { l, _, _ -> minLoggingLevel.ordinal <= l.ordinal }, messageFormatter) +): KSLog = KSLog (defaultTag, logger, messageFormatter).filtered { l, _, _ -> minLoggingLevel.ordinal <= l.ordinal } From 8dc96f708ecbf9c9376851534500a0b0822f4f50 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 30 Jul 2022 15:12:46 +0600 Subject: [PATCH 7/7] fixes --- src/commonMain/kotlin/filter/FilterKSLog.kt | 6 +----- src/commonMain/kotlin/filter/FilteredExtension.kt | 8 ++++++++ 2 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 src/commonMain/kotlin/filter/FilteredExtension.kt diff --git a/src/commonMain/kotlin/filter/FilterKSLog.kt b/src/commonMain/kotlin/filter/FilterKSLog.kt index 9f1ce1a..d369a6c 100644 --- a/src/commonMain/kotlin/filter/FilterKSLog.kt +++ b/src/commonMain/kotlin/filter/FilterKSLog.kt @@ -2,7 +2,7 @@ package dev.inmo.kslog.common.filter import dev.inmo.kslog.common.* -class FilterLogger( +class FilterKSLog( private val fallbackLogger: KSLog, private val messageFilter: MessageFilter ) : KSLog { @@ -30,7 +30,3 @@ class FilterLogger( } } } - -fun KSLog.filtered( - filter: MessageFilter -) = FilterLogger(this, filter) diff --git a/src/commonMain/kotlin/filter/FilteredExtension.kt b/src/commonMain/kotlin/filter/FilteredExtension.kt new file mode 100644 index 0000000..443ea50 --- /dev/null +++ b/src/commonMain/kotlin/filter/FilteredExtension.kt @@ -0,0 +1,8 @@ +package dev.inmo.kslog.common.filter + +import dev.inmo.kslog.common.KSLog +import dev.inmo.kslog.common.MessageFilter + +fun KSLog.filtered( + filter: MessageFilter +) = FilterKSLog(this, filter)