a lot of changes and improvements

This commit is contained in:
InsanusMokrassar 2023-11-18 17:55:19 +06:00
parent 81b32b0119
commit 0348756729
21 changed files with 99 additions and 64 deletions

View File

@ -3,6 +3,8 @@
## 1.2.5 ## 1.2.5
* Add `trace` logging level * Add `trace` logging level
* Change overall table of log level accordance
* Now you may change default platform logger
## 1.2.4 ## 1.2.4

View File

@ -1 +0,0 @@
<manifest/>

View File

@ -2,8 +2,7 @@ package dev.inmo.kslog.common
import android.util.Log import android.util.Log
actual var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, 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() val messageString = m.toString()
when(l) { when(l) {
LogLevel.TRACE -> Log.d(t, messageString, e) LogLevel.TRACE -> Log.d(t, messageString, e)

View File

@ -3,7 +3,7 @@ package dev.inmo.kslog.common
import dev.inmo.kslog.common.filter.filtered import dev.inmo.kslog.common.filter.filtered
/** /**
* Logger based on [defaultLogging] or [logging] parameter * Logger based on [KSLoggerDefaultPlatformLoggerLambda] or [logging] parameter
* *
* @param defaultTag will be used in case when `tag` parameter in [performLog] omitted * @param defaultTag will be used in case when `tag` parameter in [performLog] omitted
* @param messageFormatter special formatter which creating [String] inside of [performLog] for each call. Defaults to * @param messageFormatter special formatter which creating [String] inside of [performLog] for each call. Defaults to
@ -14,20 +14,20 @@ import dev.inmo.kslog.common.filter.filtered
class DefaultKSLog( class DefaultKSLog(
private val defaultTag: String, private val defaultTag: String,
private val messageFormatter: MessageFormatter = defaultMessageFormatter, private val messageFormatter: MessageFormatter = defaultMessageFormatter,
private val logging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = defaultLogging private val logging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = KSLoggerDefaultPlatformLoggerLambda
) : KSLog { ) : KSLog {
override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) { override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) {
val tag = tag ?: defaultTag val resultTag = tag ?: defaultTag
val text = messageFormatter(level, tag, message, throwable) val text = messageFormatter(level, resultTag, message, throwable)
logging(level, tag, text, throwable) logging(level, resultTag, text, throwable)
} }
override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> Any) { override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> Any) {
val tag = tag ?: defaultTag val resultTag = tag ?: defaultTag
val text = messageFormatter(level, tag, messageBuilder(), throwable) val text = messageFormatter(level, resultTag, messageBuilder(), throwable)
logging(level, tag, text, throwable) logging(level, resultTag, text, throwable)
} }
override suspend fun performLogS( override suspend fun performLogS(
@ -36,10 +36,10 @@ class DefaultKSLog(
throwable: Throwable?, throwable: Throwable?,
messageBuilder: suspend () -> Any messageBuilder: suspend () -> Any
) { ) {
val tag = tag ?: defaultTag val resultTag = tag ?: defaultTag
val text = messageFormatter(level, tag, messageBuilder(), throwable) val text = messageFormatter(level, resultTag, messageBuilder(), throwable)
logging(level, tag, text, throwable) logging(level, resultTag, text, throwable)
} }
} }
@ -48,7 +48,7 @@ fun DefaultKSLog(
defaultTag: String, defaultTag: String,
filter: MessageFilter = { _, _, _ -> true }, filter: MessageFilter = { _, _, _ -> true },
messageFormatter: MessageFormatter = defaultMessageFormatter, messageFormatter: MessageFormatter = defaultMessageFormatter,
logging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = defaultLogging logging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = KSLoggerDefaultPlatformLoggerLambda
) = DefaultKSLog( ) = DefaultKSLog(
defaultTag, messageFormatter, logging defaultTag, messageFormatter, logging
).filtered(filter) ).filtered(filter)

View File

@ -89,7 +89,6 @@ interface KSLog {
operator fun KSLog.invoke(performLogCallback: (level: LogLevel, tag: String?, message: Any, 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: Any, throwable: Throwable?) -> Unit
internal val printlnLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = { l, t, m, e -> internal val printlnLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = { l, t, m, e ->
println(defaultMessageFormatter(l, t, m, e)) println(defaultMessageFormatter(l, t, m, e))
} }
@ -131,9 +130,9 @@ fun KSLog(
levels: Iterable<LogLevel>, levels: Iterable<LogLevel>,
messageFormatter: MessageFormatter = defaultMessageFormatter messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog { ): KSLog {
val levels = levels.toSet() val levelsSet = levels.toSet()
return KSLog (defaultTag, messageFormatter).filtered { l, _, _ -> return KSLog (defaultTag, messageFormatter).filtered { l, _, _ ->
l in levels l in levelsSet
} }
} }

View File

@ -0,0 +1,7 @@
package dev.inmo.kslog.common
/**
* Default logger for current platform. You may change it, but you should remember that it is global logger
* used in [KSLog.default] by default
*/
expect var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit

View File

@ -1,16 +0,0 @@
package dev.inmo.kslog.common
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)
when (l) {
LogLevel.TRACE,
LogLevel.DEBUG -> console.log(*args)
LogLevel.VERBOSE,
LogLevel.INFO -> console.info(*args)
LogLevel.WARNING -> console.warn(*args)
LogLevel.ERROR,
LogLevel.ASSERT -> console.error(*args)
}
}

