最近在项目中遇到了一个坑,jdk1.8
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
Object result = null;
TimeInterval timer = DateUtil.timer();
log.info("========================================== Start ==========================================");
try {
// 执行方法
result = point.proceed();
// 保存请求日志
saveRequestLog(point, null, timer, result);
} catch (Exception e) {
// 保存异常日志
e.printStackTrace();
saveRequestLog(point, e.getMessage(), timer, result);
}
log.info("========================================== End ============================================");
return result;
}
private void saveRequestLog(ProceedingJoinPoint point, String exeMsg, TimeInterval timer, Object result) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
// 打印请求 url
log.info("请求路径 : {}", request.getRequestURL().toString());
// 打印 Http method
log.info("HTTP Method : {}", request.getMethod());
// 打印调用 controller 的全路径以及执行方法
log.info("方法路径 : {}.{}", signature.getDeclaringTypeName(), signature.getName());
// 打印请求的 IP
log.info("IP : {}", ServletUtil.getClientIP(request));
// 打印请求入参
log.info("请求参数 : {}", JSONUtil.toJsonStr(point.getArgs()));
// 获取方法上LogFilter注解
LogFilter logFilter = method.getAnnotation(LogFilter.class);
String description = logFilter.description();
log.info("模块描述 : {}", description);
String type = logFilter.type();
log.info("模块类型 : {}", type);
log.info("捕获异常 : {}", exeMsg == null ? "无异常" : exeMsg);
......
}
因为是通过获取异常类中message来判断是否存在异常
getMessage()
然而,,,在一次本地调试中确实已经捕捉到了异常。但是日志却没有显示异常信息。这样就很奇怪了。。。
然后一步步dug。发现主要是NullPointerException这个异常出现了问题。
当捕捉到时NullPointerException后输出getMessage()。这个方法NullPointerException中没有重写,直接用的
至于detailMessage ,在实例化NullPointerException (至于怎么实例化放进去的,希望有大神留言帮我讲下,,感谢感谢),放进去
就是null,,这个不是null的空字符串,,就是关键字null。想不想哭,,
所以这个取出来就是null,,这个坑看看怎么填。
然后在它祖宗Throwable中找呀,,发现了重写的toString();
发现可以呦,就算detailMessage为null,起码把类名拿到了。那就没问题。
嗯,那就换成toString()吧!