From c2ba570394e277f1dad95d5f44865664ba4ba868 Mon Sep 17 00:00:00 2001 From: leadrian Date: Sun, 25 Jul 2021 00:51:34 +0200 Subject: [PATCH] Refactor logger() to avoid having to use an inline function here --- src/main/kotlin/ch/leadrian/slf4k/Slf4k.kt | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/ch/leadrian/slf4k/Slf4k.kt b/src/main/kotlin/ch/leadrian/slf4k/Slf4k.kt index 51db6ef..8df3e4d 100644 --- a/src/main/kotlin/ch/leadrian/slf4k/Slf4k.kt +++ b/src/main/kotlin/ch/leadrian/slf4k/Slf4k.kt @@ -20,7 +20,6 @@ package ch.leadrian.slf4k import org.slf4j.Logger import org.slf4j.LoggerFactory import org.slf4j.Marker -import kotlin.reflect.KClass /** * @return a logger for the given parameterized type [T] @@ -28,17 +27,14 @@ import kotlin.reflect.KClass inline fun loggerFor(): Logger = LoggerFactory.getLogger(T::class.java) /** - * @return a logger for the given receiver type [T] or it's declaring class in case of a companion class + * @return a logger for the given receiver's class or it's declaring class in case of a companion class */ -@Suppress("unused") -inline fun T.logger(): Logger = LoggerFactory.getLogger(resolveClassToLog(T::class)) - -@PublishedApi -internal fun resolveClassToLog(inputClass: KClass<*>): Class { - return when { - inputClass.isCompanion -> inputClass.java.declaringClass ?: inputClass.java - else -> inputClass.java +fun Any.logger(): Logger { + val classToLog = when { + this::class.isCompanion -> javaClass.declaringClass ?: javaClass + else -> javaClass } + return LoggerFactory.getLogger(classToLog) } /**