Skip to content

Commit

Permalink
Refined algorithm to determine the name of the hosting class of an an…
Browse files Browse the repository at this point in the history
…onymous logger
  • Loading branch information
severn-everett committed Apr 16, 2024
1 parent c3b55ba commit 904bfdf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
package io.github.oshai.kotlinlogging.internal

private const val NO_CLASS = ""

internal actual object KLoggerNameResolver {
private val kotlinLoggingRegex = Regex("\\.KotlinLogging\\.logger\\s")
private val topLevelPropertyRegex = Regex("<init properties (\\S+)\\.kt>")
private val classPropertyRegex = Regex("\\.(\\S+)\\.<init>")

internal actual fun name(func: () -> Unit): String {
var found = false
val exception = Exception()
for (line in exception.stackTraceToString().split("\n")) {
if (found) {
return line.substringBefore(".kt").substringAfterLast(".").substringAfterLast("/")
}
if (line.contains("at KotlinLogging")) {
found = true
}
val stackTrace = Exception().stackTraceToString().split("\n")
val invokingClassLine = stackTrace.indexOfFirst(kotlinLoggingRegex::containsMatchIn) + 1
return if (invokingClassLine in 1 ..< stackTrace.size) {
getInvokingClass(stackTrace[invokingClassLine])
} else {
NO_CLASS
}
return ""
}

private fun getInvokingClass(line: String): String {
return topLevelPropertyRegex.find(line)?.let {
it.groupValues[1].split(".").last()
} ?: classPropertyRegex.find(line)?.let {
it.groupValues[1].split(".").last()
} ?: NO_CLASS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package io.github.oshai.kotlinlogging

import kotlin.test.*

private val logger = KotlinLogging.logger("SimpleWasmJsTest")
private val namedLogger = KotlinLogging.logger("SimpleWasmJsTest")
private val anonymousFilePropLogger = KotlinLogging.logger { }

class SimpleWasmJsTest {
private lateinit var appender: SimpleAppender
private val anonymousClassPropLogger = KotlinLogging.logger { }

@BeforeTest
fun setup() {
Expand All @@ -21,17 +23,31 @@ class SimpleWasmJsTest {

@Test
fun simpleWasmJsTest() {
assertEquals("SimpleWasmJsTest", logger.name)
logger.info { "info msg" }
assertEquals("SimpleWasmJsTest", namedLogger.name)
namedLogger.info { "info msg" }
assertEquals("INFO: [SimpleWasmJsTest] info msg", appender.lastMessage)
assertEquals("info", appender.lastLevel)
}

@Test
fun anonymousFilePropWasmJsTest() {
assertEquals("SimpleWasmJsTest", anonymousFilePropLogger.name)
anonymousFilePropLogger.info { "info msg" }
assertEquals("INFO: [SimpleWasmJsTest] info msg", appender.lastMessage)
}

@Test
fun anonymousClassPropWasmJsTest() {
assertEquals("SimpleWasmJsTest", anonymousClassPropLogger.name)
anonymousFilePropLogger.info { "info msg" }
assertEquals("INFO: [SimpleWasmJsTest] info msg", appender.lastMessage)
}

@Test
fun offLevelWasmJsTest() {
KotlinLoggingConfiguration.logLevel = Level.OFF
assertTrue(logger.isLoggingOff())
logger.error { "error msg" }
assertTrue(namedLogger.isLoggingOff())
namedLogger.error { "error msg" }
assertEquals("NA", appender.lastMessage)
assertEquals("NA", appender.lastLevel)
}
Expand Down

0 comments on commit 904bfdf

Please sign in to comment.