package com.tongbo.api.system.config;
import com.alibaba.fastjson2.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
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.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
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.Arrays;
@Aspect
@Component
public class HttpLogAspect {
private final static Logger log = LoggerFactory.getLogger(HttpLogAspect.class);
//所有的controller之前
/**
* 生效条件:
* 1.包范围
* 2.类上加了注解 Controller 或者 RestController
*/
@Pointcut("(execution(* com.tongbo.api.system.api..*.*(..)) || execution(* com.tongbo.api.system.api..*.*(..)) ) && (@within(org.springframework.stereotype.Controller) || @within(org.springframework.web.bind.annotation.RestController))")
public void logPointCut() {
}
@Around("logPointCut()")
public Object doBefore(ProceedingJoinPoint joinPoint) {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
// 打印请求相关参数
log.info("========================================== Start ==========================================");
String methodName = joinPoint.getSignature().toShortString();
// 记录下请求内容
log.info("Request URL : " + request.getRequestURL().toString());
log.info("Http Method : " + request.getMethod());
// 获取真实的ip地址
log.info("Request Addr : " + request.getRemoteAddr());
log.info("Class Method : " + joinPoint.getSignature().getDeclaringTypeName() + "."
+ joinPoint.getSignature().getName());
Object[] joinPointArgs = joinPoint.getArgs();
LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
String[] paramNames = u.getParameterNames(method);
JSONObject paramJsonObj = new JSONObject();
for (int i = 0; i < joinPointArgs.length; i++) {
Object joinPointArg = joinPointArgs[i];
//特殊类型 绕过
if (joinPointArg instanceof ServletRequest
|| joinPointArg instanceof ServletResponse
|| joinPointArg instanceof MultipartFile) {
continue;
}
String paramName = paramNames[i];
paramJsonObj.put(paramName, JSONObject.from(joinPointArg));
}
log.info("Request Args : " + paramJsonObj);
Object result;
long startTime = System.currentTimeMillis();
try {
result = joinPoint.proceed();
} catch (Throwable e) {
log.error("{} error, exception={}", methodName, e);
throw new RuntimeException(e);
}
long endTime = System.currentTimeMillis();
// 打印出参
log.info("Response Args: {}", JSONObject.toJSONString(result));
// 执行耗时
log.info("Time Consuming: {} ms", endTime - startTime);
log.info("========================================== End ============================================");
return result;
}
}
请求日志记录
于 2022-11-15 10:43:07 首次发布