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
* Add `trace` logging level
* Change overall table of log level accordance
* Now you may change default platform logger
## 1.2.4

View File

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

View File

@ -2,8 +2,7 @@ package dev.inmo.kslog.common
import android.util.Log
internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = { l, t, m, e ->
actual var KSLoggerDefaultPlatformLoggerLambda: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = { l, t, m, e ->
val messageString = m.toString()
when(l) {
LogLevel.TRACE -> Log.d(t, messageString, e)
@ -14,4 +13,4 @@ internal actual val defaultLogging: (level: LogLevel, tag: String, message: Any,
LogLevel.ERROR -> Log.e(t, messageString, e)
LogLevel.ASSERT -> Log.wtf(t, messageString, e)
}
}
}

View File

@ -3,7 +3,7 @@ package dev.inmo.kslog.common
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 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(
private val defaultTag: String,
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 {
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)
logging(level, tag, text, throwable)
val text = messageFormatter(level, resultTag, message, throwable)
logging(level, resultTag, text, throwable)
}
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)
logging(level, tag, text, throwable)
val text = messageFormatter(level, resultTag, messageBuilder(), throwable)
logging(level, resultTag, text, throwable)
}
override suspend fun performLogS(
@ -36,10 +36,10 @@ class DefaultKSLog(
throwable: Throwable?,
messageBuilder: suspend () -> Any
) {
val tag = tag ?: defaultTag
val resultTag = tag ?: defaultTag
val text = messageFormatter(level, tag, messageBuilder(), throwable)
logging(level, tag, text, throwable)
val text = messageFormatter(level, resultTag, messageBuilder(), throwable)
logging(level, resultTag, text, throwable)
}
}
@ -48,7 +48,7 @@ fun DefaultKSLog(
defaultTag: String,
filter: MessageFilter = { _, _, _ -> true },
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(
defaultTag, messageFormatter, logging
).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)
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 ->
println(defaultMessageFormatter(l, t, m, e))
}
@ -131,9 +130,9 @@ fun KSLog(
levels: Iterable<LogLevel>,
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog {
val levels = levels.toSet()
val levelsSet = levels.toSet()
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.Logger
private val defaultKSLogLogger by lazy {
internal val defaultKSLogLogger by lazy {
Logger.getLogger("KSLog")
}
private fun Logger.doLog(
internal fun Logger.doLog(
l: LogLevel, t: String, m: String, e: Throwable?
) = log(
when(l) {
LogLevel.TRACE -> Level.ALL
LogLevel.DEBUG -> Level.FINEST
LogLevel.TRACE -> Level.FINEST
LogLevel.DEBUG -> Level.FINER
LogLevel.VERBOSE -> Level.FINE
LogLevel.INFO -> Level.INFO
LogLevel.WARNING -> Level.WARNING
@ -22,9 +22,6 @@ private fun Logger.doLog(
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)
}
@Deprecated("Filtering should be replaced with FilterKSLog")
fun KSLog(
@ -53,8 +50,8 @@ fun KSLog(
levels: Iterable<LogLevel>,
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog {
val levels = levels.toSet()
return KSLog (defaultTag, logger, messageFormatter).filtered { l, _, _ -> l in levels }
val levelsSet = levels.toSet()
return KSLog (defaultTag, logger, messageFormatter).filtered { l, _, _ -> l in levelsSet }
}
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