View File

@ -0,0 +1,24 @@
package dev.inmo.kslog.common
import kotlin.js.Console
external interface ExtendedConsole : Console {
fun trace()
fun debug(vararg o: Any?)
fun assert(vararg o: Any?)
}
/**
* [https://developer.mozilla.org/en-US/docs/Web/API/console/debug_static](https://developer.mozilla.org/en-US/docs/Web/API/console/debug_static)
*/
fun Console.debug(vararg args: Any?) = unsafeCast<ExtendedConsole>().debug(*args)
/**
* [https://developer.mozilla.org/en-US/docs/Web/APtraceI/console/assert_static](https://developer.mozilla.org/en-US/docs/Web/API/console/assert_static)
*/
fun Console.assert(vararg args: Any?) = unsafeCast<ExtendedConsole>().assert(*args)
/**
* [https://developer.mozilla.org/en-US/docs/Web/API/console/trace_static](https://developer.mozilla.org/en-US/docs/Web/API/console/trace_static)
*/
fun Console.trace() = unsafeCast<ExtendedConsole>().trace()

View File

@ -0,0 +1,19 @@
package dev.inmo.kslog.common
actual var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = { l, _, m, e ->
val args = e ?.let {
arrayOf(m, e)
} ?: arrayOf(m)
when (l) {
LogLevel.TRACE -> {
console.trace()
console.debug(*args)
}
LogLevel.DEBUG -> console.debug(*args)
LogLevel.VERBOSE,
LogLevel.INFO -> console.info(*args)
LogLevel.WARNING -> console.warn(*args)
LogLevel.ERROR -> console.error(*args)
LogLevel.ASSERT -> console.assert(*args)
}
}

View File

@ -4,15 +4,15 @@ import dev.inmo.kslog.common.filter.filtered
import java.util.logging.Level import java.util.logging.Level
import java.util.logging.Logger import java.util.logging.Logger
private val defaultKSLogLogger by lazy { internal val defaultKSLogLogger by lazy {
Logger.getLogger("KSLog") Logger.getLogger("KSLog")
} }
private fun Logger.doLog( internal fun Logger.doLog(
l: LogLevel, t: String, m: String, e: Throwable? l: LogLevel, t: String, m: String, e: Throwable?
) = log( ) = log(
when(l) { when(l) {
LogLevel.TRACE -> Level.ALL LogLevel.TRACE -> Level.FINEST
LogLevel.DEBUG -> Level.FINEST LogLevel.DEBUG -> Level.FINER
LogLevel.VERBOSE -> Level.FINE LogLevel.VERBOSE -> Level.FINE
LogLevel.INFO -> Level.INFO LogLevel.INFO -> Level.INFO
LogLevel.WARNING -> Level.WARNING LogLevel.WARNING -> Level.WARNING
@ -22,9 +22,6 @@ private fun Logger.doLog(
m, m,
e 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)
}
@Deprecated("Filtering should be replaced with FilterKSLog") @Deprecated("Filtering should be replaced with FilterKSLog")
fun KSLog( fun KSLog(
@ -53,8 +50,8 @@ fun KSLog(
levels: Iterable<LogLevel>, levels: Iterable<LogLevel>,
messageFormatter: MessageFormatter = defaultMessageFormatter messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog { ): KSLog {
val levels = levels.toSet() val levelsSet = levels.toSet()
return KSLog (defaultTag, logger, messageFormatter).filtered { l, _, _ -> l in levels } return KSLog (defaultTag, logger, messageFormatter).filtered { l, _, _ -> l in levelsSet }
} }
fun KSLog( fun KSLog(

View File

@ -0,0 +1,5 @@
package dev.inmo.kslog.common
actual var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = { l, t, m, e ->
defaultKSLogLogger.doLog(l, t, m.toString(), e)
}

View File

@ -1,4 +0,0 @@
package dev.inmo.kslog.common
internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit
get() = printlnLogging

View File

@ -0,0 +1,4 @@
package dev.inmo.kslog.common
actual var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit =
printlnLogging

View File

@ -0,0 +1,4 @@
package dev.inmo.kslog.common
actual var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit =
printlnLogging

View File

@ -1,4 +0,0 @@
package dev.inmo.kslog.common
internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit
get() = printlnLogging

View File

@ -0,0 +1,4 @@
package dev.inmo.kslog.common
actual var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit =
printlnLogging

View File

@ -1,4 +0,0 @@
package dev.inmo.kslog.common
internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit
get() = printlnLogging

View File

@ -0,0 +1,4 @@
package dev.inmo.kslog.common
actual var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit =
printlnLogging

View File

@ -1,4 +0,0 @@
package dev.inmo.kslog.common
internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit
get() = printlnLogging

View File

@ -0,0 +1,4 @@
package dev.inmo.kslog.common
actual var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit =
printlnLogging

View File

@ -1,4 +0,0 @@
package dev.inmo.kslog.common
internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit
get() = printlnLogging