Fix log loading

This commit is contained in:
Alexander Nozik 2021-03-01 10:25:02 +03:00
parent f3d43cd40a
commit b6dad141f8
8 changed files with 59 additions and 56 deletions

View File

@ -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 <init> ()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 <init> ()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;

View File

@ -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")
}
}
}

View File

@ -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<out LogManager>
/**
* 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

View File

@ -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 {

View File

@ -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<KLoggingManager> {
override fun invoke(meta: Meta, context: Context): KLoggingManager = KLoggingManager()
public companion object : PluginFactory<ConsoleLogManager> {
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<out KLoggingManager> = KLoggingManager::class
override val tag: PluginTag = PluginTag(group = PluginTag.DATAFORGE_GROUP, name = "log.jsConsole")
override val type: KClass<out ConsoleLogManager> = ConsoleLogManager::class
}
}
internal actual val globalLogger: LogManager = KLoggingManager().apply { attach(Global) }
internal actual val globalLoggerFactory: PluginFactory<out LogManager> = ConsoleLogManager

View File

@ -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<KLoggingManager> {
override fun invoke(meta: Meta, context: Context): KLoggingManager = KLoggingManager()
public companion object : PluginFactory<SlfLogManager> {
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<out KLoggingManager> = KLoggingManager::class
override val type: KClass<out SlfLogManager> = SlfLogManager::class
}
}
internal actual val globalLogger: LogManager = KLoggingManager().apply { attach(Global) }
internal actual val globalLoggerFactory: PluginFactory<out LogManager> = SlfLogManager

View File

@ -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<out LogManager> = NativeLogManager

View File

@ -1,3 +1,5 @@
@file:Suppress("UNUSED_VARIABLE")
package hep.dataforge.workspace
import hep.dataforge.context.*