gpt4 book ai didi

java - 在使用 SLF4J 进行日志记录之前, "trimming"异常堆栈跟踪的最佳实践是什么?

转载 作者:太空宇宙 更新时间:2023-11-04 11:44:20 27 4
gpt4 key购买 nike

注意:我正在 Kotlin 中编码但我认为 Kotlin 或 Java 的答案可能是相同的

我正在构建a web framework我想向用户报告错误以及与用户代码中错误所在位置相关的堆栈跟踪(这有点复杂,因为这些错误实际上是由 Web 框架“远程控制”的 Web 浏览器生成的 JavaScript 错误)。

我可以创建 Throwable(),但是堆栈跟踪将在我的框架代码中包含大量多余的 StackTraceElements,无论是在与代码用户相关的 StackTraceElements 之前还是之后,如下所示:

[nioEventLoopGroup-3-2] ERROR com.github.sanity.kweb.KWeb - JavaScript error: 'Invalid left-hand side expression in postfix operation'
Caused by executing: '1++2':
at com.github.sanity.kweb.KWeb.execute(KWeb.kt:136)
at com.github.sanity.kweb.RootReceiver.execute(RootReceiver.kt:30)
at com.github.sanity.kweb.dom.element.Element.execute(Element.kt:24)
at com.github.sanity.kweb.demos.todo.TodoKt$main$1.invoke(todo.kt:15)
at com.github.sanity.kweb.KWeb$2.invoke(KWeb.kt:63)
at com.github.sanity.kweb.KWeb$2.invoke(KWeb.kt:28)
at org.wasabifx.wasabi.protocol.http.HttpRequestHandler.runHandlers(HttpRequestHandler.kt:123)
at org.wasabifx.wasabi.protocol.http.HttpRequestHandler.channelRead0(HttpRequestHandler.kt:82)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
at org.wasabifx.wasabi.core.HttpPipelineInitializer.applyHttp1Pipeline(HttpPipelineInitializer.kt:72)
at org.wasabifx.wasabi.core.HttpPipelineInitializer.initHttpPipeline(HttpPipelineInitializer.kt:51)
at org.wasabifx.wasabi.core.HttpPipelineInitializer.channelRead0(HttpPipelineInitializer.kt:32)
at org.wasabifx.wasabi.core.HttpPipelineInitializer.channelRead0(HttpPipelineInitializer.kt:17)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:129)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:610)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:551)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:465)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:437)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144)
at java.lang.Thread.run(Thread.java:745)

因此,我正在过滤 StackTraceElements 以删除那些与用户无关的元素(主要是通过类名和包进行过滤),然后将堆栈跟踪以标准堆栈跟踪格式附加到 StringBuilder,然后将其作为字符串(带有行分隔符)记录到 SLF4J。

val disregardClassPrefixes = listOf(KWeb::class.jvmName, RootReceiver::class.jvmName, Element::class.jvmName, "org.wasabifx", "io.netty", "java.lang")
debugInfo.throwable.stackTrace
.filter { ste -> ste.lineNumber >= 0 && !disregardClassPrefixes.any { ste.className.startsWith(it) } }
.forEach { stackTraceElement ->
logStatementBuilder.appendln(" at ${stackTraceElement.className}.${stackTraceElement.methodName}(${stackTraceElement.fileName}:${stackTraceElement.lineNumber})")
}

这给了我们更好的结果:

[nioEventLoopGroup-3-2] ERROR com.github.sanity.kweb.KWeb - JavaScript error: 'Invalid left-hand side expression in postfix operation'
Caused by executing: '1++2':
at com.github.sanity.kweb.demos.todo.TodoKt$main$1.invoke(todo.kt:15)

但是,这种方法有点笨拙,我想知道是否有更好的方法来做到这一点。

最佳答案

唯一有效的答案是“不”。

您无法提前知道堆栈跟踪的哪些部分是相关的,并且如果删除或修剪任何内容,您可能会删除允许您进行故障排除的一条数据。

重复一遍:不要这样做。堆栈跟踪很难看,但很有必要。还要抵制删除“原因”部分的冲动。它们的存在是有原因的,并且通常包含“真正的”异常。

关于java - 在使用 SLF4J 进行日志记录之前, "trimming"异常堆栈跟踪的最佳实践是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42471992/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com