-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
25 changed files
with
167 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/directMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
src/directMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/directMain/kotlin/io/github/oshai/kotlinlogging/KLoggingEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
src/directMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions
28
src/directMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerDirect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
17 changes: 11 additions & 6 deletions
17
src/jsMain/kotlin/io/github/oshai/kotlinlogging/ConsoleOutputAppender.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
src/jsMain/kotlin/io/github/oshai/kotlinlogging/DefaultMessageFormatter.kt
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
src/jsMain/kotlin/io/github/oshai/kotlinlogging/Formatter.kt
This file was deleted.
Oops, something went wrong.
21 changes: 17 additions & 4 deletions
21
src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
5 changes: 0 additions & 5 deletions
5
src/jsMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingLevel.kt
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
src/jsMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerJS.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
src/nativeMain/kotlin/io/github/oshai/kotlinlogging/Appender.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.