Scala:在我的 Scalatra 应用程序中打印堆栈跟踪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18880126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:41:50  来源:igfitidea点击:

Scala: print a stack trace in my Scalatra app

scalaexception-handlingscalatra

提问by randombits

Seems like a fairly straight forward problem, but I'd like to log a stack trace when my top level error handler in Scalatra is triggered. I'm intentionally throwing an exception in one of my methods by doing something as trivial as:

似乎是一个相当直接的问题,但我想在 Scalatra 中的顶级错误处理程序被触发时记录堆栈跟踪。我通过做一些微不足道的事情,故意在我的一种方法中抛出异常:

throw new IllegalArgumentException

In the error handler, the code looks like the following:

在错误处理程序中,代码如下所示:

 error {
  case e => {
    val logger = LoggerFactory.getLogger(getClass)
    logger.info("an exception occurred: " + e.getStackTrace())
    logger.info("the request body is: " + request)
    NotFound("An error occurred, please contact support")
  }
}

The error handler itself is Scalatra specific, but I'm pretty sure the answer I'm looking for can be solved using any vanilla Scala technique. Is there something I can do at this point to grab the stacktrace? I'm not sure if the request is on the same thread as the error handler, otherwise there might be some answer there. e.getStackTrace()gives me [Ljava.lang.StackTraceElement;@1f6b6954

错误处理程序本身是特定于 Scalatra 的,但我很确定我正在寻找的答案可以使用任何普通的 Scala 技术来解决。在这一点上我可以做些什么来获取堆栈跟踪?我不确定该请求是否与错误处理程序在同一线程上,否则那里可能会有一些答案。e.getStackTrace()给我[Ljava.lang.StackTraceElement;@1f6b6954

What's the best way to get a stack trace here printed out so I can log and review it to fix errors in my terrible code?

在此处打印堆栈跟踪以便我可以记录和查看它以修复糟糕代码中的错误的最佳方法是什么?

采纳答案by James Adam

I think you want printStackTrace()rather than getStackTrace. If you are outputting to a log file, getMessage()may be helpful. Or you may try passing the whole exception object to the logger.

我认为你想要printStackTrace()而不是getStackTrace. 如果您要输出到日志文件,getMessage()可能会有所帮助。或者您可以尝试将整个异常对象传递给记录器。

回答by Fernando Correia

Use ExceptionUtilsfrom Apache Commons Lang:

使用Apache Commons Lang 中的ExceptionUtils

import org.apache.commons.lang3.exception.ExceptionUtils
(...)
logger.info("an exception occurred: " + ExceptionUtils.getStackTrace(e))

回答by dsantaolalla

If you use the standard logger: com.typesafe.scalalogging.Logger , the logger prints the stack trace for you.

如果您使用标准记录器: com.typesafe.scalalogging.Logger ,记录器会为您打印堆栈跟踪。

You can just use it this way:

你可以这样使用它:

import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory

try {     
    throw new Exception("test message")
} catch {
    case e:Exception => logger.error("Exception " , e)
}

There is already an overload which is accepting 2 parameters, String and Throwable.

已经有一个接受 2 个参数的重载,String 和 Throwable。

回答by Kelsey Gilmore-Innis

This questionhas several ways to convert an exception stack trace to a String. printStackTrace outputs to System.err unless you provide a writer.

这个问题有几种方法可以将异常堆栈跟踪转换为字符串。除非您提供编写器,否则 printStackTrace 会输出到 System.err。