Merge pull request #6 from InsanusMokrassar/0.1.0

0.1.0
This commit is contained in:
InsanusMokrassar 2022-06-08 18:25:06 +06:00 committed by GitHub
commit 03b18d005f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 107 additions and 45 deletions

View File

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

View File

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

View File

@ -37,10 +37,14 @@ class CallbackKSLog(
}
expect fun KSLog(
fun KSLog(
defaultTag: String,
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(
defaultTag: String,
@ -52,7 +56,7 @@ fun KSLog(
}
}
inline fun KSLog(
fun KSLog(
defaultTag: String,
firstLevel: 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
fun KSLog(
messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String,
fun KSJSLog(
defaultTag: String,
messageFormatter: MessageFormatter = defaultMessageFormatter,
filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean
) = KSLog { l, t, m, e ->
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 {
arrayOf(text, e)
} ?: arrayOf(text)
@ -19,12 +20,29 @@ fun KSLog(
}
}
actual fun KSLog(
fun KSJSLog(
defaultTag: String,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean
messageFormatter: MessageFormatter = defaultMessageFormatter,
levels: Iterable<LogLevel>
): KSLog {
return KSLog(
{ l, t, m, _ -> "[$l] ${t ?: defaultTag} - $m" },
{ l, t, m, e -> filter(l, t ?: defaultTag, m, e) }
)
val levels = levels.toSet()
return KSJSLog (defaultTag, messageFormatter) { l, _, _, _ ->
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
fun KSLog(
fun KSJVMLog(
defaultTag: String,
logger: java.util.logging.Logger,
filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean,
messageFormatter: (l: LogLevel, t: String?, m: String, Throwable?) -> String
messageFormatter: MessageFormatter = defaultMessageFormatter,
filter: (l: LogLevel, t: String?, m: String, Throwable?) -> Boolean
) = KSLog { l, t, m, e ->
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(
when(l) {
LogLevel.VERBOSE -> Level.FINEST
@ -23,24 +24,32 @@ fun KSLog(
)
}
fun KSLog(
fun KSJVMLog(
defaultTag: String,
logger: java.util.logging.Logger,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean = { _, _, _, _ -> true }
messageFormatter: MessageFormatter = defaultMessageFormatter,
levels: Iterable<LogLevel>
): KSLog {
return KSLog(
logger,
{ l, t, m, e -> filter(l, t ?: defaultTag, m, e) }
) { _, t, m, _ -> "${t ?: defaultTag} - $m" }
val levels = levels.toSet()
return KSJVMLog (defaultTag, logger, messageFormatter) { l, _, _, _ ->
l in levels
}
}
actual fun KSLog(
fun KSJVMLog(
defaultTag: String,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean
): KSLog {
return KSLog(
defaultTag,
logger = java.util.logging.Logger.getAnonymousLogger(),
filter = { l, t, m, e -> filter(l, t, m, e) }
)
logger: java.util.logging.Logger,
messageFormatter: MessageFormatter = defaultMessageFormatter,
firstLevel: LogLevel,
secondLevel: LogLevel,
vararg otherLevels: LogLevel
): 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
actual fun KSLog(
fun KSAndroidLog(
defaultTag: String,
filter: (l: LogLevel, t: String, m: String, Throwable?) -> Boolean
): KSLog {
return KSLog { l, t, m, e ->
val tag = t ?: defaultTag
) = KSLog { l, t, m, e ->
if (!filter(l, t ?: defaultTag, m, e)) return@KSLog
val tag = t ?: defaultTag
when(l) {
LogLevel.VERBOSE -> Log.v(tag, m, e)
LogLevel.INFO -> Log.i(tag, m, e)
@ -18,4 +17,27 @@ actual fun KSLog(
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
}