aop日志功能完善
This commit is contained in:
@@ -1,18 +1,30 @@
|
||||
package com.macro.mall.component;
|
||||
|
||||
import com.macro.mall.bo.WebLog;
|
||||
import com.macro.mall.util.JsonUtils;
|
||||
import com.macro.mall.util.RequestUtil;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.Signature;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 统一日志处理切面
|
||||
@@ -30,19 +42,69 @@ public class WebLogAspect {
|
||||
@Before("webLog()")
|
||||
public void doBefore(JoinPoint joinPoint) throws Throwable {
|
||||
startTime.set(System.currentTimeMillis());
|
||||
//获取当前请求对象
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
//记录请求信息(可以记录到数据库中)
|
||||
String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
|
||||
String args = Arrays.toString(joinPoint.getArgs());
|
||||
LOGGER.info("HTTP URL:{}; HTTP_METHOD:{}; IP:{}; CLASS_METHOD:{}; ARGS:{}",
|
||||
request.getRequestURL(), request.getMethod(), request.getRemoteAddr(), classMethod, args);
|
||||
}
|
||||
|
||||
@AfterReturning(value = "webLog()", returning = "ret")
|
||||
public void doAfterReturning(Object ret) throws Throwable {
|
||||
long costTime = System.currentTimeMillis() - startTime.get();
|
||||
LOGGER.info("RESPONSE:{}; COST_TIME:{}ms", ret, costTime);
|
||||
}
|
||||
|
||||
@Around("webLog()")
|
||||
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
//获取当前请求对象
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
//记录请求信息(可以记录到数据库中)
|
||||
WebLog webLog = new WebLog();
|
||||
Object result = joinPoint.proceed();
|
||||
Signature signature = joinPoint.getSignature();
|
||||
MethodSignature methodSignature = (MethodSignature) signature;
|
||||
Method method = methodSignature.getMethod();
|
||||
if (method.isAnnotationPresent(ApiOperation.class)) {
|
||||
ApiOperation log = method.getAnnotation(ApiOperation.class);
|
||||
webLog.setDescription(log.value());
|
||||
}
|
||||
long endTime = System.currentTimeMillis();
|
||||
webLog.setBasePath(RequestUtil.getBasePath(request));
|
||||
webLog.setIp(request.getRemoteUser());
|
||||
webLog.setMethod(request.getMethod());
|
||||
webLog.setParameter(getParameter(method, joinPoint.getArgs()));
|
||||
webLog.setResult(result);
|
||||
webLog.setSpendTime((int) (endTime - startTime.get()));
|
||||
webLog.setStartTime(startTime.get());
|
||||
webLog.setUri(request.getRequestURI());
|
||||
webLog.setUrl(request.getRequestURL().toString());
|
||||
LOGGER.info("{}", JsonUtils.objectToJson(webLog));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据方法和传入的参数获取请求参数
|
||||
*/
|
||||
private Object getParameter(Method method, Object[] args) {
|
||||
List<Object> argList = new ArrayList<>();
|
||||
Parameter[] parameters = method.getParameters();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
|
||||
if (requestBody != null) {
|
||||
argList.add(args[i]);
|
||||
}
|
||||
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
|
||||
if (requestParam != null) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
String key = parameters[i].getName();
|
||||
if (!StringUtils.isEmpty(requestParam.value())) {
|
||||
key = requestParam.value();
|
||||
}
|
||||
map.put(key, args[i]);
|
||||
argList.add(map);
|
||||
}
|
||||
}
|
||||
if (argList.size() == 0) {
|
||||
return null;
|
||||
} else if (argList.size() == 1) {
|
||||
return argList.get(0);
|
||||
} else {
|
||||
return argList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user