Merge pull request #11 from InsanusMokrassar/0.3.0

0.3.0
This commit is contained in:
InsanusMokrassar 2022-06-09 16:37:19 +06:00 committed by GitHub
commit caf6a2d05c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 167 additions and 86 deletions

View File

@ -1,5 +1,11 @@
# Changelog
## 0.3.0
* **BREAKING CHANGE**: Filters will not accept message since this update
* New `KSLog` realization `DefaultKSLog`
* Now platforms realize their default loggers callbacks instead of `KSLog` factories
## 0.2.0
* Full rewrite of API

View File

@ -9,5 +9,5 @@ android.enableJetifier=true
# Project data
group=dev.inmo
version=0.2.0
android_code_version=6
version=0.3.0
android_code_version=7

View File

@ -0,0 +1,7 @@
package dev.inmo.kslog.common
class CallbackKSLog(
private val performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit
) : KSLog {
override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = performLogCallback(level, tag, message, throwable)
}

View File

@ -0,0 +1,40 @@
package dev.inmo.kslog.common
class DefaultKSLog(
private val defaultTag: String,
val filter: MessageFilter = { _, _, _ -> true },
private val messageFormatter: MessageFormatter = defaultMessageFormatter,
private val logging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = defaultLogging
) : KSLog {
override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) {
val tag = tag ?: defaultTag
if (filter(level, tag, throwable)) {
val text = messageFormatter(level, tag, message, throwable)
logging(level, tag, text, throwable)
}
}
override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> String) {
val tag = tag ?: defaultTag
if (filter(level, tag, throwable)) {
val text = messageFormatter(level, tag, messageBuilder(), throwable)
logging(level, tag, text, throwable)
}
}
override suspend fun performLogS(
level: LogLevel,
tag: String?,
throwable: Throwable?,
messageBuilder: suspend () -> String
) {
val tag = tag ?: defaultTag
if (filter(level, tag, throwable)) {
val text = messageFormatter(level, tag, messageBuilder(), throwable)
logging(level, tag, text, throwable)
}
}
}

View File

