Skip to content

Commit

Permalink
Changes needed for compiler plugin (#474)
Browse files Browse the repository at this point in the history
* Changes needed for compiler plugin
* Hide the public API
* Hide also toStringSafe() and castToThrowable() extension functions

---------

Co-authored-by: Neeme Praks <neeme.praks@eesti.ee>
  • Loading branch information
neeme-praks-sympower and Neeme Praks authored Feb 4, 2025
1 parent e14380d commit 2b2dbc7
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class KLoggingEventBuilder {
* Internal data that is used by compiler plugin to provide additional information about the log
* site. Not intended for use by user code, API stability is not guaranteed.
*/
public var internalCompilerData: InternalCompilerData? = null
internal var internalCompilerData: InternalCompilerData? = null

public class InternalCompilerData(
public val messageTemplate: String? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package io.github.oshai.kotlinlogging.internal

import io.github.oshai.kotlinlogging.KLogger
import io.github.oshai.kotlinlogging.KLoggingEventBuilder

@Deprecated(
message = "This is an internal API and should not be used by user code",
level = DeprecationLevel.HIDDEN,
)
@Suppress("kotlin:S1133")
public fun (() -> Any?).hiddenToStringSafe(): String = toStringSafe()

@Deprecated(
message = "This is an internal API and should not be used by user code",
level = DeprecationLevel.HIDDEN,
)
@Suppress("kotlin:S1133")
public fun Any?.hiddenCastToThrowable(): Throwable? {
return if (this is Throwable) {
this
} else {
null
}
}

@Deprecated(
message = "This is an internal API and should not be used by user code",
level = DeprecationLevel.HIDDEN,
)
@Suppress("kotlin:S1133")
public fun KLoggingEventBuilder.hiddenInternalCompilerData(
compilerData: KLoggingEventBuilder.InternalCompilerData
) {
internalCompilerData = compilerData
}

@Deprecated(
message = "This is an internal API and should not be used by user code",
level = DeprecationLevel.HIDDEN,
)
@Suppress("kotlin:S1133")
public fun KLogger.entryWithCompilerData(
compilerData: KLoggingEventBuilder.InternalCompilerData? = null,
vararg arguments: Any?,
): Unit = atTrace {
message = "entry(${arguments.joinToString()})"
internalCompilerData = compilerData
}

@Deprecated(
message = "This is an internal API and should not be used by user code",
level = DeprecationLevel.HIDDEN,
)
@Suppress("kotlin:S1133")
public fun KLogger.exitWithCompilerData(
compilerData: KLoggingEventBuilder.InternalCompilerData? = null
): Unit = atTrace {
message = "exit"
internalCompilerData = compilerData
}

@Deprecated(
message = "This is an internal API and should not be used by user code",
level = DeprecationLevel.HIDDEN,
)
@Suppress("kotlin:S1133")
public fun <T> KLogger.exitWithCompilerData(
compilerData: KLoggingEventBuilder.InternalCompilerData? = null,
result: T,
): T where T : Any? {
atTrace {
message = "exit($result)"
internalCompilerData = compilerData
}
return result
}

@Deprecated(
message = "This is an internal API and should not be used by user code",
level = DeprecationLevel.HIDDEN,
)
@Suppress("kotlin:S1133")
public fun <T> KLogger.throwingWithCompilerData(
compilerData: KLoggingEventBuilder.InternalCompilerData? = null,
throwable: T,
): T where T : Throwable {
atError {
cause = throwable
message = "throwing($throwable)"
internalCompilerData = compilerData
}
return throwable
}

@Deprecated(
message = "This is an internal API and should not be used by user code",
level = DeprecationLevel.HIDDEN,
)
@Suppress("kotlin:S1133")
public fun <T> KLogger.catchingWithCompilerData(
compilerData: KLoggingEventBuilder.InternalCompilerData? = null,
throwable: T,
) where T : Throwable {
atError {
cause = throwable
message = "catching($throwable)"
internalCompilerData = compilerData
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.spi.LogbackServiceProvider
import io.github.oshai.kotlinlogging.KLogger

internal object LogbackLoggerFactory {
public object LogbackLoggerFactory {

private val logbackServiceProvider = createLogbackServiceProvider()

Expand All @@ -22,5 +22,6 @@ internal object LogbackLoggerFactory {
internal fun wrapLogbackLogger(logbackLogger: Logger): KLogger =
LogbackLoggerWrapper(logbackLogger, logbackServiceProvider)

fun getLoggerContext() = logbackServiceProvider.loggerFactory as LoggerContext
public fun getLoggerContext(): LoggerContext =
logbackServiceProvider.loggerFactory as LoggerContext
}

0 comments on commit 2b2dbc7

Please sign in to comment.