Merge pull request #14 from InsanusMokrassar/0.4.0

0.4.0
This commit is contained in:
InsanusMokrassar 2022-07-30 15:16:21 +06:00 committed by GitHub
commit 923add4550
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 274 additions and 140 deletions

View File

@ -1,5 +1,15 @@
# Changelog
## 0.4.0
**THIS UPDATE CONTAINS BREAKING CHANGES**
* **BREAKING CHANGE** Any `KSLog` now accept any object. It was made to allow to work with structured logging
* New factory for `TagLogger`: fun `taggedLogger`
* New logger has been added: `TypedLogger`
* New logger has been added: `FilterLogger`
* **BREAKING CHANGE** Logger `DefaultKSLog` lost its constructor with `filter`. Currently, it is still available as factory function
## 0.3.2
* Add duplication functions for all extensions which allow to send log without tag but with exception and message builder

View File

@ -9,5 +9,5 @@ android.enableJetifier=true
# Project data
group=dev.inmo
version=0.3.2
android_code_version=9
version=0.4.0
android_code_version=10

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -4,3 +4,5 @@ val Any.logTag
get() = this::class.simpleName ?: error("Unable to retrieve log tag")
val Any.logger: KSLog
get() = TagLogger(logTag)
fun taggedLogger(tagBase: Any): KSLog = tagBase.logger

View File

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

View File

