Skip to content

Commit

Permalink
consolidate platforms with non-delegating impl (#350)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oshai authored Aug 10, 2023
1 parent a821a78 commit 6a3f37c
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 261 deletions.
7 changes: 5 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions src/directMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
44 changes: 44 additions & 0 deletions src/directMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Any>? = null,
) {
public constructor(
level: Level,
marker: Marker?,
loggerName: String,
eventBuilder: KLoggingEventBuilder
) : this(
level,
marker,
loggerName,
eventBuilder.message,
eventBuilder.cause,
eventBuilder.payload
)
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 0 additions & 9 deletions src/jsMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
}
}
}

This file was deleted.

11 changes: 0 additions & 11 deletions src/jsMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
}
}

This file was deleted.

This file was deleted.

This file was deleted.

28 changes: 4 additions & 24 deletions src/jsTest/kotlin/io/github/oshai/kotlinlogging/SimpleJsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SimpleJsTest {

@AfterTest
fun cleanup() {
KotlinLoggingConfiguration.APPENDER = ConsoleOutputAppender
KotlinLoggingConfiguration.APPENDER = ConsoleOutputAppender()
KotlinLoggingConfiguration.LOG_LEVEL = Level.INFO
}

Expand Down Expand Up @@ -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()
}
}
}
Expand Down
11 changes: 0 additions & 11 deletions src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Loading

0 comments on commit 6a3f37c

Please sign in to comment.