Skip to content

Commit 6a3f37c

Browse files
authored
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.
1 parent a821a78 commit 6a3f37c

File tree

25 files changed

+167
-261
lines changed

25 files changed

+167
-261
lines changed

build.gradle.kts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,19 @@ kotlin {
146146
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:${extra["coroutines_version"]}")
147147
}
148148
}
149-
val jsMain by getting {
149+
val directMain by creating {
150150
dependsOn(commonMain)
151151
}
152+
val jsMain by getting {
153+
dependsOn(directMain)
154+
}
152155
val jsTest by getting {
153156
dependencies {
154157
implementation(kotlin("test-js"))
155158
}
156159
}
157160
val nativeMain by creating {
158-
dependsOn(commonMain)
161+
dependsOn(directMain)
159162
}
160163
val nativeTest by creating {
161164
dependencies {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.oshai.kotlinlogging
2+
3+
public interface Appender {
4+
public fun log(loggingEvent: KLoggingEvent)
5+
}
6+
7+
public abstract class FormattingAppender : Appender {
8+
public abstract fun logFormattedMessage(loggingEvent: KLoggingEvent, formattedMessage: Any?)
9+
override fun log(loggingEvent: KLoggingEvent) {
10+
KotlinLoggingConfiguration.formatter.formatMessage(loggingEvent).let {
11+
logFormattedMessage(loggingEvent, it)
12+
}
13+
}
14+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.github.oshai.kotlinlogging
2+
3+
public interface Formatter {
4+
5+
public fun formatMessage(loggingEvent: KLoggingEvent): String
6+
}
7+
8+
public class DefaultMessageFormatter(private val includePrefix: Boolean = true) : Formatter {
9+
10+
override fun formatMessage(loggingEvent: KLoggingEvent): String {
11+
with(loggingEvent) {
12+
return buildString {
13+
append(prefix(level, loggerName))
14+
marker?.getName()?.let {
15+
append(it)
16+
append(" ")
17+
}
18+
append(message)
19+
append(cause.throwableToString())
20+
}
21+
}
22+
}
23+
24+
private fun prefix(level: Level, loggerName: String): String {
25+
return if (includePrefix) {
26+
"${level.name}: [$loggerName] "
27+
} else {
28+
""
29+
}
30+
}
31+
32+
private fun Throwable?.throwableToString(): String {
33+
if (this == null) {
34+
return ""
35+
}
36+
var msg = ""
37+
var current = this
38+
while (current != null && current.cause != current) {
39+
msg += ", Caused by: '${current.message}'"
40+
current = current.cause
41+
}
42+
return msg
43+
}
44+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.oshai.kotlinlogging
2+
3+
public data class KLoggingEvent(
4+
public val level: Level,
5+
public val marker: Marker?,
6+
public val loggerName: String,
7+
public val message: String? = null,
8+
public val cause: Throwable? = null,
9+
public val payload: Map<String, Any>? = null,
10+
) {
11+
public constructor(
12+
level: Level,
13+
marker: Marker?,
14+
loggerName: String,
15+
eventBuilder: KLoggingEventBuilder
16+
) : this(
17+
level,
18+
marker,
19+
loggerName,
20+
eventBuilder.message,
21+
eventBuilder.cause,
22+
eventBuilder.payload
23+
)
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.github.oshai.kotlinlogging
2+
3+
public expect object KotlinLoggingConfiguration {
4+
public var logLevel: Level
5+
public var formatter: Formatter
6+
public var appender: Appender
7+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.oshai.kotlinlogging.internal
2+
3+
import io.github.oshai.kotlinlogging.KLogger
4+
import io.github.oshai.kotlinlogging.KLoggingEvent
5+
import io.github.oshai.kotlinlogging.KLoggingEventBuilder
6+
import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration
7+
import io.github.oshai.kotlinlogging.Level
8+
import io.github.oshai.kotlinlogging.Level.OFF
9+
import io.github.oshai.kotlinlogging.Marker
10+
import io.github.oshai.kotlinlogging.isLoggingEnabled
11+
12+
internal class KLoggerDirect(override val name: String) : KLogger {
13+
14+
override fun at(level: Level, marker: Marker?, block: KLoggingEventBuilder.() -> Unit) {
15+
if (isLoggingEnabledFor(level, marker)) {
16+
KLoggingEventBuilder().apply(block).run {
17+
when (level) {
18+
OFF -> Unit
19+
else -> KotlinLoggingConfiguration.appender.log(KLoggingEvent(level, marker, name, this))
20+
}
21+
}
22+
}
23+
}
24+
25+
override fun isLoggingEnabledFor(level: Level, marker: Marker?): Boolean {
26+
return level.isLoggingEnabled()
27+
}
28+
}

src/nativeMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt renamed to src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import io.github.oshai.kotlinlogging.KLogger
44

55
internal actual object KLoggerFactory {
66

7-
actual fun logger(name: String): KLogger = KLoggerNative(name)
7+
actual fun logger(name: String): KLogger = KLoggerDirect(name)
88
}

src/jsMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)