Skip to content

Commit

Permalink
Refactor logger() to avoid having to use an inline function here
Browse files Browse the repository at this point in the history
  • Loading branch information
leadrian committed Jul 24, 2021
1 parent c667a82 commit c2ba570
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/main/kotlin/ch/leadrian/slf4k/Slf4k.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,21 @@ 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]
*/
inline fun <reified T : Any> 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 <reified T : Any> T.logger(): Logger = LoggerFactory.getLogger(resolveClassToLog(T::class))

@PublishedApi
internal fun resolveClassToLog(inputClass: KClass<*>): Class<out Any> {
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)
}

/**
Expand Down

0 comments on commit c2ba570

Please sign in to comment.