From cf0c934acf369c04d91b0e090a731b101c570a96 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 23 Feb 2021 17:40:05 +0300 Subject: [PATCH] Move logging to implementation --- .../kotlin/hep/dataforge/context/logging.kt | 21 ++++++-- .../kotlin/hep/dataforge/context/loggingJs.kt | 8 +++ .../hep/dataforge/context/loggingJvm.kt | 8 +++ .../hep/dataforge/context/loggingNative.kt | 53 +++++++++++++++++++ 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 dataforge-context/src/jsMain/kotlin/hep/dataforge/context/loggingJs.kt create mode 100644 dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/loggingJvm.kt create mode 100644 dataforge-context/src/nativeMain/kotlin/hep/dataforge/context/loggingNative.kt diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/logging.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/logging.kt index c43f7ea2..6868e0f2 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/logging.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/logging.kt @@ -2,20 +2,31 @@ package hep.dataforge.context import hep.dataforge.misc.Named import hep.dataforge.provider.Path -import mu.KLogger -import mu.KotlinLogging + +/** + * Part of kotlin-logging interface + */ +public expect interface Logger { + public fun trace(msg: () -> Any?) + public fun debug(msg: () -> Any?) + public fun info(msg: () -> Any?) + public fun warn(msg: () -> Any?) + public fun error(msg: () -> Any?) +} + +public expect fun Context.buildLogger(name: String): Logger /** * The logger specific to this context */ -public val Context.logger: KLogger get() = KotlinLogging.logger(name.toString()) +public val Context.logger: Logger get() = buildLogger(name.toString()) /** * The logger */ -public val ContextAware.logger: KLogger +public val ContextAware.logger: Logger get() = if (this is Named) { - KotlinLogging.logger(Path(context.name, this.name).toString()) + context.buildLogger(Path(context.name, this.name).toString()) } else { context.logger } diff --git a/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/loggingJs.kt b/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/loggingJs.kt new file mode 100644 index 00000000..bf2df3bb --- /dev/null +++ b/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/loggingJs.kt @@ -0,0 +1,8 @@ +package hep.dataforge.context + +import mu.KLogger +import mu.KotlinLogging + +public actual typealias Logger = KLogger + +public actual fun Context.buildLogger(name: String): Logger = KotlinLogging.logger(name) \ No newline at end of file diff --git a/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/loggingJvm.kt b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/loggingJvm.kt new file mode 100644 index 00000000..bf2df3bb --- /dev/null +++ b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/loggingJvm.kt @@ -0,0 +1,8 @@ +package hep.dataforge.context + +import mu.KLogger +import mu.KotlinLogging + +public actual typealias Logger = KLogger + +public actual fun Context.buildLogger(name: String): Logger = KotlinLogging.logger(name) \ No newline at end of file diff --git a/dataforge-context/src/nativeMain/kotlin/hep/dataforge/context/loggingNative.kt b/dataforge-context/src/nativeMain/kotlin/hep/dataforge/context/loggingNative.kt new file mode 100644 index 00000000..4654ebac --- /dev/null +++ b/dataforge-context/src/nativeMain/kotlin/hep/dataforge/context/loggingNative.kt @@ -0,0 +1,53 @@ +package hep.dataforge.context + + +public actual interface Logger { + /** + * Lazy add a log message if isTraceEnabled is true + */ + public actual fun trace(msg: () -> Any?) + + /** + * Lazy add a log message if isDebugEnabled is true + */ + public actual fun debug(msg: () -> Any?) + + /** + * Lazy add a log message if isInfoEnabled is true + */ + public actual fun info(msg: () -> Any?) + + /** + * Lazy add a log message if isWarnEnabled is true + */ + public actual fun warn(msg: () -> Any?) + + /** + * Lazy add a log message if isErrorEnabled is true + */ + public actual fun error(msg: () -> Any?) + +} + +public actual fun Context.buildLogger(name: String): Logger = object :Logger{ + override fun trace(msg: () -> Any?) { + println("[TRACE] $name - ${msg()}") + } + + override fun debug(msg: () -> Any?) { + println("[DEBUG] $name - ${msg()}") + } + + override fun info(msg: () -> Any?) { + println("[INFO] $name - ${msg()}") + } + + override fun warn(msg: () -> Any?) { + println("[WARNING] $name - ${msg()}") + } + + override fun error(msg: () -> Any?) { + println("[ERROR] $name - ${msg()}") + } + +} \ No newline at end of file