Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into upgrade_gradle_8_7
Browse files Browse the repository at this point in the history
  • Loading branch information
severn-everett committed Apr 12, 2024
2 parents bf0a3c5 + d0514cd commit 1060c58
Show file tree
Hide file tree
Showing 27 changed files with 157 additions and 61 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ plugins {
apply("versions.gradle.kts")

group = "io.github.oshai"
version = "6.0.4"
version = "6.0.5"

repositories {
gradlePluginPortal()
Expand Down Expand Up @@ -268,7 +268,7 @@ tasks {
spotless {
kotlin {
target("src/**/*.kt")
ktfmt("0.24").googleStyle()
ktfmt("0.47").googleStyle()
}
}

Expand Down
25 changes: 13 additions & 12 deletions src/commonMain/kotlin/io/github/oshai/kotlinlogging/KLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface KLogger {

/**
* Return the name of this `Logger` instance.
*
* @return name of this logger instance
*/
public val name: String
Expand Down Expand Up @@ -378,7 +379,7 @@ public interface KLogger {
*/
@Deprecated(
"Use trace {} instead",
replaceWith = ReplaceWith("trace(marker) { \"\$msg \$arg1 \$arg2\"}")
replaceWith = ReplaceWith("trace(marker) { \"\$msg \$arg1 \$arg2\"}"),
)
public fun trace(marker: Marker?, msg: String?, arg1: Any?, arg2: Any?): Unit = TODO()

Expand All @@ -392,7 +393,7 @@ public interface KLogger {
*/
@Deprecated(
"Use trace {} instead",
replaceWith = ReplaceWith("trace(marker) { \"\$msg \$arguments\"}")
replaceWith = ReplaceWith("trace(marker) { \"\$msg \$arguments\"}"),
)
public fun trace(marker: Marker?, msg: String?, vararg arguments: Any?): Unit = TODO()

Expand Down Expand Up @@ -494,7 +495,7 @@ public interface KLogger {
*/
@Deprecated(
"Use debug {} instead",
replaceWith = ReplaceWith("debug(marker) { \"\$msg \$arg1 \$arg2\"}")
replaceWith = ReplaceWith("debug(marker) { \"\$msg \$arg1 \$arg2\"}"),
)
public fun debug(marker: Marker?, msg: String?, arg1: Any?, arg2: Any?): Unit = TODO()

Expand All @@ -508,7 +509,7 @@ public interface KLogger {
*/
@Deprecated(
"Use debug {} instead",
replaceWith = ReplaceWith("debug(marker) { \"\$msg \$arguments\"}")
replaceWith = ReplaceWith("debug(marker) { \"\$msg \$arguments\"}"),
)
public fun debug(marker: Marker?, msg: String?, vararg arguments: Any?): Unit = TODO()

Expand Down Expand Up @@ -609,7 +610,7 @@ public interface KLogger {
*/
@Deprecated(
"Use info {} instead",
replaceWith = ReplaceWith("info(marker) { \"\$msg \$arg1 \$arg2\"}")
replaceWith = ReplaceWith("info(marker) { \"\$msg \$arg1 \$arg2\"}"),
)
public fun info(marker: Marker?, msg: String?, arg1: Any?, arg2: Any?): Unit = TODO()

Expand All @@ -623,7 +624,7 @@ public interface KLogger {
*/
@Deprecated(
"Use info {} instead",
replaceWith = ReplaceWith("info(marker) { \"\$msg \$arguments\"}")
replaceWith = ReplaceWith("info(marker) { \"\$msg \$arguments\"}"),
)
public fun info(marker: Marker?, msg: String?, vararg arguments: Any?): Unit = TODO()

Expand Down Expand Up @@ -724,7 +725,7 @@ public interface KLogger {
*/
@Deprecated(
"Use warn {} instead",
replaceWith = ReplaceWith("warn(marker) { \"\$msg \$arg1 \$arg2\"}")
replaceWith = ReplaceWith("warn(marker) { \"\$msg \$arg1 \$arg2\"}"),
)
public fun warn(marker: Marker?, msg: String?, arg1: Any?, arg2: Any?): Unit = TODO()

Expand All @@ -738,7 +739,7 @@ public interface KLogger {
*/
@Deprecated(
"Use warn {} instead",
replaceWith = ReplaceWith("warn(marker) { \"\$msg \$arguments\"}")
replaceWith = ReplaceWith("warn(marker) { \"\$msg \$arguments\"}"),
)
public fun warn(marker: Marker?, msg: String?, vararg arguments: Any?): Unit = TODO()

Expand Down Expand Up @@ -817,7 +818,7 @@ public interface KLogger {
*/
@Deprecated(
"Use error(marker){} instead",
replaceWith = ReplaceWith("error(marker) { \"\$msg\"}")
replaceWith = ReplaceWith("error(marker) { \"\$msg\"}"),
)
public fun error(marker: Marker?, msg: String?): Unit = error(null as Throwable?, marker) { msg }

Expand All @@ -831,7 +832,7 @@ public interface KLogger {
*/
@Deprecated(
"Use error(marker){} instead",
replaceWith = ReplaceWith("error(marker) { \"\$msg \$arg \"}")
replaceWith = ReplaceWith("error(marker) { \"\$msg \$arg \"}"),
)
public fun error(marker: Marker?, msg: String?, arg: Any?): Unit = TODO()

Expand All @@ -846,7 +847,7 @@ public interface KLogger {
*/
@Deprecated(
"Use error(marker){} instead",
replaceWith = ReplaceWith("error(marker) { \"\$msg \$arg1 \$arg2\"}")
replaceWith = ReplaceWith("error(marker) { \"\$msg \$arg1 \$arg2\"}"),
)
public fun error(marker: Marker?, msg: String?, arg1: Any?, arg2: Any?): Unit = TODO()

Expand All @@ -860,7 +861,7 @@ public interface KLogger {
*/
@Deprecated(
"Use error(marker){} instead",
replaceWith = ReplaceWith("error(marker) { \"\$msg \$arguments\"}")
replaceWith = ReplaceWith("error(marker) { \"\$msg \$arguments\"}"),
)
public fun error(marker: Marker?, msg: String?, vararg arguments: Any?): Unit = TODO()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public object KotlinLogging {
* ```
* private val logger = KotlinLogging.logger("io.github.oshai.kotlinlogging.MyLogger")
* ```
*
* In most cases the name represents the package notation of the file that the logger is defined
* in.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class DarwinKLogger(override val name: String, override val underlyingLog
__dso_handle.ptr,
underlyingLogger,
level.toDarwinLevel(),
formattedMessage
formattedMessage,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal actual object KLoggerFactory {
name.contains(".") -> {
DarwinKLogger(
name,
os_log_create(name.substringBeforeLast("."), name.substringAfterLast("."))
os_log_create(name.substringBeforeLast("."), name.substringAfterLast(".")),
)
}
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface Appender {

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public data class KLoggingEvent(
level: Level,
marker: Marker?,
loggerName: String,
eventBuilder: KLoggingEventBuilder
eventBuilder: KLoggingEventBuilder,
) : this(
level,
marker,
loggerName,
eventBuilder.message,
eventBuilder.cause,
eventBuilder.payload
eventBuilder.payload,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ internal actual object KLoggerNameResolver {
// to search for the target declared field.
null
}
}
?: clazz
} ?: clazz
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.oshai.kotlinlogging.Level
import io.github.oshai.kotlinlogging.Marker
import io.github.oshai.kotlinlogging.slf4j.internal.Slf4jLoggerFactory
import io.github.oshai.kotlinlogging.slf4j.internal.Slf4jMarker
import org.slf4j.Logger
import org.slf4j.MarkerFactory

public fun Marker.toSlf4j(): org.slf4j.Marker = MarkerFactory.getMarker(this.getName())
public fun Marker.toSlf4j(): org.slf4j.Marker =
when (this) {
is Slf4jMarker -> marker
else -> MarkerFactory.getMarker(getName())
}

public fun org.slf4j.Marker.toKotlinLogging(): Marker = Slf4jMarker(this)

public fun Level.toSlf4j(): org.slf4j.event.Level =
when (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware
override fun isLoggingEnabledFor(level: Level, marker: Marker?): Boolean {
return isLoggingEnabledFor(underlyingLogger, level, marker)
}

override fun at(level: Level, marker: Marker?, block: KLoggingEventBuilder.() -> Unit) {
if (isLoggingEnabledFor(level, marker)) {
KLoggingEventBuilder().apply(block).run {
Expand All @@ -52,7 +53,7 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware
private fun logWithPayload(
kLoggingEventBuilder: KLoggingEventBuilder,
level: Level,
marker: Marker?
marker: Marker?,
) {
val builder = underlyingLogger.atLevel(level.toSlf4j())
marker?.toSlf4j()?.let { builder.addMarker(it) }
Expand All @@ -67,15 +68,15 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware
private fun logWithoutPayload(
kLoggingEventBuilder: KLoggingEventBuilder,
level: Level,
marker: Marker?
marker: Marker?,
) {
underlyingLogger.log(
marker?.toSlf4j(),
fqcn,
level.toSlf4j().toInt(),
kLoggingEventBuilder.message,
null,
kLoggingEventBuilder.cause
kLoggingEventBuilder.cause,
)
}

Expand Down Expand Up @@ -158,7 +159,7 @@ internal class LocationAwareKLogger(override val underlyingLogger: LocationAware
EventConstants.TRACE_INT,
tp.message,
arrayOf<Any?>(result),
tp.throwable
tp.throwable,
)
}
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class LocationIgnorantKLogger(override val underlyingLogger: Logger) :
private fun logWithPayload(
kLoggingEventBuilder: KLoggingEventBuilder,
level: Level,
marker: Marker?
marker: Marker?,
) {
val builder = underlyingLogger.atLevel(level.toSlf4j())
marker?.toSlf4j()?.let { builder.addMarker(it) }
Expand All @@ -50,7 +50,7 @@ internal class LocationIgnorantKLogger(override val underlyingLogger: Logger) :
private fun logWithoutPayload(
kLoggingEventBuilder: KLoggingEventBuilder,
level: Level,
marker: Marker?
marker: Marker?,
) {
val slf4jMarker = marker?.toSlf4j()
val message = kLoggingEventBuilder.message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class Slf4jLogger<T : Logger> : KLogger, DelegatingKLogger<T> {
protected fun isLoggingEnabledFor(
underlyingLogger: Logger,
level: Level,
marker: Marker?
marker: Marker?,
): Boolean {
return when (level) {
Level.TRACE -> underlyingLogger.isTraceEnabled(marker?.toSlf4j())
Expand Down Expand Up @@ -137,21 +137,21 @@ public abstract class Slf4jLogger<T : Logger> : KLogger, DelegatingKLogger<T> {

@Deprecated(
"Use error(marker){} instead",
replaceWith = ReplaceWith("error(marker){ \"\$msg \$arg\"}")
replaceWith = ReplaceWith("error(marker){ \"\$msg \$arg\"}"),
)
public override fun error(marker: Marker?, msg: String?, arg: Any?): Unit =
underlyingLogger.error(marker?.toSlf4j(), msg, arg)

@Deprecated(
"Use error(marker){} instead",
replaceWith = ReplaceWith("error(marker){ \"\$msg \$arg1 \$arg2\"}")
replaceWith = ReplaceWith("error(marker){ \"\$msg \$arg1 \$arg2\"}"),
)
public override fun error(marker: Marker?, msg: String?, arg1: Any?, arg2: Any?): Unit =
underlyingLogger.error(marker?.toSlf4j(), msg, arg1, arg2)

@Deprecated(
"Use error(marker){} instead",
replaceWith = ReplaceWith("error(marker){ \"\$msg \$arguments\"}")
replaceWith = ReplaceWith("error(marker){ \"\$msg \$arguments\"}"),
)
public override fun error(marker: Marker?, msg: String?, vararg arguments: Any?): Unit =
underlyingLogger.error(marker?.toSlf4j(), msg, *arguments)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.oshai.kotlinlogging.slf4j.internal

import io.github.oshai.kotlinlogging.Marker

internal class Slf4jMarker(val marker: org.slf4j.Marker) : Marker, org.slf4j.Marker by marker
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlin.reflect.KProperty

/**
* The JS way to define a logger without explicit name
*
* ```
* class MyClass {
* private val logger by KotlinLogging.logger()
Expand All @@ -21,8 +22,7 @@ private class LoggerDelegate : ReadOnlyProperty<Any?, KLogger> {
logger =
thisRef.asDynamic()?.constructor?.name.unsafeCast<String?>()?.let {
KotlinLogging.logger(it)
}
?: KotlinLogging.logger("root-logger")
} ?: KotlinLogging.logger("root-logger")
}
return logger
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public actual object KotlinLoggingConfiguration {
set(value) {
appender = value
}

@Deprecated("Use logLevel instead", ReplaceWith("logLevel"))
public var LOG_LEVEL: Level
get() = logLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.slf4j.MDC
public inline fun <T> withLoggingContext(
pair: Pair<String, String?>,
restorePrevious: Boolean = true,
body: () -> T
body: () -> T,
): T =
if (pair.second == null) {
body()
Expand All @@ -44,7 +44,7 @@ public inline fun <T> withLoggingContext(
public inline fun <T> withLoggingContext(
vararg pair: Pair<String, String?>,
restorePrevious: Boolean = true,
body: () -> T
body: () -> T,
): T {
val pairForMDC = pair.filter { it.second != null }
val cleanupCallbacks =
Expand Down Expand Up @@ -81,7 +81,7 @@ public inline fun <T> withLoggingContext(
public inline fun <T> withLoggingContext(
map: Map<String, String?>,
restorePrevious: Boolean = true,
body: () -> T
body: () -> T,
): T {
val cleanupCallbacks =
map.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import kotlinx.coroutines.slf4j.*
public suspend inline fun <T> withLoggingContextAsync(
pair: Pair<String, String?>,
restorePrevious: Boolean = true,
crossinline body: suspend () -> T
crossinline body: suspend () -> T,
): T =
withLoggingContext(pair, restorePrevious = restorePrevious) {
withContext(MDCContext()) { body() }
Expand All @@ -37,7 +37,7 @@ public suspend inline fun <T> withLoggingContextAsync(
public suspend inline fun <T> withLoggingContextAsync(
vararg pair: Pair<String, String?>,
restorePrevious: Boolean = true,
crossinline body: suspend () -> T
crossinline body: suspend () -> T,
): T =
withLoggingContext(*pair, restorePrevious = restorePrevious) {
withContext(MDCContext()) { body() }
Expand All @@ -59,7 +59,7 @@ public suspend inline fun <T> withLoggingContextAsync(
public suspend inline fun <T> withLoggingContextAsync(
map: Map<String, String?>,
restorePrevious: Boolean = true,
crossinline body: suspend () -> T
crossinline body: suspend () -> T,
): T =
withLoggingContext(map, restorePrevious = restorePrevious) {
withContext(MDCContext()) { body() }
Expand Down
Loading

0 comments on commit 1060c58

Please sign in to comment.