data-dump/src/main/java/com/celnet/datadump/aspect/OperateLogAspect.java

166 lines
5.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.celnet.datadump.aspect;
import com.alibaba.fastjson.JSON;
import com.celnet.datadump.annotation.LogServiceAnnotation;
import com.celnet.datadump.entity.DataLog;
import com.celnet.datadump.service.DataLogService;
import com.google.common.collect.Lists;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
/**
*
*/
@Aspect
@Component
@Order(-1)
public class OperateLogAspect {
private static final Logger log = LoggerFactory.getLogger(OperateLogAspect.class);
@Autowired
private DataLogService dataLogService;
@Pointcut(value = "@annotation(com.celnet.datadump.annotation.LogServiceAnnotation)")
public void operateLogAspectPoint(){
}
@Around("operateLogAspectPoint()")
public Object around(ProceedingJoinPoint joinPoint) {
//开始时间
Date startTime = new Date();
//日志注解
LogServiceAnnotation logServiceAnno = null;
//request请求
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
//捕获请求参数
Object[] args = joinPoint.getArgs();
//结果
Object result = null;
try {
//获取注解
logServiceAnno = getAnnotationLog(joinPoint);
//执行程序
result = joinPoint.proceed();
//初始化日志记录
DataLog dataLog = initializeOperateLog(joinPoint,args,startTime,logServiceAnno,result,null);
//保存日志
dataLogService.save(dataLog);
} catch (Throwable throwable) {
//初始化日志记录
DataLog dataLog = initializeOperateLog(joinPoint,args,startTime,logServiceAnno,null,throwable);
//保存日志
dataLogService.save(dataLog);
log.error("日志拦截异常:"+throwable);
}
return result;
}
/**
* 初始化操作日志
* @param joinPoint 节点
*/
private DataLog initializeOperateLog(ProceedingJoinPoint joinPoint , Object[] args , Date startTime , LogServiceAnnotation logServiceAnno , Object result , Throwable throwable) {
if(logServiceAnno == null){
return null;
}
//request请求
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
//ip
String ip = request.getHeader("HTTP_X_FORWARDED_FOR");
//请求参数
Object[] arguments = new Object[args.length];
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof ServletRequest || args[i] instanceof ServletResponse || args[i] instanceof MultipartFile) {
//ServletRequest不能序列化从入参里排除否则报异常java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
//ServletResponse不能序列化 从入参里排除否则报异常java.lang.IllegalStateException: getOutputStream() has already been called for this response
continue;
}
arguments[i] = args[i];
}
String argStr = arguments.length <= 0 ? "" : JSON.toJSONString(arguments);
//响应结果
String resultStr = result == null ? "" : JSON.toJSONString(result);
//异常信息
String exceptionStr = throwable == null ? "" : JSON.toJSONString(throwable.getMessage());
//结束时间
Date endTime = new Date();
DataLog dataLog = new DataLog();
dataLog.setIp(ip);
dataLog.setStartTime(startTime);
dataLog.setEndTime(endTime);
if(resultStr.contains("200")){
dataLog.setCode("200");
dataLog.setStatus("成功");
dataLog.setMessage(resultStr);
} else {
dataLog.setCode("500");
dataLog.setStatus("失败");
dataLog.setMessage(exceptionStr);
}
dataLog.setRequestUrl(request.getRequestURI());
dataLog.setRequestType(logServiceAnno.operateType());
dataLog.setRequestData(argStr);
dataLog.setRequestMethod(logServiceAnno.remark());
return dataLog;
}
/**
* 获取注解
* @param joinPoint 节点
* @return 结果
*/
private LogServiceAnnotation getAnnotationLog(ProceedingJoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
if (method != null) {
// 拿到自定义注解中的信息
LogServiceAnnotation annotation = method.getAnnotation(LogServiceAnnotation.class);
System.out.println("打印注解信息:"+ JSON.toJSONString(annotation));
return annotation;
}
return null;
}
}