mirror of
https://github.com/InsanusMokrassar/KSLog.git
synced 2024-12-22 14:07:15 +00:00
rewrite way of filters work
This commit is contained in:
parent
be48c2559e
commit
4d98b7df18
@ -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
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
36
src/commonMain/kotlin/filter/FilterKSLog.kt
Normal file
36
src/commonMain/kotlin/filter/FilterKSLog.kt
Normal file
@ -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)
|
23
src/commonMain/kotlin/typed/TypedKSLog.kt
Normal file
23
src/commonMain/kotlin/typed/TypedKSLog.kt
Normal file
@ -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<KClass<*>?, 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)
|
||||
}
|
||||
}
|
28
src/commonMain/kotlin/typed/TypedKSLogBuilder.kt
Normal file
28
src/commonMain/kotlin/typed/TypedKSLogBuilder.kt
Normal file
@ -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<KClass<*>?, KSLog> = emptyMap()
|
||||
) {
|
||||
private val loggers = mutableMapOf<KClass<*>?, 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 <reified T: Any> on(with: KSLog) = on(T::class, with)
|
||||
inline fun <reified T: Any> on(noinline with: SimpleKSLogCallback) = on(T::class, with)
|
||||
|
||||
fun build() = TypedKSLog(loggers.toMap())
|
||||
}
|
||||
|
||||
inline fun buildTypedLogger(
|
||||
preset: Map<KClass<*>?, KSLog> = emptyMap(),
|
||||
block: TypedKSLogBuilder.() -> Unit
|
||||
) = TypedKSLogBuilder(preset).apply(block).build()
|
@ -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 }
|
||||
|
Loading…
Reference in New Issue
Block a user