Scala:在我的Scalatra应用程序中打印堆栈跟踪 [英] Scala: print a stack trace in my Scalatra app

查看:219
本文介绍了Scala:在我的Scalatra应用程序中打印堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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")
  }
}

错误处理程序本身是Scalatra特定的,但是我很确定我正在寻找的答案可以使用任何原始的Scala技术来解决.我现在可以做些什么来抓住stacktrace吗?我不确定请求是否与错误处理程序在同一线程上,否则可能会有一些答案. e.getStackTrace()给了我[Ljava.lang.StackTraceElement;@1f6b6954

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

在这里打印出堆栈跟踪的最佳方法是什么,以便我可以对其进行记录和检查以纠正可怕代码中的错误?

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?

推荐答案

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

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.

这篇关于Scala:在我的Scalatra应用程序中打印堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