Skip to content

Commit c5df855

Browse files
committed
Improve docs for getLogger and normalizeLoggerName
1 parent e31347a commit c5df855

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/commonMain/kotlin/dev/hermannm/devlog/Logger.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import kotlin.reflect.KClass
3737
* the file name in that case.
3838
*
3939
* This is the pattern that
40-
* [the SLF4J docs recommends](https://www.slf4j.org/faq.html#declaration_pattern) for getting
41-
* loggers for a class in a generic manner.
40+
* [the SLF4J docs recommends](https://www.slf4j.org/faq.html#declaration_pattern) for instantiating
41+
* loggers in a generic manner.
4242
*/
4343
public expect inline fun getLogger(): Logger
4444

@@ -557,7 +557,8 @@ internal expect interface PlatformLogger {
557557
}
558558

559559
/**
560-
* Removes any `Kt` suffix from the given class name (added to
560+
* Removes any `Kt` suffix from the given class name (which Kotlin adds to the classes that are
561+
* generated for the top-level of files).
561562
*
562563
* Implementation based on the
563564
* [KLoggerNameResolver from kotlin-logging](https://github.com/oshai/kotlin-logging/blob/e9c6ec570cd503c626fca5878efcf1291d4125b7/src/jvmMain/kotlin/mu/internal/KLoggerNameResolver.kt#L9-L19),
@@ -566,6 +567,9 @@ internal expect interface PlatformLogger {
566567
*/
567568
internal fun normalizeLoggerName(name: String?): String {
568569
return when {
570+
// We may get a null name from `KClass.qualifiedName` in the `getLogger(forClass: KClass<*>)`
571+
// overload. Although this should be rare (as it would only happen if the user somehow passed a
572+
// synthetic class), we want to gracefully handle this, using "Logger" as a generic fallback.
569573
name == null -> "Logger"
570574
name.contains("Kt$") -> name.substringBefore("Kt$")
571575
name.contains("$") -> name.substringBefore("$")

src/jvmMain/kotlin/dev/hermannm/devlog/Logger.jvm.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ public actual inline fun getLogger(): Logger {
1414
// inline, that will actually return the class that called `getLogger`, so we can use it to get
1515
// the name of the caller. When called at file scope, the calling class will be the synthetic `Kt`
1616
// class that Kotlin generates for the file, so we can use the file name in that case.
17+
//
18+
// This is the pattern that SLF4J recommends for instantiating loggers in a generic manner:
19+
// https://www.slf4j.org/faq.html#declaration_pattern
1720
return getLogger(javaClass = MethodHandles.lookup().lookupClass())
1821
}
1922

0 commit comments

Comments
 (0)