From 6a3f37c2219d83fe9cddad974a54eb5ad70f0689 Mon Sep 17 00:00:00 2001 From: Ohad Shai Date: Thu, 10 Aug 2023 09:48:42 +0300 Subject: [PATCH] consolidate platforms with non-delegating impl (#350) create directMain source set as base for all non-delegating platforms - platforms that use kotlin-logging to write the logs with native kotlin functions and not via other libs or platform constructs. This change the js/linux/etc' platforms. Some duplication was removed. And the way it is now configured changed. `KotlinLoggingConfiguration` now contains: - logLevel - formatter - appender Those interfaces changed. Logger now calls the appender that is responsible to call the formatter - unlike previously where it called the formatter and passed the message to the appender. Appender don't need to check if log is enabled, and has a single method: `fun log(loggingEvent: KLoggingEvent)` (which contains also payload). Formatter has a single method: `fun formatMessage(loggingEvent: KLoggingEvent): String` which returns a formatted message. * old upper case names in js are now deprecated and delegating to those. --- build.gradle.kts | 7 ++- .../io/github/oshai/kotlinlogging/Appender.kt | 14 ++++++ .../github/oshai/kotlinlogging/Formatter.kt | 44 +++++++++++++++++++ .../oshai/kotlinlogging/KLoggingEvent.kt | 24 ++++++++++ .../KotlinLoggingConfiguration.kt | 7 +++ .../oshai/kotlinlogging/KotlinLoggingLevel.kt | 0 .../internal/ErrorMessageProducer.kt | 0 .../kotlinlogging/internal/KLoggerDirect.kt | 28 ++++++++++++ .../kotlinlogging/internal/KLoggerFactory.kt | 2 +- .../io/github/oshai/kotlinlogging/Appender.kt | 9 ---- .../kotlinlogging/ConsoleOutputAppender.kt | 17 ++++--- .../kotlinlogging/DefaultMessageFormatter.kt | 40 ----------------- .../github/oshai/kotlinlogging/Formatter.kt | 11 ----- .../KotlinLoggingConfiguration.kt | 21 +++++++-- .../oshai/kotlinlogging/KotlinLoggingLevel.kt | 5 --- .../kotlinlogging/internal/KLoggerFactory.kt | 8 ---- .../oshai/kotlinlogging/internal/KLoggerJS.kt | 37 ---------------- .../oshai/kotlinlogging/SimpleJsTest.kt | 28 ++---------- .../io/github/oshai/kotlinlogging/Appender.kt | 11 ----- .../kotlinlogging/ConsoleOutputAppender.kt | 16 +++---- .../kotlinlogging/DefaultMessageFormatter.kt | 37 ---------------- .../github/oshai/kotlinlogging/Formatter.kt | 13 ------ .../KotlinLoggingConfiguration.kt | 10 ++--- .../internal/ErrorMessageProducer.kt | 3 -- .../kotlinlogging/internal/KLoggerNative.kt | 36 --------------- 25 files changed, 167 insertions(+), 261 deletions(-) create mode 100644 src/directMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt create mode 100644 src/directMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt create mode 100644 src/directMain/kotlin/io/github/oshai/kotlinlogging/KLoggingEvent.kt create mode 100644 src/directMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt rename src/{nativeMain => directMain}/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt (100%) rename src/{jsMain => directMain}/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt (100%) create mode 100644 src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerDirect.kt rename src/{nativeMain => directMain}/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt (68%) delete mode 100644 src/jsMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt delete mode 100644 src/jsMain/kotlin/io/github/oshai/kotlinlogging/DefaultMessageFormatter.kt delete mode 100644 src/jsMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt delete mode 100644 src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt delete mode 100644 src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt delete mode 100644 src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerJS.kt delete mode 100644 src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt delete mode 100644 src/nativeMain/kotlin/io/github/oshai/kotlinlogging/DefaultMessageFormatter.kt delete mode 100644 src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt delete mode 100644 src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt delete mode 100644 src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNative.kt diff --git a/build.gradle.kts b/build.gradle.kts index 16e42ebf..d9d2860c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -146,16 +146,19 @@ kotlin { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:${extra["coroutines_version"]}") } } - val jsMain by getting { + val directMain by creating { dependsOn(commonMain) } + val jsMain by getting { + dependsOn(directMain) + } val jsTest by getting { dependencies { implementation(kotlin("test-js")) } } val nativeMain by creating { - dependsOn(commonMain) + dependsOn(directMain) } val nativeTest by creating { dependencies { diff --git a/src/directMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt b/src/directMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt new file mode 100644 index 00000000..5d56b226 --- /dev/null +++ b/src/directMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt @@ -0,0 +1,14 @@ +package io.github.oshai.kotlinlogging + +public interface Appender { + public fun log(loggingEvent: KLoggingEvent) +} + +public abstract class FormattingAppender : Appender { + public abstract fun logFormattedMessage(loggingEvent: KLoggingEvent, formattedMessage: Any?) + override fun log(loggingEvent: KLoggingEvent) { + KotlinLoggingConfiguration.formatter.formatMessage(loggingEvent).let { + logFormattedMessage(loggingEvent, it) + } + } +} diff --git a/src/directMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt b/src/directMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt new file mode 100644 index 00000000..ab92ac5d --- /dev/null +++ b/src/directMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt @@ -0,0 +1,44 @@ +package io.github.oshai.kotlinlogging + +public interface Formatter { + + public fun formatMessage(loggingEvent: KLoggingEvent): String +} + +public class DefaultMessageFormatter(private val includePrefix: Boolean = true) : Formatter { + + override fun formatMessage(loggingEvent: KLoggingEvent): String { + with(loggingEvent) { + return buildString { + append(prefix(level, loggerName)) + marker?.getName()?.let { + append(it) + append(" ") + } + append(message) + append(cause.throwableToString()) + } + } + } + + private fun prefix(level: Level, loggerName: String): String { + return if (includePrefix) { + "${level.name}: [$loggerName] " + } else { + "" + } + } + + private fun Throwable?.throwableToString(): String { + if (this == null) { + return "" + } + var msg = "" + var current = this + while (current != null && current.cause != current) { + msg += ", Caused by: '${current.message}'" + current = current.cause + } + return msg + } +} diff --git a/src/directMain/kotlin/io/github/oshai/kotlinlogging/KLoggingEvent.kt b/src/directMain/kotlin/io/github/oshai/kotlinlogging/KLoggingEvent.kt new file mode 100644 index 00000000..427f5396 --- /dev/null +++ b/src/directMain/kotlin/io/github/oshai/kotlinlogging/KLoggingEvent.kt @@ -0,0 +1,24 @@ +package io.github.oshai.kotlinlogging + +public data class KLoggingEvent( + public val level: Level, + public val marker: Marker?, + public val loggerName: String, + public val message: String? = null, + public val cause: Throwable? = null, + public val payload: Map? = null, +) { + public constructor( + level: Level, + marker: Marker?, + loggerName: String, + eventBuilder: KLoggingEventBuilder + ) : this( + level, + marker, + loggerName, + eventBuilder.message, + eventBuilder.cause, + eventBuilder.payload + ) +} diff --git a/src/directMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt b/src/directMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt new file mode 100644 index 00000000..4dcff99e --- /dev/null +++ b/src/directMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt @@ -0,0 +1,7 @@ +package io.github.oshai.kotlinlogging + +public expect object KotlinLoggingConfiguration { + public var logLevel: Level + public var formatter: Formatter + public var appender: Appender +} diff --git a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt b/src/directMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt similarity index 100% rename from src/nativeMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt rename to src/directMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt diff --git a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt b/src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt similarity index 100% rename from src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt rename to src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt diff --git a/src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerDirect.kt b/src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerDirect.kt new file mode 100644 index 00000000..a2a00513 --- /dev/null +++ b/src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerDirect.kt @@ -0,0 +1,28 @@ +package io.github.oshai.kotlinlogging.internal + +import io.github.oshai.kotlinlogging.KLogger +import io.github.oshai.kotlinlogging.KLoggingEvent +import io.github.oshai.kotlinlogging.KLoggingEventBuilder +import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration +import io.github.oshai.kotlinlogging.Level +import io.github.oshai.kotlinlogging.Level.OFF +import io.github.oshai.kotlinlogging.Marker +import io.github.oshai.kotlinlogging.isLoggingEnabled + +internal class KLoggerDirect(override val name: String) : KLogger { + + override fun at(level: Level, marker: Marker?, block: KLoggingEventBuilder.() -> Unit) { + if (isLoggingEnabledFor(level, marker)) { + KLoggingEventBuilder().apply(block).run { + when (level) { + OFF -> Unit + else -> KotlinLoggingConfiguration.appender.log(KLoggingEvent(level, marker, name, this)) + } + } + } + } + + override fun isLoggingEnabledFor(level: Level, marker: Marker?): Boolean { + return level.isLoggingEnabled() + } +} diff --git a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt b/src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt similarity index 68% rename from src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt rename to src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt index 568fdbd0..b866658a 100644 --- a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt +++ b/src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt @@ -4,5 +4,5 @@ import io.github.oshai.kotlinlogging.KLogger internal actual object KLoggerFactory { - actual fun logger(name: String): KLogger = KLoggerNative(name) + actual fun logger(name: String): KLogger = KLoggerDirect(name) } diff --git a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt deleted file mode 100644 index 4fcb87a8..00000000 --- a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt +++ /dev/null @@ -1,9 +0,0 @@ -package io.github.oshai.kotlinlogging - -public interface Appender { - public fun trace(message: Any?) - public fun debug(message: Any?) - public fun info(message: Any?) - public fun warn(message: Any?) - public fun error(message: Any?) -} diff --git a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/ConsoleOutputAppender.kt b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/ConsoleOutputAppender.kt index 4a4806bf..5240cd89 100644 --- a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/ConsoleOutputAppender.kt +++ b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/ConsoleOutputAppender.kt @@ -1,9 +1,14 @@ package io.github.oshai.kotlinlogging -public object ConsoleOutputAppender : Appender { - public override fun trace(message: Any?): Unit = console.log(message) - public override fun debug(message: Any?): Unit = console.log(message) - public override fun info(message: Any?): Unit = console.info(message) - public override fun warn(message: Any?): Unit = console.warn(message) - public override fun error(message: Any?): Unit = console.error(message) +public class ConsoleOutputAppender : FormattingAppender() { + override fun logFormattedMessage(loggingEvent: KLoggingEvent, formattedMessage: Any?) { + when (loggingEvent.level) { + Level.TRACE -> console.log(formattedMessage) + Level.DEBUG -> console.log(formattedMessage) + Level.INFO -> console.info(formattedMessage) + Level.WARN -> console.warn(formattedMessage) + Level.ERROR -> console.error(formattedMessage) + Level.OFF -> Unit + } + } } diff --git a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/DefaultMessageFormatter.kt b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/DefaultMessageFormatter.kt deleted file mode 100644 index 0da96047..00000000 --- a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/DefaultMessageFormatter.kt +++ /dev/null @@ -1,40 +0,0 @@ -package io.github.oshai.kotlinlogging - -import io.github.oshai.kotlinlogging.internal.toStringSafe - -public object DefaultMessageFormatter : Formatter { - - public override fun formatMessage( - level: Level, - loggerName: String, - marker: Marker?, - throwable: Throwable?, - message: () -> Any? - ): String { - return buildString { - append(level.name) - append(": [") - append(loggerName) - append("] ") - marker?.getName()?.let { - append(it) - append(" ") - } - append(message.toStringSafe()) - append(throwable.throwableToString()) - } - } - - private fun Throwable?.throwableToString(): String { - if (this == null) { - return "" - } - var msg = "" - var current = this - while (current != null && current.cause != current) { - msg += ", Caused by: '${current.message}'" - current = current.cause - } - return msg - } -} diff --git a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt deleted file mode 100644 index 0d26368e..00000000 --- a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt +++ /dev/null @@ -1,11 +0,0 @@ -package io.github.oshai.kotlinlogging - -public interface Formatter { - public fun formatMessage( - level: Level, - loggerName: String, - marker: Marker?, - throwable: Throwable?, - message: () -> Any? - ): Any? -} diff --git a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt index 12cd3e42..18489c3d 100644 --- a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt +++ b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt @@ -1,7 +1,20 @@ package io.github.oshai.kotlinlogging -public object KotlinLoggingConfiguration { - public var LOG_LEVEL: Level = Level.INFO - public var APPENDER: Appender = ConsoleOutputAppender - public var FORMATTER: Formatter = DefaultMessageFormatter +public actual object KotlinLoggingConfiguration { + public actual var logLevel: Level = Level.INFO + public actual var formatter: Formatter = DefaultMessageFormatter(includePrefix = true) + public actual var appender: Appender = ConsoleOutputAppender() + + @Deprecated("Use appender instead", ReplaceWith("appender")) + public var APPENDER: Appender + get() = appender + set(value) { + appender = value + } + @Deprecated("Use logLevel instead", ReplaceWith("logLevel")) + public var LOG_LEVEL: Level + get() = logLevel + set(value) { + logLevel = value + } } diff --git a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt deleted file mode 100644 index afe7a3dd..00000000 --- a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt +++ /dev/null @@ -1,5 +0,0 @@ -package io.github.oshai.kotlinlogging - -import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration.LOG_LEVEL - -public fun Level.isLoggingEnabled(): Boolean = this.ordinal >= LOG_LEVEL.ordinal diff --git a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt deleted file mode 100644 index 8564e19f..00000000 --- a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt +++ /dev/null @@ -1,8 +0,0 @@ -package io.github.oshai.kotlinlogging.internal - -import io.github.oshai.kotlinlogging.KLogger - -internal actual object KLoggerFactory { - - actual fun logger(name: String): KLogger = KLoggerJS(name) -} diff --git a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerJS.kt b/src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerJS.kt deleted file mode 100644 index 438bbcb1..00000000 --- a/src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerJS.kt +++ /dev/null @@ -1,37 +0,0 @@ -package io.github.oshai.kotlinlogging.internal - -import io.github.oshai.kotlinlogging.KLogger -import io.github.oshai.kotlinlogging.KLoggingEventBuilder -import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration.APPENDER -import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration.FORMATTER -import io.github.oshai.kotlinlogging.Level -import io.github.oshai.kotlinlogging.Level.DEBUG -import io.github.oshai.kotlinlogging.Level.ERROR -import io.github.oshai.kotlinlogging.Level.INFO -import io.github.oshai.kotlinlogging.Level.OFF -import io.github.oshai.kotlinlogging.Level.TRACE -import io.github.oshai.kotlinlogging.Level.WARN -import io.github.oshai.kotlinlogging.Marker -import io.github.oshai.kotlinlogging.isLoggingEnabled - -internal class KLoggerJS(override val name: String) : KLogger { - - override fun at(level: Level, marker: Marker?, block: KLoggingEventBuilder.() -> Unit) { - if (isLoggingEnabledFor(level, marker)) { - KLoggingEventBuilder().apply(block).run { - when (level) { - TRACE -> APPENDER.trace(FORMATTER.formatMessage(level, name, marker, cause) { message }) - DEBUG -> APPENDER.debug(FORMATTER.formatMessage(level, name, marker, cause) { message }) - INFO -> APPENDER.info(FORMATTER.formatMessage(level, name, marker, cause) { message }) - WARN -> APPENDER.warn(FORMATTER.formatMessage(level, name, marker, cause) { message }) - ERROR -> APPENDER.error(FORMATTER.formatMessage(level, name, marker, cause) { message }) - OFF -> Unit - } - } - } - } - - override fun isLoggingEnabledFor(level: Level, marker: Marker?): Boolean { - return level.isLoggingEnabled() - } -} diff --git a/src/jsTest/kotlin/io/github/oshai/kotlinlogging/SimpleJsTest.kt b/src/jsTest/kotlin/io/github/oshai/kotlinlogging/SimpleJsTest.kt index ad2f4b08..6c3f9b7f 100644 --- a/src/jsTest/kotlin/io/github/oshai/kotlinlogging/SimpleJsTest.kt +++ b/src/jsTest/kotlin/io/github/oshai/kotlinlogging/SimpleJsTest.kt @@ -15,7 +15,7 @@ class SimpleJsTest { @AfterTest fun cleanup() { - KotlinLoggingConfiguration.APPENDER = ConsoleOutputAppender + KotlinLoggingConfiguration.APPENDER = ConsoleOutputAppender() KotlinLoggingConfiguration.LOG_LEVEL = Level.INFO } @@ -47,29 +47,9 @@ class SimpleJsTest { var lastMessage: String = "NA" var lastLevel: String = "NA" - override fun trace(message: Any?) { - lastMessage = message.toString() - lastLevel = "trace" - } - - override fun debug(message: Any?) { - lastMessage = message.toString() - lastLevel = "debug" - } - - override fun info(message: Any?) { - lastMessage = message.toString() - lastLevel = "info" - } - - override fun warn(message: Any?) { - lastMessage = message.toString() - lastLevel = "warn" - } - - override fun error(message: Any?) { - lastMessage = message.toString() - lastLevel = "error" + override fun log(loggingEvent: KLoggingEvent) { + lastMessage = DefaultMessageFormatter(includePrefix = true).formatMessage(loggingEvent) + lastLevel = loggingEvent.level.name.toLowerCase() } } } diff --git a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt b/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt deleted file mode 100644 index 72fae92b..00000000 --- a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt +++ /dev/null @@ -1,11 +0,0 @@ -package io.github.oshai.kotlinlogging - -public interface Appender { - public val includePrefix: Boolean - - public fun trace(loggerName: String, message: String) - public fun debug(loggerName: String, message: String) - public fun info(loggerName: String, message: String) - public fun warn(loggerName: String, message: String) - public fun error(loggerName: String, message: String) -} diff --git a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/ConsoleOutputAppender.kt b/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/ConsoleOutputAppender.kt index a8f4c0e5..a51ed3c0 100644 --- a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/ConsoleOutputAppender.kt +++ b/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/ConsoleOutputAppender.kt @@ -3,14 +3,12 @@ package io.github.oshai.kotlinlogging import platform.posix.fprintf import platform.posix.stderr -public object ConsoleOutputAppender : Appender { - override val includePrefix: Boolean = true - public override fun trace(loggerName: String, message: String): Unit = println(message) - public override fun debug(loggerName: String, message: String): Unit = println(message) - public override fun info(loggerName: String, message: String): Unit = println(message) - public override fun warn(loggerName: String, message: String): Unit = println(message) - - override fun error(loggerName: String, message: String) { - fprintf(stderr, "$message\n") +public object ConsoleOutputAppender : FormattingAppender() { + override fun logFormattedMessage(loggingEvent: KLoggingEvent, formattedMessage: Any?) { + if (loggingEvent.level == Level.ERROR) { + fprintf(stderr, "$formattedMessage\n") + } else { + println(formattedMessage) + } } } diff --git a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/DefaultMessageFormatter.kt b/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/DefaultMessageFormatter.kt deleted file mode 100644 index bf054463..00000000 --- a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/DefaultMessageFormatter.kt +++ /dev/null @@ -1,37 +0,0 @@ -package io.github.oshai.kotlinlogging - -import io.github.oshai.kotlinlogging.internal.toStringSafe - -public object DefaultMessageFormatter : Formatter { - - public override fun formatMessage( - includePrefix: Boolean, - level: Level, - loggerName: String, - marker: Marker?, - t: Throwable?, - msg: () -> Any? - ): String = - "${prefix(includePrefix, level, loggerName)}${marker?.getName()} ${msg.toStringSafe()}${t.throwableToString()}" - - private fun prefix(includePrefix: Boolean, level: Level, loggerName: String): String { - return if (includePrefix) { - "${level.name}: [$loggerName] " - } else { - "" - } - } - - private fun Throwable?.throwableToString(): String { - if (this == null) { - return "" - } - var msg = "" - var current = this - while (current != null && current.cause != current) { - msg += ", Caused by: '${current.message}'" - current = current.cause - } - return msg - } -} diff --git a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt b/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt deleted file mode 100644 index a6ce341f..00000000 --- a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt +++ /dev/null @@ -1,13 +0,0 @@ -package io.github.oshai.kotlinlogging - -public interface Formatter { - - public fun formatMessage( - includePrefix: Boolean, - level: Level, - loggerName: String, - marker: Marker?, - t: Throwable?, - msg: () -> Any? - ): String -} diff --git a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt b/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt index b30aaae7..9ca99631 100644 --- a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt +++ b/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt @@ -4,21 +4,21 @@ import kotlin.native.concurrent.AtomicReference public expect val DefaultAppender: Appender -public object KotlinLoggingConfiguration { +public actual object KotlinLoggingConfiguration { private val _logLevel = AtomicReference(Level.INFO) - public var logLevel: Level + public actual var logLevel: Level get() = _logLevel.value set(value) { _logLevel.value = value } private val _appender = AtomicReference(DefaultAppender) - public var appender: Appender + public actual var appender: Appender get() = _appender.value set(value) { _appender.value = value } - private val _formatter = AtomicReference(DefaultMessageFormatter) - public var formatter: Formatter + private val _formatter = AtomicReference(DefaultMessageFormatter(includePrefix = true)) + public actual var formatter: Formatter get() = _formatter.value set(value) { _formatter.value = value diff --git a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt b/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt deleted file mode 100644 index 6d35a773..00000000 --- a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt +++ /dev/null @@ -1,3 +0,0 @@ -package io.github.oshai.kotlinlogging.internal - -internal actual typealias ErrorMessageProducer = DefaultErrorMessageProducer diff --git a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNative.kt b/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNative.kt deleted file mode 100644 index 8db7a033..00000000 --- a/src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNative.kt +++ /dev/null @@ -1,36 +0,0 @@ -package io.github.oshai.kotlinlogging.internal - -import io.github.oshai.kotlinlogging.KLogger -import io.github.oshai.kotlinlogging.KLoggingEventBuilder -import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration.appender -import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration.formatter -import io.github.oshai.kotlinlogging.Level -import io.github.oshai.kotlinlogging.Level.* -import io.github.oshai.kotlinlogging.Marker -import io.github.oshai.kotlinlogging.isLoggingEnabled - -internal class KLoggerNative(override val name: String) : KLogger { - - override fun at(level: Level, marker: Marker?, block: KLoggingEventBuilder.() -> Unit) { - if (isLoggingEnabledFor(level, marker)) { - KLoggingEventBuilder().apply(block).run { - val formattedMessage = - formatter.formatMessage(appender.includePrefix, level, name, marker, this.cause) { - this.message - } - when (level) { - TRACE -> appender.trace(name, formattedMessage) - DEBUG -> appender.debug(name, formattedMessage) - INFO -> appender.info(name, formattedMessage) - WARN -> appender.warn(name, formattedMessage) - ERROR -> appender.error(name, formattedMessage) - OFF -> Unit - } - } - } - } - - override fun isLoggingEnabledFor(level: Level, marker: Marker?): Boolean { - return level.isLoggingEnabled() - } -}