@ -1,40 +1,45 @@
package dev.inmo.kslog.common
import dev.inmo.kslog.common.filter.filtered
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
private val logging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = defaultLogging
) : KSLog {
override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) {
override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) {
val tag = tag ?: defaultTag
if (filter(level, tag, throwable)) {
val text = messageFormatter(level, tag, message, throwable)
logging(level, tag, text, throwable)
}
val text = messageFormatter(level, tag, message, throwable)
logging(level, tag, text, throwable)
}
override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> String) {
override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> Any) {
val tag = tag ?: defaultTag
if (filter(level, tag, throwable)) {
val text = messageFormatter(level, tag, messageBuilder(), throwable)
logging(level, tag, text, 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
messageBuilder: suspend () -> Any
) {
val tag = tag ?: defaultTag
if (filter(level, tag, throwable)) {
val text = messageFormatter(level, tag, messageBuilder(), throwable)
logging(level, tag, text, throwable)
}
val text = messageFormatter(level, tag, messageBuilder(), throwable)
logging(level, tag, text, throwable)
}
}
@Deprecated("Filtering should be replaced with FilterKSLog")
fun DefaultKSLog(
defaultTag: String,
filter: MessageFilter = { _, _, _ -> true },
messageFormatter: MessageFormatter = defaultMessageFormatter,
logging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit = defaultLogging
) = DefaultKSLog(
defaultTag, messageFormatter, logging
).filtered(filter)

View File

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

View File

@ -1,5 +1,7 @@
package dev.inmo.kslog.common
import dev.inmo.kslog.common.filter.filtered
enum class LogLevel {
DEBUG,
@ -11,20 +13,22 @@ 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?, message: Any, throwable: Throwable?)
fun performLog(level: LogLevel, message: Any, throwable: Throwable?) = performLog(level, null, message, throwable)
fun performLog(
level: LogLevel,
tag: String?,
throwable: Throwable?,
messageBuilder: () -> String
messageBuilder: () -> Any
) = performLog(level, tag, messageBuilder(), throwable)
suspend fun performLogS(
level: LogLevel,
tag: String?,
throwable: Throwable?,
messageBuilder: suspend () -> String
messageBuilder: suspend () -> Any
) = performLog(level, tag, messageBuilder(), throwable)
companion object : KSLog {
private var defaultLogger: KSLog? = null
var default: KSLog
@ -36,31 +40,40 @@ interface KSLog {
set(value) {
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?, message: Any, throwable: Throwable?) = default.performLog(level, tag, message, throwable)
override fun performLog(level: LogLevel, message: Any, throwable: Throwable?) = default.performLog(level, message, throwable)
override fun performLog(
level: LogLevel,
tag: String?,
throwable: Throwable?,
messageBuilder: () -> String
messageBuilder: () -> Any
) = default.performLog(level, tag, throwable, messageBuilder)
override suspend fun performLogS(
level: LogLevel,
tag: String?,
throwable: Throwable?,
messageBuilder: suspend () -> String
messageBuilder: suspend () -> Any
) = default.performLogS(level, tag, throwable, messageBuilder)
}
}
operator fun KSLog.invoke(performLogCallback: (level: LogLevel, tag: String?, message: String, 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: String, throwable: Throwable?) -> Unit
internal expect val defaultLogging: (level: LogLevel, tag: String, message: Any, throwable: Throwable?) -> Unit
fun KSLog(
defaultTag: String,
filter: MessageFilter = { _, _, _ -> true },
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog = DefaultKSLog(
defaultTag,
messageFormatter
)
@Deprecated("Filtering should be replaced with FilterKSLog")
fun KSLog(
defaultTag: String,
filter: MessageFilter,
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog = DefaultKSLog(
defaultTag,
@ -74,9 +87,9 @@ fun KSLog(
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog {
val levels = levels.toSet()
return KSLog (defaultTag, { l, _, _ ->
return KSLog (defaultTag, messageFormatter).filtered { l, _, _ ->
l in levels
}, messageFormatter)
}
}
fun KSLog(
@ -93,8 +106,7 @@ fun KSLog(
messageFormatter: MessageFormatter = defaultMessageFormatter,
): KSLog = KSLog (
defaultTag,
{ l, _, _ ->
minLoggingLevel.ordinal <= l.ordinal
},
messageFormatter
)
).filtered { l, _, _ ->
minLoggingLevel.ordinal <= l.ordinal
}

View File

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

View File

@ -4,7 +4,7 @@ import kotlin.jvm.JvmInline
@JvmInline
value class TagLogger(val tag: String) : KSLog {
override fun performLog(level: LogLevel, tag: String?, message: String, throwable: Throwable?) {
override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) {
KSLog.performLog(level, tag ?: this.tag, message, throwable)
}
}

View File

@ -0,0 +1,32 @@
package dev.inmo.kslog.common.filter
import dev.inmo.kslog.common.*
class FilterKSLog(
private val fallbackLogger: KSLog,
private val messageFilter: MessageFilter
) : KSLog {
override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) {
if (messageFilter(level, tag, throwable)) {
fallbackLogger.performLog(level, tag, message, throwable)
}
}
override fun performLog(level: LogLevel, message: Any, throwable: Throwable?) {
if (messageFilter(level, null, throwable)) {
fallbackLogger.performLog(level, message, throwable)
}
}
override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> Any) {
if (messageFilter(level, tag, throwable)) {
fallbackLogger.performLog(level, tag, throwable, messageBuilder)
}
}
override suspend fun performLogS(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: suspend () -> Any) {
if (messageFilter(level, tag, throwable)) {
fallbackLogger.performLogS(level, tag, throwable, messageBuilder)
}
}
}

View File

@ -0,0 +1,8 @@
package dev.inmo.kslog.common.filter
import dev.inmo.kslog.common.KSLog
import dev.inmo.kslog.common.MessageFilter
fun KSLog.filtered(
filter: MessageFilter
) = FilterKSLog(this, filter)

View File

@ -0,0 +1,23 @@
package dev.inmo.kslog.common.typed
import dev.inmo.kslog.common.*
import kotlin.reflect.KClass
class TypedKSLog(
private val typedLoggers: Map<KClass<*>?, KSLog>
) : KSLog {
override fun performLog(level: LogLevel, tag: String?, message: Any, throwable: Throwable?) {
(typedLoggers[message::class] ?: typedLoggers[null]) ?.performLog(level, tag, message, throwable)
}
override fun performLog(level: LogLevel, message: Any, throwable: Throwable?) {
(typedLoggers[message::class] ?: typedLoggers[null]) ?.performLog(level, message, throwable)
}
override fun performLog(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: () -> Any) {
val message = messageBuilder()
(typedLoggers[message::class] ?: typedLoggers[null]) ?.performLog(level, tag, message, throwable)
}
override suspend fun performLogS(level: LogLevel, tag: String?, throwable: Throwable?, messageBuilder: suspend () -> Any) {
val message = messageBuilder()
(typedLoggers[message::class] ?: typedLoggers[null]) ?.performLog(level, tag, message, throwable)
}
}

View File

@ -0,0 +1,28 @@
package dev.inmo.kslog.common.typed
import dev.inmo.kslog.common.*
import kotlin.reflect.KClass
class TypedKSLogBuilder(
private val preset: Map<KClass<*>?, KSLog> = emptyMap()
) {
private val loggers = mutableMapOf<KClass<*>?, KSLog>().apply {
putAll(preset)
}
fun on(kClass: KClass<*>?, with: KSLog) = loggers.put(kClass, with)
fun on(kClass: KClass<*>?, with: SimpleKSLogCallback) = loggers.put(kClass, CallbackKSLog(with))
fun default(with: KSLog) = on(null, with)
fun default(with: SimpleKSLogCallback) = on(null, with)
inline fun <reified T: Any> on(with: KSLog) = on(T::class, with)
inline fun <reified T: Any> on(noinline with: SimpleKSLogCallback) = on(T::class, with)
fun build() = TypedKSLog(loggers.toMap())
}
inline fun buildTypedLogger(
preset: Map<KClass<*>?, KSLog> = emptyMap(),
block: TypedKSLogBuilder.() -> Unit
) = TypedKSLogBuilder(preset).apply(block).build()

View File

@ -1,6 +1,6 @@
package dev.inmo.kslog.common
internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, 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 args = e ?.let {
arrayOf(m, e)
} ?: arrayOf(m)

View File

@ -1,5 +1,6 @@
package dev.inmo.kslog.common
import dev.inmo.kslog.common.filter.filtered
import java.util.logging.Level
import java.util.logging.Logger
@ -20,18 +21,28 @@ private fun Logger.doLog(
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)
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(
defaultTag: String,
logger: Logger,
filter: MessageFilter,
messageFormatter: MessageFormatter = defaultMessageFormatter
) = KSLog { l, t, m, e ->
if (!filter(l, t, e)) return@KSLog
val text = messageFormatter(l,t ?: defaultTag,m.toString(),e)
logger.doLog(l, t ?: defaultTag, text, e)
}
fun KSLog(
defaultTag: String,
logger: Logger,
filter: MessageFilter = { _, _, _ -> true },
messageFormatter: MessageFormatter = defaultMessageFormatter
) = KSLog { l, t, m, e ->
if (!filter(l, t, e)) return@KSLog
val text = messageFormatter(l,t ?: defaultTag,m,e)
val text = messageFormatter(l,t ?: defaultTag,m.toString(),e)
logger.doLog(l, t ?: defaultTag, text, e)
}
@ -42,7 +53,7 @@ fun KSLog(
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog {
val levels = levels.toSet()
return KSLog (defaultTag, logger, { l, _, _ -> l in levels }, messageFormatter)
return KSLog (defaultTag, logger, messageFormatter).filtered { l, _, _ -> l in levels }
}
fun KSLog(
@ -59,4 +70,4 @@ fun KSLog(
logger: Logger,
minLoggingLevel: LogLevel = LogLevel.values().first(),
messageFormatter: MessageFormatter = defaultMessageFormatter
): KSLog = KSLog (defaultTag, logger, { l, _, _ -> minLoggingLevel.ordinal <= l.ordinal }, messageFormatter)
): KSLog = KSLog (defaultTag, logger, messageFormatter).filtered { l, _, _ -> minLoggingLevel.ordinal <= l.ordinal }

View File

@ -3,13 +3,14 @@ package dev.inmo.kslog.common
import android.util.Log
internal actual val defaultLogging: (level: LogLevel, tag: String, message: String, 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()
when(l) {
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)
LogLevel.DEBUG -> Log.d(t, messageString, e)
LogLevel.VERBOSE -> Log.v(t, messageString, e)
LogLevel.INFO -> Log.i(t, messageString, e)
LogLevel.WARNING -> Log.w(t, messageString, e)
LogLevel.ERROR -> Log.e(t, messageString, e)
LogLevel.ASSERT -> Log.wtf(t, messageString, e)
}
}