From 3f4018f9691c3af59fd89bd813054b4260abfbb4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 3 Mar 2025 09:25:19 +0600 Subject: [PATCH] update KSLog part of library --- CHANGELOG.md | 3 ++ .../dev/inmo/tgbotapi/utils/DefaultKSLog.kt | 45 ++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cc1f0a184..a8c45ca5e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 24.0.2 +* `DefaultKTgBotAPIKSLog` will drop `CancellationException`s by default +* You may configure `DefaultKTgBotAPIKSLog` in simple way with `SetDefaultKTgBotAPIKSLog` + ## 24.0.1 * `Core`: diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt index 8ac4a4baa7..d781274496 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/DefaultKSLog.kt @@ -2,18 +2,59 @@ package dev.inmo.tgbotapi.utils import dev.inmo.kslog.common.KSLog import dev.inmo.kslog.common.LogLevel +import dev.inmo.kslog.common.MessageFilter import dev.inmo.kslog.common.TagLogger +import dev.inmo.kslog.common.filter.filtered +import kotlinx.coroutines.CancellationException /** * Default tag for [DefaultKTgBotAPIKSLog]. You may change it and tag will be changed since the near logging */ var DefaultKTgBotAPIKSLogSystemTag: String = "KTgBot" + +private inline fun CreateDefaultKSLogger( + crossinline loggerTagGetter: () -> String, + dropCancellationExceptions: Boolean = true, + noinline additionalLoggerMapper: (KSLog.() -> KSLog)? = null +): KSLog { + val filter: MessageFilter? = if (dropCancellationExceptions) { + { ll, message, e -> + e !is CancellationException + } + } else { + null + } + return KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> + TagLogger(loggerTagGetter()).performLog(level, tag, message, throwable) + }.let { + if (filter == null) { + additionalLoggerMapper ?.invoke(it) ?: it + } else { + val base = it.filtered(filter) + additionalLoggerMapper ?.invoke(base) ?: base + } + } +} /** * Default realization of [KSLog] which will be used everywhere where there is no some custom variant of [KSLog] * * By default, uses [KSLog] factory with lambda and tag [DefaultKTgBotAPIKSLogSystemTag] (which in fact falling back to * [KSLog.default] with `KTgBot` default tag) + * + * @see SetDefaultKTgBotAPIKSLog */ -var DefaultKTgBotAPIKSLog: KSLog = KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> - TagLogger(DefaultKTgBotAPIKSLogSystemTag).performLog(level, tag, message, throwable) +var DefaultKTgBotAPIKSLog: KSLog = CreateDefaultKSLogger({ DefaultKTgBotAPIKSLogSystemTag } ) + +/** + * Setting [DefaultKTgBotAPIKSLog] with applying of [dropCancellationExceptions] and [additionalFilter] to it + * + * @param dropCancellationExceptions Will drap coroutines job [CancellationException]s + * @param additionalLoggerMapper Receives [KSLog] to allow you to use extensions like [filtered]. Returned + * [KSLog] will be used as the result [KSLog] in [DefaultKTgBotAPIKSLog] + */ +fun SetDefaultKTgBotAPIKSLog( + dropCancellationExceptions: Boolean = true, + additionalLoggerMapper: (KSLog.() -> KSLog)? = null +) { + DefaultKTgBotAPIKSLog = CreateDefaultKSLogger({ DefaultKTgBotAPIKSLogSystemTag }, dropCancellationExceptions, additionalLoggerMapper) }