it's relovution, Johny

This commit is contained in:
InsanusMokrassar 2022-06-08 18:23:11 +06:00
parent 3f600f3691
commit 95779af7b6
7 changed files with 107 additions and 45 deletions

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
## 0.1.0
* All the platform specific logged factories has been renamed
## 0.0.3 ## 0.0.3
* Fixes in `JVM` logging * Fixes in `JVM` logging

View File

@ -9,5 +9,5 @@ android.enableJetifier=true
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.0.3 version=0.1.0
android_code_version=3 android_code_version=4

View File

@ -37,10 +37,14 @@ class CallbackKSLog(
} }
expect fun KSLog( fun KSLog(
defaultTag: String, defaultTag: String,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean
) : KSLog ) : KSLog = KSLog { l, t, m, e ->
if (filter(l, t ?: defaultTag, m, e)) {
KSLog.default.performLog(l, t ?: defaultTag, m, e)
}
}
fun KSLog( fun KSLog(
defaultTag: String, defaultTag: String,
@ -52,7 +56,7 @@ fun KSLog(
} }
} }
inline fun KSLog( fun KSLog(
defaultTag: String, defaultTag: String,
firstLevel: LogLevel, firstLevel: LogLevel,
secondLevel: LogLevel, secondLevel: LogLevel,

View File

@ -0,0 +1,5 @@
package dev.inmo.kslog.common
typealias MessageFormatter = (l: LogLevel, t: String, m: String, Throwable?) -> String
val defaultMessageFormatter: MessageFormatter = { l, t, m, _ -> "[$l] $t - $m" }

View File

@ -1,11 +1,12 @@
package dev.inmo.kslog.common package dev.inmo.kslog.common
fun KSLog( fun KSJSLog(
messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String, defaultTag: String,
messageFormatter: MessageFormatter = defaultMessageFormatter,
filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean
) = KSLog { l, t, m, e -> ) = KSLog { l, t, m, e ->
if (!filter(l, t, m, e)) return@KSLog if (!filter(l, t, m, e)) return@KSLog
val text = messageFormatter(l,t,m,e) val text = messageFormatter(l, t?:defaultTag, m, e)
val args = e ?.let { val args = e ?.let {
arrayOf(text, e) arrayOf(text, e)
} ?: arrayOf(text) } ?: arrayOf(text)
@ -19,12 +20,29 @@ fun KSLog(
} }
} }
actual fun KSLog( fun KSJSLog(
defaultTag: String, defaultTag: String,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean messageFormatter: MessageFormatter = defaultMessageFormatter,
levels: Iterable<LogLevel>
): KSLog { ): KSLog {
return KSLog( val levels = levels.toSet()
{ l, t, m, _ -> "[$l] ${t ?: defaultTag} - $m" }, return KSJSLog (defaultTag, messageFormatter) { l, _, _, _ ->
{ l, t, m, e -> filter(l, t ?: defaultTag, m, e) } l in levels
) }
}
fun KSJSLog(
defaultTag: String,
messageFormatter: MessageFormatter = defaultMessageFormatter,
firstLevel: LogLevel,
secondLevel: LogLevel,
vararg otherLevels: LogLevel
): KSLog = KSJSLog (defaultTag, messageFormatter, setOf(firstLevel, secondLevel, *otherLevels))
fun KSJSLog(
defaultTag: String,
messageFormatter: MessageFormatter = defaultMessageFormatter,
minLoggingLevel: LogLevel = LogLevel.VERBOSE
): KSLog = KSJSLog (defaultTag, messageFormatter) { l, _, _, _ ->
minLoggingLevel.ordinal <= l.ordinal
} }

View File

@ -2,13 +2,14 @@ package dev.inmo.kslog.common
import java.util.logging.Level import java.util.logging.Level
fun KSLog( fun KSJVMLog(
defaultTag: String,
logger: java.util.logging.Logger, logger: java.util.logging.Logger,
filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean, messageFormatter: MessageFormatter = defaultMessageFormatter,
messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean
) = KSLog { l, t, m, e -> ) = KSLog { l, t, m, e ->
if (!filter(l, t, m, e)) return@KSLog if (!filter(l, t, m, e)) return@KSLog
val text = messageFormatter(l,t,m,e) val text = messageFormatter(l,t ?: defaultTag,m,e)
logger.log( logger.log(
when(l) { when(l) {
LogLevel.VERBOSE -> Level.FINEST LogLevel.VERBOSE -> Level.FINEST
@ -23,24 +24,32 @@ fun KSLog(
) )
} }
fun KSLog( fun KSJVMLog(
defaultTag: String, defaultTag: String,
logger: java.util.logging.Logger, logger: java.util.logging.Logger,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean = { _, _, _, _ -> true } messageFormatter: MessageFormatter = defaultMessageFormatter,
levels: Iterable<LogLevel>
): KSLog { ): KSLog {
return KSLog( val levels = levels.toSet()
logger, return KSJVMLog (defaultTag, logger, messageFormatter) { l, _, _, _ ->
{ l, t, m, e -> filter(l, t ?: defaultTag, m, e) } l in levels
) { _, t, m, _ -> "${t ?: defaultTag} - $m" } }
} }
actual fun KSLog( fun KSJVMLog(
defaultTag: String, defaultTag: String,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean logger: java.util.logging.Logger,
): KSLog { messageFormatter: MessageFormatter = defaultMessageFormatter,
return KSLog( firstLevel: LogLevel,
defaultTag, secondLevel: LogLevel,
logger = java.util.logging.Logger.getAnonymousLogger(), vararg otherLevels: LogLevel
filter = { l, t, m, e -> filter(l, t, m, e) } ): KSLog = KSJVMLog (defaultTag, logger, messageFormatter, setOf(firstLevel, secondLevel, *otherLevels))
)
fun KSJVMLog(
defaultTag: String,
logger: java.util.logging.Logger,
messageFormatter: MessageFormatter = defaultMessageFormatter,
minLoggingLevel: LogLevel = LogLevel.VERBOSE
): KSLog = KSJVMLog (defaultTag, logger, messageFormatter) { l, _, _, _ ->
minLoggingLevel.ordinal <= l.ordinal
} }

View File

@ -2,13 +2,12 @@ package dev.inmo.kslog.common
import android.util.Log import android.util.Log
actual fun KSLog( fun KSAndroidLog(
defaultTag: String, defaultTag: String,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean
): KSLog { ) = KSLog { l, t, m, e ->
return KSLog { l, t, m, e ->
val tag = t ?: defaultTag
if (!filter(l, t ?: defaultTag, m, e)) return@KSLog if (!filter(l, t ?: defaultTag, m, e)) return@KSLog
val tag = t ?: defaultTag
when(l) { when(l) {
LogLevel.VERBOSE -> Log.v(tag, m, e) LogLevel.VERBOSE -> Log.v(tag, m, e)
LogLevel.INFO -> Log.i(tag, m, e) LogLevel.INFO -> Log.i(tag, m, e)
@ -17,5 +16,28 @@ actual fun KSLog(
LogLevel.ASSERT -> Log.wtf(tag, m, e) LogLevel.ASSERT -> Log.wtf(tag, m, e)
LogLevel.DEBUG -> Log.d(tag, m, e) LogLevel.DEBUG -> Log.d(tag, m, e)
} }
}
fun KSAndroidLog(
defaultTag: String,
levels: Iterable<LogLevel>
): KSLog {
val levels = levels.toSet()
return KSAndroidLog (defaultTag) { l, _, _, _ ->
l in levels
} }
} }
inline fun KSAndroidLog(
defaultTag: String,
firstLevel: LogLevel,
secondLevel: LogLevel,
vararg otherLevels: LogLevel
): KSLog = KSAndroidLog(defaultTag, setOf(firstLevel, secondLevel, *otherLevels))
fun KSAndroidLog(
defaultTag: String,
minLoggingLevel: LogLevel = LogLevel.VERBOSE
): KSLog = KSAndroidLog (defaultTag) { l, _, _, _ ->
minLoggingLevel.ordinal <= l.ordinal
}