Nikky
10/17/2021, 1:22 PM.(Main.kt:23)
into a clickable link
i eneded up making a custom renderer and ${level}F
overloads that throws and catches to get the stacktrace.. its not clean.. but timing measurements show it only costs 100us on average.. good enough for meMichael Strasser
10/17/2021, 10:03 PMNikky
10/17/2021, 10:23 PMfun getLocation(): String? {
if (!scanStacktrace) return null
return try {
throw Exception()
} catch (e: Exception) {
val firstElement = e.stackTrace.first()
e.stackTrace.firstOrNull { element ->
element.fileName != firstElement.fileName
&& element.lineNumber > 0
&& element.className.startsWith("moe.nikky.")
}?.let { element ->
"${element.fileName}:${element.lineNumber}"
}.also {
if (it == null) e.printStackTrace()
}
}
}
Nikky
10/17/2021, 10:24 PMcallsite: StackTraceElement?
to LogEvent
and a flag in the log configuration whether to grab the stacktraces or not.. creating sand catching stacktraces is (relatively) expensive
currently i store in in the log context and remove it before printing the items, not so clean.. but i did not want to fork Klogging (yet)
val CUSTOM_RENDERER: RenderString = { e: LogEvent ->
val loggerOrFile = e.items["file"]?.let { ".($it)" } ?: e.logger
val time = e.timestamp.localString.padEnd(29, '0')
val message = "$time ${e.level} $loggerOrFile : ${e.evalTemplate()}"
val cleanedItems = e.items - "file"
val maybeItems = if (cleanedItems.isNotEmpty()) " : $cleanedItems" else ""
val maybeStackTrace = if (e.stackTrace != null) "\n${e.stackTrace}" else ""
message + maybeItems + maybeStackTrace
}
charleskorn
10/18/2021, 12:42 AMOliver.O
10/18/2021, 3:40 PMThrowable()
in combination with the fully qualified caller class name for location information:
• log4j source
• logback source
As logback is supposedly better at figuring out line numbers, it might be worth a look.
Browsers offer a similar capability via the Error()
object: https://stackoverflow.com/a/2343382/2529022. However, as such stack traces normally refer to minified code they will require further decoding via source maps.Oliver.O
10/18/2021, 3:43 PMNikky
10/19/2021, 3:41 PMNikky
10/19/2021, 3:47 PMthread.getStrackTrace()
fun getLocation(): String? {
if (!scanStacktrace) return null
val stackTrace = Thread.currentThread().stackTrace.drop(1)
val firstElement = stackTrace.first()
return stackTrace.firstOrNull { element ->
element.fileName != firstElement.fileName
&& element.lineNumber > 0
&& element.className.startsWith("moe.nikky.")
}?.let { element ->
"${element.fileName}:${element.lineNumber}"
}.also {
if (it == null) System.err.println(stackTrace.joinToString("\n at "))
}
}
this code seems to be more performantOliver.O
10/19/2021, 3:49 PM