@ -2,72 +2,86 @@
package dev.inmo.kslog.common
inline fun KSLog.log(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = performLog(level, tag, messageBuilder(), e)
inline fun KSLog.log(level: LogLevel, message: String, e: Throwable? = null) = log(level, null, e) { message }
inline fun KSLog.log(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, e) { message }
inline fun KSLog.log(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = performLog(level, tag, e, messageBuilder)
suspend inline fun KSLog.logS(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = performLogS(level, tag, e, messageBuilder)
inline fun KSLog.log(level: LogLevel, message: String, e: Throwable? = null) = performLog(level, message, e)
inline fun KSLog.log(level: LogLevel, tag: String, message: String, e: Throwable? = null) = performLog(level, tag, message, e)
inline fun KSLog.verbose(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.VERBOSE, tag, e, messageBuilder)
inline fun KSLog.debug(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.DEBUG, tag, e, messageBuilder)
suspend inline fun KSLog.debugS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.DEBUG, tag, e, messageBuilder)
inline fun KSLog.debug(message: String, e: Throwable? = null) = log(LogLevel.DEBUG, message, e)
inline fun KSLog.debug(tag: String, message: String, e: Throwable? = null) = log(LogLevel.DEBUG, tag, message, e)
inline fun KSLog.verbose(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.VERBOSE, tag, e, messageBuilder)
suspend inline fun KSLog.verboseS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.VERBOSE, tag, e, messageBuilder)
inline fun KSLog.verbose(message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, message, e)
inline fun KSLog.verbose(tag: String, message: String, e: Throwable? = null) = log(LogLevel.VERBOSE, tag, message, e)
inline fun KSLog.info(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.INFO, tag, e, messageBuilder)
inline fun KSLog.info(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.INFO, tag, e, messageBuilder)
suspend inline fun KSLog.infoS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.INFO, tag, e, messageBuilder)
inline fun KSLog.info(message: String, e: Throwable? = null) = log(LogLevel.INFO, message, e)
inline fun KSLog.info(tag: String, message: String, e: Throwable? = null) = log(LogLevel.INFO, tag, message, e)
inline fun KSLog.warning(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.WARNING, tag, e, messageBuilder)
inline fun KSLog.warning(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.WARNING, tag, e, messageBuilder)
suspend inline fun KSLog.warningS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.WARNING, tag, e, messageBuilder)
inline fun KSLog.warning(message: String, e: Throwable? = null) = log(LogLevel.WARNING, message, e)
inline fun KSLog.warning(tag: String, message: String, e: Throwable? = null) = log(LogLevel.WARNING, tag, message, e)
inline fun KSLog.error(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ERROR, tag, e, messageBuilder)
inline fun KSLog.error(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.ERROR, tag, e, messageBuilder)
suspend inline fun KSLog.errorS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.ERROR, tag, e, messageBuilder)
inline fun KSLog.error(message: String, e: Throwable? = null) = log(LogLevel.ERROR, message, e)
inline fun KSLog.error(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ERROR, tag, message, e)
inline fun KSLog.assert(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.ASSERT, tag, e, messageBuilder)
inline fun KSLog.assert(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(LogLevel.ASSERT, tag, e, messageBuilder)
suspend inline fun KSLog.assertS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(LogLevel.ASSERT, tag, e, messageBuilder)
inline fun KSLog.assert(message: String, e: Throwable? = null) = log(LogLevel.ASSERT, message, e)
inline fun KSLog.assert(tag: String, message: String, e: Throwable? = null) = log(LogLevel.ASSERT, tag, message, e)
inline fun KSLog.debug(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(LogLevel.DEBUG, tag, e, messageBuilder)
inline fun KSLog.debug(message: String, e: Throwable? = null) = log(LogLevel.DEBUG, message, e)
inline fun KSLog.debug(tag: String, message: String, e: Throwable? = null) = log(LogLevel.DEBUG, tag, message, e)
// ----- Short names
inline fun KSLog.l(level: LogLevel, tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = log(level, tag, e, messageBuilder)
inline fun KSLog.l(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = log(level, tag, e, messageBuilder)
suspend inline fun KSLog.lS(level: LogLevel, tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = logS(level, tag, e, messageBuilder)
inline fun KSLog.l(level: LogLevel, message: String, e: Throwable? = null) = log(level, message, e)
inline fun KSLog.l(level: LogLevel, tag: String, message: String, e: Throwable? = null) = log(level, tag, message, e)
inline fun KSLog.v(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = verbose(tag, e, messageBuilder)
inline fun KSLog.d(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = debug(tag, e, messageBuilder)
suspend inline fun KSLog.dS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = debugS(tag, e, messageBuilder)
inline fun KSLog.d(message: String, e: Throwable? = null) = debug(message, e)
inline fun KSLog.d(tag: String, message: String, e: Throwable? = null) = debug(tag, message, e)
inline fun KSLog.v(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = verbose(tag, e, messageBuilder)
suspend inline fun KSLog.vS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = verboseS(tag, e, messageBuilder)
inline fun KSLog.v(message: String, e: Throwable? = null) = verbose(message, e)
inline fun KSLog.v(tag: String, message: String, e: Throwable? = null) = verbose(tag, message, e)
inline fun KSLog.i(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = info(tag, e, messageBuilder)
inline fun KSLog.i(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = info(tag, e, messageBuilder)
suspend inline fun KSLog.iS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = infoS(tag, e, messageBuilder)
inline fun KSLog.i(message: String, e: Throwable? = null) = info(message, e)
inline fun KSLog.i(tag: String, message: String, e: Throwable? = null) = info(tag, message, e)
inline fun KSLog.w(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = warning(tag, e, messageBuilder)
inline fun KSLog.w(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = warning(tag, e, messageBuilder)
suspend inline fun KSLog.wS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = warningS(tag, e, messageBuilder)
inline fun KSLog.w(message: String, e: Throwable? = null) = warning(message, e)
inline fun KSLog.w(tag: String, message: String, e: Throwable? = null) = warning(tag, message, e)
inline fun KSLog.e(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = error(tag, e, messageBuilder)
inline fun KSLog.e(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = error(tag, e, messageBuilder)
suspend inline fun KSLog.eS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = errorS(tag, e, messageBuilder)
inline fun KSLog.e(message: String, e: Throwable? = null) = error(message, e)
inline fun KSLog.e(tag: String, message: String, e: Throwable? = null) = error(tag, message, e)
inline fun KSLog.wtf(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = assert(tag, e, messageBuilder)
inline fun KSLog.wtf(tag: String? = null, e: Throwable? = null, noinline messageBuilder: () -> String) = assert(tag, e, messageBuilder)
suspend inline fun KSLog.wtfS(tag: String? = null, e: Throwable? = null, noinline messageBuilder: suspend () -> String) = assertS(tag, e, messageBuilder)
inline fun KSLog.wtf(message: String, e: Throwable? = null) = assert(message, e)
inline fun KSLog.wtf(tag: String, message: String, e: Throwable? = null) = assert(tag, message, e)
inline fun KSLog.d(tag: String? = null, e: Throwable? = null, messageBuilder: () -> String) = debug(tag, e, messageBuilder)
inline fun KSLog.d(message: String, e: Throwable? = null) = debug(message, e)
inline fun KSLog.d(tag: String, message: String, e: Throwable? = null) = debug(tag, message, e)

View File

@ -12,6 +12,19 @@ enum class LogLevel {
interface KSLog {
fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?)
fun performLog(level: LogLevel, message: String, throwable: Throwable?) = performLog(level, null, message, throwable)
fun performLog(
level: LogLevel,
tag: String?,
throwable: Throwable?,
messageBuilder: () -> String
) = performLog(level, tag, messageBuilder(), throwable)
suspend fun performLogS(
level: LogLevel,
tag: String?,
throwable: Throwable?,
messageBuilder: suspend () -> String
) = performLog(level, tag, messageBuilder(), throwable)
companion object : KSLog {
private var defaultLogger: KSLog? = null
var default: KSLog
@ -24,23 +37,36 @@ interface KSLog {
defaultLogger = value
}
override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = default.performLog(level, tag, message, throwable)
override fun performLog(level: LogLevel, message: String, throwable: Throwable?) = default.performLog(level, message, throwable)
override fun performLog(
level: LogLevel,
tag: String?,
throwable: Throwable?,
messageBuilder: () -> String
) = default.performLog(level, tag, throwable, messageBuilder)
override suspend fun performLogS(
level: LogLevel,
tag: String?,
throwable: Throwable?,
messageBuilder: suspend () -> String
) = default.performLogS(level, tag, throwable, messageBuilder)
}
}
operator fun KSLog.invoke(performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit) = CallbackKSLog(performLogCallback)
class CallbackKSLog(
private val performLogCallback: (level: LogLevel, tag: String?, message: String, throwable: Throwable?) -> Unit
) : KSLog {
override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) = performLogCallback(level, tag, message, throwable)
}
internal expect val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit
expect fun KSLog(
fun KSLog(
defaultTag: String,
filter: MessageFilter = { _, _, _, _ -> true },
filter: MessageFilter = { _, _, _ -> true },
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog
): KSLog = DefaultKSLog(
defaultTag,
filter,
messageFormatter
)
fun KSLog(
defaultTag: String,
@ -48,7 +74,7 @@ fun KSLog(
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog {
val levels = levels.toSet()
return KSLog (defaultTag, { l, _, _, _ ->
return KSLog (defaultTag, { l, _, _ ->
l in levels
}, messageFormatter)
}
@ -67,7 +93,7 @@ fun KSLog(
messageFormatter: MessageFormatter = defaultMessageFormatter,
): KSLog = KSLog (
defaultTag,
{ l, _, _, _ ->
{ l, _, _ ->
minLoggingLevel.ordinal <= l.ordinal
},
messageFormatter

View File

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

View File

@ -1,15 +1,9 @@
package dev.inmo.kslog.common
actual fun KSLog(
defaultTag: String,
filter: MessageFilter,
messageFormatter: MessageFormatter
): KSLog = KSLog { l, t, m, e ->
if (!filter(l, t, m, e)) return@KSLog
val text = messageFormatter(l, t?:defaultTag, m, e)
internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = { l, t, m, e ->
val args = e ?.let {
arrayOf(text, e)
} ?: arrayOf(text)
arrayOf(m, e)
} ?: arrayOf(m)
when (l) {
LogLevel.DEBUG -> console.log(*args)
LogLevel.VERBOSE,

View File

@ -3,26 +3,36 @@ package dev.inmo.kslog.common
import java.util.logging.Level
import java.util.logging.Logger
private val defaultKSLogLogger by lazy {
Logger.getLogger("KSLog")
}
private fun Logger.doLog(
l: LogLevel, t: String, m: String, e: Throwable?
) = log(
when(l) {
LogLevel.DEBUG -> Level.FINEST
LogLevel.VERBOSE -> Level.FINE
LogLevel.INFO -> Level.INFO
LogLevel.WARNING -> Level.WARNING
LogLevel.ERROR -> Level.SEVERE
LogLevel.ASSERT -> Level.SEVERE
},
m,
e
)
internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = { l, t, m, e ->
defaultKSLogLogger.doLog(l, t, m, e)
}
fun KSLog(
defaultTag: String,
logger: Logger,
filter: MessageFilter = { _, _, _, _ -> true },
filter: MessageFilter = { _, _, _ -> true },
messageFormatter: MessageFormatter = defaultMessageFormatter
) = KSLog { l, t, m, e ->
if (!filter(l, t, m, e)) return@KSLog
if (!filter(l, t, e)) return@KSLog
val text = messageFormatter(l,t ?: defaultTag,m,e)
logger.log(
when(l) {
LogLevel.DEBUG -> Level.FINEST
LogLevel.VERBOSE -> Level.FINE
LogLevel.INFO -> Level.INFO
LogLevel.WARNING -> Level.WARNING
LogLevel.ERROR -> Level.SEVERE
LogLevel.ASSERT -> Level.SEVERE
},
text,
e
)
logger.doLog(l, t ?: defaultTag, text, e)
}
fun KSLog(
@ -32,9 +42,7 @@ fun KSLog(
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog {
val levels = levels.toSet()
return KSLog (defaultTag, logger, { l, _, _, _ ->
l in levels
}, messageFormatter)
return KSLog (defaultTag, logger, { l, _, _ -> l in levels }, messageFormatter)
}
fun KSLog(
@ -51,12 +59,4 @@ fun KSLog(
logger: Logger,
minLoggingLevel: LogLevel = LogLevel.values().first(),
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog = KSLog (defaultTag, logger, { l, _, _, _ ->
minLoggingLevel.ordinal <= l.ordinal
}, messageFormatter)
actual fun KSLog(
defaultTag: String,
filter: MessageFilter,
messageFormatter: MessageFormatter
): KSLog = KSLog(defaultTag, Logger.getLogger("KSLog"), filter, messageFormatter)
): KSLog = KSLog (defaultTag, logger, { l, _, _ -> minLoggingLevel.ordinal <= l.ordinal }, messageFormatter)

View File

@ -2,20 +2,14 @@ package dev.inmo.kslog.common
import android.util.Log
actual fun KSLog(
defaultTag: String,
filter: MessageFilter,
messageFormatter: MessageFormatter
): KSLog = KSLog { l, t, m, e ->
if (!filter(l, t ?: defaultTag, m, e)) return@KSLog
val tag = t ?: defaultTag
val text = messageFormatter(l, tag, m, e)
internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, throwable: Throwable?) -> Unit = { l, t, m, e ->
when(l) {
LogLevel.DEBUG -> Log.d(tag, text, e)
LogLevel.VERBOSE -> Log.v(tag, text, e)
LogLevel.INFO -> Log.i(tag, text, e)
LogLevel.WARNING -> Log.w(tag, text, e)
LogLevel.ERROR -> Log.e(tag, text, e)
LogLevel.ASSERT -> Log.wtf(tag, text, e)
LogLevel.DEBUG -> Log.d(t, m, e)
LogLevel.VERBOSE -> Log.v(t, m, e)
LogLevel.INFO -> Log.i(t, m, e)
LogLevel.WARNING -> Log.w(t, m, e)
LogLevel.ERROR -> Log.e(t, m, e)
LogLevel.ASSERT -> Log.wtf(t, m, e)
}
}