Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM Anonymous Logging Refinement #424

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
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
Loading