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 }