|
|
|
@ -16,6 +16,12 @@ import java.time.LocalDateTime; |
|
|
|
|
|
|
|
/** |
|
|
|
* 日志切面 |
|
|
|
* |
|
|
|
* 异常拦截: |
|
|
|
* 会将错误信息都放到internalMsg中 |
|
|
|
* 如果是要提示给用户的异常,则原样转化为result,return |
|
|
|
* 如果是内部异常,则转化为服务器开小差,然后return |
|
|
|
* |
|
|
|
* @Author wxz |
|
|
|
* @Description |
|
|
|
**/ |
|
|
|
@ -41,45 +47,68 @@ public abstract class BaseRequestLogAspect { |
|
|
|
protected Object proceed(ProceedingJoinPoint point, HttpServletRequest request) throws Throwable { |
|
|
|
String requestURI = request.getRequestURI(); |
|
|
|
|
|
|
|
long id = Thread.currentThread().getId(); |
|
|
|
Object result = null; |
|
|
|
Exception exception = null; |
|
|
|
long threadId = Thread.currentThread().getId(); |
|
|
|
Object result; |
|
|
|
LocalDateTime startTime = LocalDateTime.now(); |
|
|
|
|
|
|
|
try { |
|
|
|
Object[] args = point.getArgs(); |
|
|
|
log.info(">>>>>>>>请求信息>>>>>>>>:线程ID:{},url:{},请求参数:{}", id, requestURI, objectsToString(args)); |
|
|
|
log.info(">>>>>>>>请求信息>>>>>>>>:线程ID:{},url:{},请求参数:{}", threadId, requestURI, objectsToString(args)); |
|
|
|
result = point.proceed(); |
|
|
|
resultInfoLog(threadId, getExecPeriod(startTime), result); |
|
|
|
} catch (RenException e) { |
|
|
|
exception = e; |
|
|
|
result = handleRenException(e); |
|
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
|
} catch (ValidateException e) { |
|
|
|
exception = e; |
|
|
|
result = handleValidateException(e); |
|
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
|
} catch (DuplicateKeyException e) { |
|
|
|
exception = e; |
|
|
|
result = handlerDuplicateKeyException(e); |
|
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
|
} catch (RuntimeException re) { |
|
|
|
exception = re; |
|
|
|
result = handlerRuntimeException(re); |
|
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, re.getMessage(), ExceptionUtils.getErrorStackTrace(re)); |
|
|
|
} catch (Exception e) { |
|
|
|
exception = e; |
|
|
|
result = handlerException(e); |
|
|
|
} finally { |
|
|
|
LocalDateTime endTime = LocalDateTime.now(); |
|
|
|
long execTimeMillis = Duration.between(startTime, endTime).toMillis(); |
|
|
|
|
|
|
|
if (exception != null) { |
|
|
|
log.error("<<<<<<<<异常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}, 异常信息:{}", |
|
|
|
id, execTimeMillis, result == null ? result : result.toString(), ExceptionUtils.getErrorStackTrace(exception)); |
|
|
|
} else { |
|
|
|
log.info("<<<<<<<<正常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}", |
|
|
|
id, execTimeMillis, result == null ? result : result.toString()); |
|
|
|
} |
|
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* info日志 |
|
|
|
* @param threadId |
|
|
|
* @param execTimeMillis |
|
|
|
* @param result |
|
|
|
*/ |
|
|
|
private void resultInfoLog(Long threadId, Long execTimeMillis, Object result) { |
|
|
|
log.info("<<<<<<<<正常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}", |
|
|
|
threadId, execTimeMillis, result == null ? result : result.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 异常信息 |
|
|
|
* @param threadId |
|
|
|
* @param execTimeMillis |
|
|
|
* @param result |
|
|
|
* @param exceptionMsg |
|
|
|
* @param exceptionDetail |
|
|
|
*/ |
|
|
|
private void resultErrorLog(Long threadId, Long execTimeMillis, Object result, String exceptionMsg, String exceptionDetail) { |
|
|
|
log.error("<<<<<<<<异常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}, 异常信息:{}, 堆栈信息:{}", |
|
|
|
threadId, execTimeMillis, result == null ? result : result.toString(), exceptionMsg, exceptionDetail); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 计算执行周期 |
|
|
|
* @param startTime |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private Long getExecPeriod(LocalDateTime startTime) { |
|
|
|
LocalDateTime endTime = LocalDateTime.now(); |
|
|
|
return Duration.between(startTime, endTime).toMillis(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 处理Exception |
|
|
|
* @param e |
|
|
|
@ -87,7 +116,7 @@ public abstract class BaseRequestLogAspect { |
|
|
|
*/ |
|
|
|
private Result handlerException(Exception e) { |
|
|
|
Result result=new Result().error(); |
|
|
|
result.setData(e.getMessage()); |
|
|
|
result.setInternalMsg(e.getMessage()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@ -105,7 +134,7 @@ public abstract class BaseRequestLogAspect { |
|
|
|
*/ |
|
|
|
private Result handlerRuntimeException(RuntimeException ex) { |
|
|
|
Result result=new Result().error(); |
|
|
|
result.setData(ex.getMessage()); |
|
|
|
result.setInternalMsg(ex.getMessage()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
@ -128,12 +157,14 @@ public abstract class BaseRequestLogAspect { |
|
|
|
*/ |
|
|
|
private Result handleRenException(RenException e) { |
|
|
|
if (e.getCode() > 8000) { |
|
|
|
// 原样返回
|
|
|
|
Result result=new Result().error(e.getCode()); |
|
|
|
result.setData(e.getMsg()); |
|
|
|
result.setInternalMsg(e.getMsg()); |
|
|
|
return result; |
|
|
|
} |
|
|
|
// 转化成服务器开小差...
|
|
|
|
Result result=new Result().error(); |
|
|
|
result.setData(e.getMsg()); |
|
|
|
result.setInternalMsg(e.getMsg()); |
|
|
|
//result.setMsg(e.getMsg());
|
|
|
|
return result; |
|
|
|
} |
|
|
|
|