【发布时间】:2017-07-27 09:14:52
【问题描述】:
我正在尝试在基于 Spring Boot 的 Web 服务中记录异常。
所以我使用了 GlobalExceptionHandler 我的代码:
@ControllerAdvice
@RestController
public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = Exception.class)
public String handleException(Exception e){
System.out.println("Ankit == "+e.getMessage());
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
System.out.println(errors.toString());
return e.getMessage();
}
}
代码运行良好。我想要的是异常细节。我的意思是发生异常的代码?文件名/行?还是我必须解析堆栈跟踪?我的意思是 spring boot 一定是为此想到了什么?
【问题讨论】:
-
Afaik spring 没有任何东西可以解析异常以找到发生异常的行。最好的办法是遍历堆栈跟踪,检查包名称并获取您感兴趣的包名称(线路信息也将包含在其中,以便您可以随意形成消息)。
-
您应该使用一些日志框架来捕获错误日志,而不是 sysout 语句。就跟踪而言,您必须使用 stacktrace 手动进行。
标签: spring-boot exception-handling