話不多說,直接上代碼:
/**
* 自定義日誌記錄
*/
@Documented
@Target({ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface OptionalLog {
/**
* 日誌模塊
*
* @return
*/
String module() default "";
/**
* 記錄日誌的操作類型,日誌說明
*/
String operateType() default "";
}
定義切面:
/**
* 切面日誌
*
* @author lpz
*/
@Slf4j
@Aspect
@Component
public class OptionalLogAop {
// 配置接入點,即爲所要記錄的action操作目錄
@Pointcut("execution(* com.cmcc.hy.portray.api.controller..*.*(..))")
private void controllerAspect() {
}
@Around("controllerAspect()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// 攔截的放參數類型
Signature sign = joinPoint.getSignature();
if (!(sign instanceof MethodSignature)) {
log.error("該註解只能用於方法, OptionalLog only use for method... {}", sign);
// throw new IllegalArgumentException("該註解只能用於方法");
}
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method currentMethod = methodSignature.getMethod();
// 獲取方法(此爲自定義註解)
OptionalLog annot = currentMethod.getAnnotation(OptionalLog.class);
if (Objects.isNull(annot)) {
return joinPoint.proceed();
}
// 攔截的方法名稱。當前正在執行的方法
String methodName = methodSignature.getName();
//
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
// 從session獲取用戶名
String userName = (String) request.getSession().getAttribute(SessionConstants.SESSION_KEY_USERNAME);
// 獲取系統當前時間
String recordTime = DateUtil.formatDateToStr(new Date(), DateUtil.FORMATSTR_YYYY_MM_DD_HHMMSS);
// 獲取訪問真實IP
String ipAddress = IpUtil.getRequestIp();
//獲取請求路徑
String requestURI = request.getRequestURI();
// 獲取註解的modules 設爲操作模塊, 設爲執行方法
String module = annot.module();
String operateType = annot.operateType();
//接受客戶端的數據
StringBuilder paramsBuild = new StringBuilder();
Map<String, String[]> maps = request.getParameterMap();
maps.forEach((s, strings) -> {
for (String string : strings) {
paramsBuild.append(s).append("-").append(string).append(",");
}
});
String params = paramsBuild.toString();
params = StringUtil.isBlank(params) ? params : params.substring(0, params.length() - 1);
Object[] arguments = joinPoint.getArgs();
for (Object argument : arguments) {
System.out.println(argument);
}
// 執行目標方法,獲得返回值。response
Object result = null;
try {
result = joinPoint.proceed();
} catch (Exception e) {
log.error("處理響應結果異常response exception, {}", e.getMessage(), e);
} finally {
log.info("OptionalLog record... userName:{}, requestURI:{}, params:{}, module:{}, operateType:{}, ip:{}, result:{}, recordTime:{}",
userName, requestURI, params, module, operateType, ipAddress, result, recordTime);
}
return result;
}
}
添加註解:
/**
* 導出模型分析結果excel
*/
@OptionalLog(module = "操作日誌", operateType = "導出模型分析結果")
@RequestMapping(value = "/exportModel", method = RequestMethod.GET)
@ApiOperation(value = "導出模型分析結果", notes = "導出模型分析結果")
public String exportModel(Long id, HttpServletResponse response) {
return modelResultInfoService.exportModel(id, response);
}
打印形式:
2019-12-05 10:45:56.334 INFO 64116 [io-8800-exec-28] hy.portray.api.common.aop.OptionalLogAop 95 : OptionalLog record... userName:tscmail, requestURI:/customgroup/query/export, params:id-79, module:操作日誌, operateType:導出客羣數據, ip:172.18.34.133, result:嘿嘿嘿.xls, recordTime:2019-12-05 10:45:54
2019-12-05 10:45:56.334 INFO 64116 [io-8800-exec-28] hy.portray.api.common.aop.OptionalLogAop 95 : OptionalLog record... userName:tscmail, requestURI:/customgroup/query/export, params:id-79, module:操作日誌, operateType:導出客羣數據, ip:172.18.34.133, result:嘿嘿嘿.xls, recordTime:2019-12-05 10:45:54