From b6dad141f8f08cebc200294f83c54288bc89e4c6 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Mon, 1 Mar 2021 10:25:02 +0300 Subject: [PATCH] Fix log loading --- dataforge-context/api/dataforge-context.api | 28 +++++++++---------- dataforge-context/build.gradle.kts | 3 +- .../kotlin/hep/dataforge/context/Global.kt | 4 +-- .../hep/dataforge/context/LogManager.kt | 18 ++++++++---- .../kotlin/hep/dataforge/context/loggingJs.kt | 24 ++++++++-------- .../hep/dataforge/context/loggingJvm.kt | 26 +++++++++-------- .../hep/dataforge/context/loggingNative.kt | 10 ++----- .../workspace/SimpleWorkspaceTest.kt | 2 ++ 8 files changed, 59 insertions(+), 56 deletions(-) diff --git a/dataforge-context/api/dataforge-context.api b/dataforge-context/api/dataforge-context.api index b603c615..8ea82998 100644 --- a/dataforge-context/api/dataforge-context.api +++ b/dataforge-context/api/dataforge-context.api @@ -96,20 +96,6 @@ public final class hep/dataforge/context/Global : hep/dataforge/context/Context public final fun getLogger ()Lhep/dataforge/context/LogManager; } -public final class hep/dataforge/context/KLoggingManager : hep/dataforge/context/AbstractPlugin, hep/dataforge/context/LogManager { - public static final field Companion Lhep/dataforge/context/KLoggingManager$Companion; - public fun ()V - public fun getTag ()Lhep/dataforge/context/PluginTag; - public fun log (Lhep/dataforge/names/Name;Ljava/lang/String;Lkotlin/jvm/functions/Function0;)V -} - -public final class hep/dataforge/context/KLoggingManager$Companion : hep/dataforge/context/PluginFactory { - public fun getTag ()Lhep/dataforge/context/PluginTag; - public fun getType ()Lkotlin/reflect/KClass; - public fun invoke (Lhep/dataforge/meta/Meta;Lhep/dataforge/context/Context;)Lhep/dataforge/context/KLoggingManager; - public synthetic fun invoke (Lhep/dataforge/meta/Meta;Lhep/dataforge/context/Context;)Ljava/lang/Object; -} - public abstract interface class hep/dataforge/context/LogManager : hep/dataforge/context/Logable, hep/dataforge/context/Plugin { public static final field Companion Lhep/dataforge/context/LogManager$Companion; public static final field DEBUG Ljava/lang/String; @@ -245,6 +231,20 @@ public final class hep/dataforge/context/ResolveKt { public static final fun resolve (Lhep/dataforge/context/Context;Ljava/lang/String;Lhep/dataforge/names/Name;Lkotlin/reflect/KClass;)Ljava/lang/Object; } +public final class hep/dataforge/context/SlfLogManager : hep/dataforge/context/AbstractPlugin, hep/dataforge/context/LogManager { + public static final field Companion Lhep/dataforge/context/SlfLogManager$Companion; + public fun ()V + public fun getTag ()Lhep/dataforge/context/PluginTag; + public fun log (Lhep/dataforge/names/Name;Ljava/lang/String;Lkotlin/jvm/functions/Function0;)V +} + +public final class hep/dataforge/context/SlfLogManager$Companion : hep/dataforge/context/PluginFactory { + public fun getTag ()Lhep/dataforge/context/PluginTag; + public fun getType ()Lkotlin/reflect/KClass; + public fun invoke (Lhep/dataforge/meta/Meta;Lhep/dataforge/context/Context;)Lhep/dataforge/context/SlfLogManager; + public synthetic fun invoke (Lhep/dataforge/meta/Meta;Lhep/dataforge/context/Context;)Ljava/lang/Object; +} + public abstract interface annotation class hep/dataforge/descriptors/Attribute : java/lang/annotation/Annotation { public abstract fun key ()Ljava/lang/String; public abstract fun value ()Ljava/lang/String; diff --git a/dataforge-context/build.gradle.kts b/dataforge-context/build.gradle.kts index 0eb1083e..abbaf290 100644 --- a/dataforge-context/build.gradle.kts +++ b/dataforge-context/build.gradle.kts @@ -19,13 +19,12 @@ kotlin { jvmMain { dependencies { api(kotlin("reflect")) - api("io.github.microutils:kotlin-logging-jvm:2.0.4") implementation("ch.qos.logback:logback-classic:1.2.3") } } jsMain { dependencies { - api("io.github.microutils:kotlin-logging-js:2.0.4") + } } } diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Global.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Global.kt index b1b38179..f288d65a 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Global.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/Global.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.SupervisorJob import kotlin.coroutines.CoroutineContext import kotlin.native.concurrent.ThreadLocal -internal expect val globalLogger: LogManager +internal expect val globalLoggerFactory: PluginFactory /** * A global root context. Closing [Global] terminates the framework. @@ -20,7 +20,7 @@ public object Global : Context("GLOBAL".asName(), null, Meta.EMPTY) { /** * The default logging manager */ - public val logger: LogManager = globalLogger + public val logger: LogManager by lazy { globalLoggerFactory.invoke(context = this).apply { attach(this@Global) } } /** * Closing all contexts diff --git a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/LogManager.kt b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/LogManager.kt index 032f4e09..a96f4f40 100644 --- a/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/LogManager.kt +++ b/dataforge-context/src/commonMain/kotlin/hep/dataforge/context/LogManager.kt @@ -4,7 +4,7 @@ import hep.dataforge.misc.Named import hep.dataforge.names.Name import hep.dataforge.names.plus -public interface Logable{ +public interface Logable { public fun log(name: Name, tag: String, body: () -> String) } @@ -19,19 +19,25 @@ public interface LogManager : Plugin, Logable { } } - - public fun Logable.trace(name: Name = Name.EMPTY, body: () -> String): Unit = log(name, LogManager.TRACE, body) public fun Logable.info(name: Name = Name.EMPTY, body: () -> String): Unit = log(name, LogManager.INFO, body) public fun Logable.debug(name: Name = Name.EMPTY, body: () -> String): Unit = log(name, LogManager.DEBUG, body) public fun Logable.warn(name: Name = Name.EMPTY, body: () -> String): Unit = log(name, LogManager.WARNING, body) public fun Logable.error(name: Name = Name.EMPTY, body: () -> String): Unit = log(name, LogManager.ERROR, body) +internal val (() -> String).safe: String + get() = try { + invoke() + } catch (t: Throwable) { + "Error while evaluating log string: ${t.message}" + } + + public fun Logable.error(throwable: Throwable?, name: Name = Name.EMPTY, body: () -> String): Unit = log(name, LogManager.ERROR) { buildString { appendLine(body()) - throwable?.let { appendLine(throwable.stackTraceToString())} + throwable?.let { appendLine(throwable.stackTraceToString()) } } } @@ -46,10 +52,10 @@ public val Context.logger: LogManager */ public val ContextAware.logger: Logable get() = if (this is Named) { - object :Logable{ + object : Logable { val contextLog = context.logger override fun log(name: Name, tag: String, body: () -> String) { - contextLog.log(this@logger.name + name,tag, body) + contextLog.log(this@logger.name + name, tag, body) } } } else { diff --git a/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/loggingJs.kt b/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/loggingJs.kt index ffeb68f6..172edece 100644 --- a/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/loggingJs.kt +++ b/dataforge-context/src/jsMain/kotlin/hep/dataforge/context/loggingJs.kt @@ -2,30 +2,28 @@ package hep.dataforge.context import hep.dataforge.meta.Meta import hep.dataforge.names.Name -import mu.KotlinLogging import kotlin.reflect.KClass -public class KLoggingManager : AbstractPlugin(), LogManager { +public class ConsoleLogManager : AbstractPlugin(), LogManager { override fun log(name: Name, tag: String, body: () -> String) { - val logger = KotlinLogging.logger("[${context.name}] $name") + val message: String = body.safe when (tag) { - LogManager.DEBUG -> logger.debug(body) - LogManager.INFO -> logger.info(body) - LogManager.WARNING -> logger.warn(body) - LogManager.ERROR -> logger.error(body) - else -> logger.trace(body) + LogManager.INFO -> console.info("[${context.name}] $name: $message") + LogManager.WARNING -> console.warn("[${context.name}] $name: $message") + LogManager.ERROR -> console.error("[${context.name}] $name: $message") + else -> console.log("[${context.name}] $name: $message") } } override val tag: PluginTag get() = Companion.tag - public companion object : PluginFactory { - override fun invoke(meta: Meta, context: Context): KLoggingManager = KLoggingManager() + public companion object : PluginFactory { + override fun invoke(meta: Meta, context: Context): ConsoleLogManager = ConsoleLogManager() - override val tag: PluginTag = PluginTag(group = PluginTag.DATAFORGE_GROUP, name = "log.kotlinLogging") - override val type: KClass = KLoggingManager::class + override val tag: PluginTag = PluginTag(group = PluginTag.DATAFORGE_GROUP, name = "log.jsConsole") + override val type: KClass = ConsoleLogManager::class } } -internal actual val globalLogger: LogManager = KLoggingManager().apply { attach(Global) } +internal actual val globalLoggerFactory: PluginFactory = ConsoleLogManager diff --git a/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/loggingJvm.kt b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/loggingJvm.kt index ffeb68f6..4796bb24 100644 --- a/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/loggingJvm.kt +++ b/dataforge-context/src/jvmMain/kotlin/hep/dataforge/context/loggingJvm.kt @@ -2,30 +2,32 @@ package hep.dataforge.context import hep.dataforge.meta.Meta import hep.dataforge.names.Name -import mu.KotlinLogging + +import org.slf4j.LoggerFactory import kotlin.reflect.KClass -public class KLoggingManager : AbstractPlugin(), LogManager { +public class SlfLogManager : AbstractPlugin(), LogManager { override fun log(name: Name, tag: String, body: () -> String) { - val logger = KotlinLogging.logger("[${context.name}] $name") + val logger = LoggerFactory.getLogger("[${context.name}] $name") //KotlinLogging.logger("[${context.name}] $name") + val message = body.safe when (tag) { - LogManager.DEBUG -> logger.debug(body) - LogManager.INFO -> logger.info(body) - LogManager.WARNING -> logger.warn(body) - LogManager.ERROR -> logger.error(body) - else -> logger.trace(body) + LogManager.DEBUG -> logger.debug(message) + LogManager.INFO -> logger.info(message) + LogManager.WARNING -> logger.warn(message) + LogManager.ERROR -> logger.error(message) + else -> logger.trace(message) } } override val tag: PluginTag get() = Companion.tag - public companion object : PluginFactory { - override fun invoke(meta: Meta, context: Context): KLoggingManager = KLoggingManager() + public companion object : PluginFactory { + override fun invoke(meta: Meta, context: Context): SlfLogManager = SlfLogManager() override val tag: PluginTag = PluginTag(group = PluginTag.DATAFORGE_GROUP, name = "log.kotlinLogging") - override val type: KClass = KLoggingManager::class + override val type: KClass = SlfLogManager::class } } -internal actual val globalLogger: LogManager = KLoggingManager().apply { attach(Global) } +internal actual val globalLoggerFactory: PluginFactory = SlfLogManager diff --git a/dataforge-context/src/nativeMain/kotlin/hep/dataforge/context/loggingNative.kt b/dataforge-context/src/nativeMain/kotlin/hep/dataforge/context/loggingNative.kt index 9caa3398..8d471225 100644 --- a/dataforge-context/src/nativeMain/kotlin/hep/dataforge/context/loggingNative.kt +++ b/dataforge-context/src/nativeMain/kotlin/hep/dataforge/context/loggingNative.kt @@ -8,12 +8,8 @@ import kotlin.reflect.KClass public class NativeLogManager : AbstractPlugin(), LogManager { override fun log(name: Name, tag: String, body: () -> String) { - val text = try { - body() - } catch (t: Throwable){ - "Error while evaluating log string: ${t.message}" - } - println("[${context.name}] $name: $text") + val message: String = body.safe + println("[${context.name}] $name: $message") } override val tag: PluginTag get() = Companion.tag @@ -26,4 +22,4 @@ public class NativeLogManager : AbstractPlugin(), LogManager { } } -internal actual val globalLogger: LogManager = NativeLogManager().apply { attach(Global) } +internal actual val globalLoggerFactory: PluginFactory = NativeLogManager diff --git a/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt b/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt index 5ee08ea5..b4374624 100644 --- a/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt +++ b/dataforge-workspace/src/jvmTest/kotlin/hep/dataforge/workspace/SimpleWorkspaceTest.kt @@ -1,3 +1,5 @@ +@file:Suppress("UNUSED_VARIABLE") + package hep.dataforge.workspace import hep.dataforge.context.*