需求:
- 我们在排查线上可复现问题时,如果出现异常可能还需要登陆服务器去翻日志.
解决:
- java Code
@RequestMapping("UserController/register")
@ResponseBody
public Map<Object, Object> register(String email, String userName, String password) {
HashMap<Object, Object> result = MethodReturn.returnMapDefault();
try {
// result = userService.register(email, userName, password);
int a = 1 / 0;
} catch (Exception e) {
e.printStackTrace();
/*********************************未便于展示,暂为封装******************************************/
Map<Object, String> exceptionStatckTrace = new LinkedHashMap<>();
exceptionStatckTrace.put(0, e.getClass().getName() + ": " + e.getMessage());
StackTraceElement[] stackTrace = e.getStackTrace();
int length = stackTrace.length;
StackTraceElement obj = null;
StringBuilder fileName = null;
for (int i = 0; i < length; i++) {
obj = stackTrace[i];
fileName = new StringBuilder();
if (obj.getFileName() != null) {
fileName.append(obj.getFileName());
if (obj.getLineNumber() != -1) {
fileName.append(": " + obj.getLineNumber());
}
} else {
fileName.append("Unknown Source");
}
exceptionStatckTrace.put(i + 1, obj.getClassName() + "." + obj.getMethodName() + "(" + fileName.toString() + ")");
}
/***************************************************************************/
result.put("exception", exceptionStatckTrace);
} finally {
return result;
}
}
- 效果
2017年8月13日 15:19:18
时间: 2024-11-09 23:38:54