【问题标题】:How to log stack trace using kotlin.logging?如何使用 kotlin.logging 记录堆栈跟踪?
【发布时间】:2021-02-07 02:29:04
【问题描述】:

我正在使用 kotlin.logging 作为应用程序的日志记录框架。

我希望能够以更易读的方式(可能是 JSON 格式)记录堆栈跟踪:

我正在执行以下操作,但会导致格式非常冗长/不可读:

logger.error("Exception caught in handleIllegalArgumentsException: $e", e.printStackTrace())

仅记录 Throwable 异常时(见下文),它不显示堆栈跟踪:

logger.error("Exception caught in handleIllegalArgumentsException: $e") 

【问题讨论】:

    标签: spring-boot kotlin exception


    【解决方案1】:

    您应该能够使用以下方式获取堆栈跟踪。

    logger.error(e) {"Exception caught in handleIllegalArgumentsException}
    

    【讨论】:

    • 缺少结束报价
    【解决方案2】:

    对于通过谷歌搜索到达这里的人:

    由于 Kotlin 1.4 Throwable 有一个扩展方法,允许您将堆栈跟踪作为字符串获取,这基本上可以满足其他答案的建议。

    
    /**
     * Returns the detailed description of this throwable with its stack trace.
     *
     * The detailed description includes:
     * - the short description (see [Throwable.toString]) of this throwable;
     * - the complete stack trace;
     * - detailed descriptions of the exceptions that were [suppressed][suppressedExceptions] in order to deliver this exception;
     * - the detailed description of each throwable in the [Throwable.cause] chain.
     */
    @SinceKotlin("1.4")
    public actual fun Throwable.stackTraceToString(): String {
        val sw = StringWriter()
        val pw = PrintWriter(sw)
        printStackTrace(pw)
        pw.flush()
        return sw.toString()
    }
    

    这意味着原来的e.printStackTrace()只需更改为e.stackTraceToString()

    像这样:

    logger.error("Exception caught in handleIllegalArgumentsException: ${e.stackTraceToString()}") 
    

    【讨论】:

      【解决方案3】:

      Stacktrace 始终是多行的,而传统的日志记录消息应该是单行的。 所以你需要先save it to a String variable,然后以某种方式替换换行符号(例如用|符号):

      logger.error {
          val stacktrace = StringWriter().also { e.printStackTrace(PrintWriter(it)) }.toString().trim()
          val singleLineStackTrace = stacktrace.lines().map { it.trim() }.joinToString(separator = "|")
          "Exception caught in handleIllegalArgumentsException: $singleLineStackTrace"
      }
      

      那么日志将如下所示:

      [main] ERROR Logging - Exception caught in handleIllegalArgumentsException: java.lang.IllegalArgumentException: !!!!|at LoggingKt.main(logging.kt:12)|at LoggingKt.main(logging.kt)
      

      【讨论】:

        【解决方案4】:

        如果您希望记录器打印异常堆栈跟踪:

        try {
            ...
        } catch (e: Exception) {
            logger.error("Exception caught:", e)
        }
        

        如果您想要字符串本身(用于进一步操作),您可以使用@Михаил Нафталь 建议的内容:

        try {
            ...
        } catch (e: Exception) {
            val stacktrace = StringWriter().also { e.printStackTrace(PrintWriter(it)) }.toString().trim()
            logger.error("Exception caught: $stacktrace")
        }
        

        这两种方法都会在日志中打印整个堆栈跟踪,例如:

        16:41:50.528 [main] ERROR org.myorg.MyClass - Exception caught:
        java.lang.IllegalArgumentException: Argument X was not allowed.
            at org.myorg.MyClass.func (MyClass.kt:124)
            at java.base/java.lang.Thread.run(Thread.java:834)
            etc...
        

        我知道它不是回答问题文本中的问题,而是回答问题标题。由于这篇文章是我在谷歌上搜索如何使用 Kotlin 记录堆栈跟踪时得到的第一个建议,我认为这可能对其他人有所帮助。

        【讨论】:

          猜你喜欢
          • 2011-04-06
          • 1970-01-01
          • 1970-01-01
          • 2014-11-01
          • 1970-01-01
          • 2013-11-16
          • 2012-03-22
          • 1970-01-01
          • 2015-10-03
          相关资源
          最近更新 更多