From a1b189b683d511352213377092853ce2730a19dc Mon Sep 17 00:00:00 2001 From: Cedrick Cooke Date: Wed, 4 Sep 2024 19:12:11 -0700 Subject: [PATCH] Add khronicle logger --- gradle/libs.versions.toml | 1 + logging-khronicle/build.gradle.kts | 24 ++++++++++++++++++ logging-khronicle/src/jsMain/kotlin/Event.kt | 6 +++++ .../src/jsMain/kotlin/KhronicleLogger.kt | 25 +++++++++++++++++++ settings.gradle.kts | 1 + 5 files changed, 57 insertions(+) create mode 100644 logging-khronicle/build.gradle.kts create mode 100644 logging-khronicle/src/jsMain/kotlin/Event.kt create mode 100644 logging-khronicle/src/jsMain/kotlin/KhronicleLogger.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8c909b1..eba4c6a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,6 +4,7 @@ kotlin = "2.0.20" [libraries] coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } +khronicle-core = { module = "com.juul.khronicle:khronicle-core", version = "0.3.0" } [plugins] dokka = { id = "org.jetbrains.dokka", version = "1.9.20" } diff --git a/logging-khronicle/build.gradle.kts b/logging-khronicle/build.gradle.kts new file mode 100644 index 0000000..9dfb22e --- /dev/null +++ b/logging-khronicle/build.gradle.kts @@ -0,0 +1,24 @@ +plugins { + kotlin("multiplatform") + id("org.jmailen.kotlinter") + id("org.jetbrains.dokka") + id("com.vanniktech.maven.publish") +} + +kotlin { + explicitApi() + + js { + browser() + binaries.library() + } + + sourceSets { + val commonMain by getting { + dependencies { + api(project(":core")) + api(libs.khronicle.core) + } + } + } +} diff --git a/logging-khronicle/src/jsMain/kotlin/Event.kt b/logging-khronicle/src/jsMain/kotlin/Event.kt new file mode 100644 index 0000000..8702818 --- /dev/null +++ b/logging-khronicle/src/jsMain/kotlin/Event.kt @@ -0,0 +1,6 @@ +package com.juul.indexeddb.logs + +import com.juul.khronicle.Key +import org.w3c.dom.events.Event + +public object Event : Key diff --git a/logging-khronicle/src/jsMain/kotlin/KhronicleLogger.kt b/logging-khronicle/src/jsMain/kotlin/KhronicleLogger.kt new file mode 100644 index 0000000..c2046c6 --- /dev/null +++ b/logging-khronicle/src/jsMain/kotlin/KhronicleLogger.kt @@ -0,0 +1,25 @@ +package com.juul.indexeddb.logs + +import com.juul.khronicle.Log +import com.juul.khronicle.LogLevel +import org.w3c.dom.events.Event as JsEvent + +public object KhronicleLogger : Logger { + + override fun log(type: Type, event: JsEvent?, message: () -> String) { + val level = when (event?.type) { + "error", "blocked" -> LogLevel.Error + else -> when (type) { + Type.Database -> LogLevel.Info + Type.Transaction -> LogLevel.Debug + else -> LogLevel.Verbose + } + } + Log.dynamic(level = level, tag = "IndexedDB/$type") { metadata -> + if (event != null) { + metadata[Event] = event + } + message() + } + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 339019b..eccfbb7 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -11,4 +11,5 @@ pluginManagement { include( "core", "external", + "logging-khronicle", )