Scala: print a stack trace in my Scalatra app

南笙酒味 提交于 2019-12-03 11:32:46

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.

Use ExceptionUtils from Apache Commons Lang:

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

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

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.

Kelsey Gilmore-Innis

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!