Move logging to implementation

This commit is contained in:
Alexander Nozik 2021-02-23 17:40:05 +03:00
parent a726307641
commit cf0c934acf
4 changed files with 85 additions and 5 deletions

View File

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

View File

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

View File

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

View File

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