Logger created

This commit is contained in:
akkkihi 2022-06-07 17:26:11 +04:00
parent 45772e7376
commit 879587ada7
11 changed files with 119 additions and 25 deletions

View File

@ -8,6 +8,6 @@ android.enableJetifier=true
# Project data
group=project_group
group=dev.akkihi
version=0.0.1
android_code_version=1

View File

@ -1,7 +0,0 @@
package project_group
class Library {
fun someLibraryMethod(): Boolean {
return true
}
}

View File

@ -1,14 +0,0 @@
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package project_group
import kotlin.test.Test
import kotlin.test.assertTrue
class LibraryTest {
@Test fun testSomeLibraryMethod() {
val classUnderTest = Library()
assertTrue(classUnderTest.someLibraryMethod(), "someLibraryMethod should return 'true'")
}
}

View File

@ -1 +0,0 @@
<manifest package="project_group.lib"/>

View File

@ -0,0 +1,40 @@
package truth.simple.kmp.logger.common
enum class LogLevel {
VERBOSE,
INFO,
WARNING,
ERROR,
ASSERT,
DEBUG
}
interface Logger {
fun log (level: LogLevel, message: String, tag: String? = null, throwable: Throwable? = null)
companion object : Logger {
var DEFAULT: Logger = Logger("app")
override fun log(level: LogLevel, message: String, tag: String?, throwable: Throwable?) = DEFAULT.log(level, message, tag, throwable)
operator fun invoke (log: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit) = CallbackLogger(log)
}
}
class CallbackLogger(
private val logger: (level: LogLevel, message: String, tag: String?, throwable: Throwable?) -> Unit
) : Logger {
override fun log(level: LogLevel, message: String, tag: String?, throwable: Throwable?) = logger(level, message, tag, throwable)
}
expect fun Logger(
defaultTag: String,
filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean
) : Logger
fun Logger(
defaultTag: String,
levels: Set<LogLevel> = LogLevel.values().toSet()
): Logger = Logger (defaultTag) { l, _, _, _ ->
l in levels
}

View File

@ -0,0 +1,28 @@
package truth.simple.kmp.logger.common
fun Logger(
messageFormatter: (l: LogLevel, m: String, t: String?, Throwable?) -> String,
filter: (l: LogLevel, m: String, t: String?, Throwable?) -> Boolean
) = Logger { l, m, t, e ->
if (!filter(l, m, t, e)) return@Logger
val text = messageFormatter(l,m,t,e)
when (l) {
LogLevel.VERBOSE,
LogLevel.INFO -> console.info(text, e)
LogLevel.WARNING -> console.warn(text, e)
LogLevel.ERROR,
LogLevel.ASSERT -> console.error(text, e)
LogLevel.DEBUG -> console.log(text, e)
}
}
actual fun Logger(
defaultTag: String,
filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean
): Logger {
return Logger(
{ l, m, t, e -> "[$l] ${t ?: defaultTag} - $m" },
{ l, m, t, e -> filter(l, m, t ?: defaultTag, e) }
)
}

View File

@ -0,0 +1,26 @@
package truth.simple.kmp.logger.common
import java.util.logging.Level
actual fun Logger(
defaultTag: String,
filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean
): Logger {
val logger = java.util.logging.Logger.getLogger(defaultTag)
return Logger { l, m, t, e ->
if (!filter(l, m, t ?: defaultTag, e)) return@Logger
val currentLogger = t ?.let { java.util.logging.Logger.getLogger(t) } ?: logger
currentLogger.log(
when(l) {
LogLevel.VERBOSE -> Level.FINEST
LogLevel.INFO -> Level.INFO
LogLevel.WARNING -> Level.WARNING
LogLevel.ERROR -> Level.SEVERE
LogLevel.ASSERT -> Level.OFF
LogLevel.DEBUG -> Level.ALL
},
m,
e
)
}
}

View File

@ -0,0 +1 @@
<manifest package="dev.akkihi.lib"/>

View File

@ -0,0 +1,21 @@
package truth.simple.kmp.logger.common
import android.util.Log
actual fun Logger(
defaultTag: String,
filter: (l: LogLevel, m: String, t: String, Throwable?) -> Boolean
): Logger {
return Logger { l, m, t, e ->
val tag = t ?: defaultTag
if (!filter(l, m, t ?: defaultTag, e)) return@Logger
when(l) {
LogLevel.VERBOSE -> Log.v(tag, m, e)
LogLevel.INFO -> Log.i(tag, m, e)
LogLevel.WARNING -> Log.w(tag, m, e)
LogLevel.ERROR -> Log.e(tag, m, e)
LogLevel.ASSERT -> Log.wtf(tag, m, e)
LogLevel.DEBUG -> Log.d(tag, m, e)
}
}
}

View File

@ -1,7 +1,7 @@
rootProject.name = 'project_name'
rootProject.name = 'truth_simple_kmp_logger'
String[] includes = [
":lib"
":logger"
]