|
@ -1,10 +1,12 @@ |
|
|
package com.epmet.commons.tools.aspect; |
|
|
package com.epmet.commons.tools.aspect; |
|
|
|
|
|
|
|
|
|
|
|
import com.epmet.commons.tools.constant.AppClientConstant; |
|
|
import com.epmet.commons.tools.exception.ErrorCode; |
|
|
import com.epmet.commons.tools.exception.ErrorCode; |
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
import com.epmet.commons.tools.exception.ValidateException; |
|
|
import com.epmet.commons.tools.exception.ValidateException; |
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.aspectj.lang.ProceedingJoinPoint; |
|
|
import org.aspectj.lang.ProceedingJoinPoint; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.Logger; |
|
|
import org.slf4j.LoggerFactory; |
|
|
import org.slf4j.LoggerFactory; |
|
@ -13,6 +15,7 @@ import org.springframework.dao.DuplicateKeyException; |
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
import java.time.Duration; |
|
|
import java.time.Duration; |
|
|
import java.time.LocalDateTime; |
|
|
import java.time.LocalDateTime; |
|
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 日志切面 |
|
|
* 日志切面 |
|
@ -47,56 +50,65 @@ public abstract class BaseRequestLogAspect { |
|
|
protected Object proceed(ProceedingJoinPoint point, HttpServletRequest request) throws Throwable { |
|
|
protected Object proceed(ProceedingJoinPoint point, HttpServletRequest request) throws Throwable { |
|
|
String requestURI = request.getRequestURI(); |
|
|
String requestURI = request.getRequestURI(); |
|
|
|
|
|
|
|
|
long threadId = Thread.currentThread().getId(); |
|
|
// 获取事务流水号
|
|
|
|
|
|
String transactionSerial = request.getHeader(AppClientConstant.TRANSACTION_SERIAL_KEY); |
|
|
|
|
|
//if (StringUtils.isEmpty(transactionSerial)) {
|
|
|
|
|
|
// transactionSerial = UUID.randomUUID().toString();
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
// 将当前线程名称设置为事务流水号
|
|
|
|
|
|
if (!StringUtils.isEmpty(transactionSerial)) { |
|
|
|
|
|
Thread.currentThread().setName(transactionSerial); |
|
|
|
|
|
} |
|
|
Object result; |
|
|
Object result; |
|
|
LocalDateTime startTime = LocalDateTime.now(); |
|
|
LocalDateTime startTime = LocalDateTime.now(); |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
Object[] args = point.getArgs(); |
|
|
Object[] args = point.getArgs(); |
|
|
log.info(">>>>>>>>请求信息>>>>>>>>:线程ID:{},url:{},请求参数:{}", threadId, requestURI, objectsToString(args)); |
|
|
log.info(">>>>>>>>请求信息>>>>>>>>:事务流水号:{},url:{},请求参数:{}", transactionSerial, requestURI, objectsToString(args)); |
|
|
result = point.proceed(); |
|
|
result = point.proceed(); |
|
|
resultInfoLog(threadId, getExecPeriod(startTime), result); |
|
|
resultInfoLog(transactionSerial, getExecPeriod(startTime), result); |
|
|
} catch (RenException e) { |
|
|
} catch (RenException e) { |
|
|
result = handleRenException(e); |
|
|
result = handleRenException(e); |
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
} catch (ValidateException e) { |
|
|
} catch (ValidateException e) { |
|
|
result = handleValidateException(e); |
|
|
result = handleValidateException(e); |
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
} catch (DuplicateKeyException e) { |
|
|
} catch (DuplicateKeyException e) { |
|
|
result = handlerDuplicateKeyException(e); |
|
|
result = handlerDuplicateKeyException(e); |
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
} catch (RuntimeException re) { |
|
|
} catch (RuntimeException re) { |
|
|
result = handlerRuntimeException(re); |
|
|
result = handlerRuntimeException(re); |
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, re.getMessage(), ExceptionUtils.getErrorStackTrace(re)); |
|
|
resultErrorLog(transactionSerial, getExecPeriod(startTime), result, re.getMessage(), ExceptionUtils.getErrorStackTrace(re)); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
result = handlerException(e); |
|
|
result = handlerException(e); |
|
|
resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); |
|
|
} |
|
|
} |
|
|
return result; |
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* info日志 |
|
|
* info日志 |
|
|
* @param threadId |
|
|
* @param transactionSerial |
|
|
* @param execTimeMillis |
|
|
* @param execTimeMillis |
|
|
* @param result |
|
|
* @param result |
|
|
*/ |
|
|
*/ |
|
|
private void resultInfoLog(Long threadId, Long execTimeMillis, Object result) { |
|
|
private void resultInfoLog(String transactionSerial, Long execTimeMillis, Object result) { |
|
|
log.info("<<<<<<<<正常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}", |
|
|
log.info("<<<<<<<<正常响应<<<<<<<<:事务流水号:{}, 执行时长:{}ms, 响应数据:{}", |
|
|
threadId, execTimeMillis, result == null ? result : result.toString()); |
|
|
transactionSerial, execTimeMillis, result == null ? result : result.toString()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 异常信息 |
|
|
* 异常信息 |
|
|
* @param threadId |
|
|
* @param transactionSerial |
|
|
* @param execTimeMillis |
|
|
* @param execTimeMillis |
|
|
* @param result |
|
|
* @param result |
|
|
* @param exceptionMsg |
|
|
* @param exceptionMsg |
|
|
* @param exceptionDetail |
|
|
* @param exceptionDetail |
|
|
*/ |
|
|
*/ |
|
|
private void resultErrorLog(Long threadId, Long execTimeMillis, Object result, String exceptionMsg, String exceptionDetail) { |
|
|
private void resultErrorLog(String transactionSerial, Long execTimeMillis, Object result, String exceptionMsg, String exceptionDetail) { |
|
|
log.error("<<<<<<<<异常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}, 异常信息:{}, 堆栈信息:{}", |
|
|
log.error("<<<<<<<<异常响应<<<<<<<<:事务流水号:{}, 执行时长:{}ms, 响应数据:{}, 异常信息:{}, 堆栈信息:{}", |
|
|
threadId, execTimeMillis, result == null ? result : result.toString(), exceptionMsg, exceptionDetail); |
|
|
transactionSerial, execTimeMillis, result == null ? result : result.toString(), exceptionMsg, exceptionDetail); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|