mirror of
https://github.com/InsanusMokrassar/KSLog.git
synced 2024-12-22 22:17:15 +00:00
Logger created
This commit is contained in:
parent
45772e7376
commit
879587ada7
@ -8,6 +8,6 @@ android.enableJetifier=true
|
|||||||
|
|
||||||
# Project data
|
# Project data
|
||||||
|
|
||||||
group=project_group
|
group=dev.akkihi
|
||||||
version=0.0.1
|
version=0.0.1
|
||||||
android_code_version=1
|
android_code_version=1
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
package project_group
|
|
||||||
|
|
||||||
class Library {
|
|
||||||
fun someLibraryMethod(): Boolean {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
@ -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'")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
<manifest package="project_group.lib"/>
|
|
40
logger/src/commonMain/kotlin/Logger.kt
Normal file
40
logger/src/commonMain/kotlin/Logger.kt
Normal 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
|
||||||
|
}
|
28
logger/src/jsMain/kotlin/ActualLogger.kt
Normal file
28
logger/src/jsMain/kotlin/ActualLogger.kt
Normal 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) }
|
||||||
|
)
|
||||||
|
}
|
26
logger/src/jvmMain/kotlin/ActualLogger.kt
Normal file
26
logger/src/jvmMain/kotlin/ActualLogger.kt
Normal 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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
1
logger/src/main/AndroidManifest.xml
Normal file
1
logger/src/main/AndroidManifest.xml
Normal file
@ -0,0 +1 @@
|
|||||||
|
<manifest package="dev.akkihi.lib"/>
|
21
logger/src/main/kotlin/ActualLogger.kt
Normal file
21
logger/src/main/kotlin/ActualLogger.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
rootProject.name = 'project_name'
|
rootProject.name = 'truth_simple_kmp_logger'
|
||||||
|
|
||||||
String[] includes = [
|
String[] includes = [
|
||||||
":lib"
|
":logger"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user