引入依赖
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
监控类代码,目前只做了控制台输出,后续自己切换到文本或者数据库中
package com.java.core.web.config;
import com.alibaba.fastjson.JSONObject;
import com.java.core.core.vo.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
//处理AOP切面请求日志
@Slf4j
@Component
@Aspect
public class DBLogConfig {
//方法执行前-要监控的请求包地址
@Before("execution(public * com.java.core.web.contrller.*.*(..))")
public void requestBefore(JoinPoint joinPoint) throws Exception {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
StringBuilder requestLog = new StringBuilder();
Signature signature = joinPoint.getSignature();
requestLog.append("请求信息:").append("URL = {").append(request.getRequestURI()).append("},\t")
.append("请求方式 = {").append(request.getMethod()).append("},\t")
.append("请求IP = {").append(request.getRemoteAddr()).append("},\t")
.append("类方法 = {").append(signature.getDeclaringTypeName()).append(".")
.append(signature.getName()).append("},\t");
// 处理请求参数
String[] paramNames = ((MethodSignature) signature).getParameterNames();
Object[] paramValues = joinPoint.getArgs();
int paramLength = null == paramNames ? 0 : paramNames.length;
if (paramLength == 0) {
requestLog.append("请求参数 = {} ");
} else {
requestLog.append("请求参数 = [");
for (int i = 0; i < paramLength - 1; i++) {
requestLog.append(paramNames[i]).append("=").append(JSONObject.toJSONString(paramValues[i])).append(",");
}
requestLog.append(paramNames[paramLength - 1]).append("=").append(JSONObject.toJSONString(paramValues[paramLength - 1])).append("]");
}
log.info(requestLog.toString());
}
//方法执行后-要监控的请求包地址
@AfterReturning(returning = "result", pointcut = "execution(public * com.java.core.web.contrller.*.*(..))")
public void requestAfter(HttpResult result) throws Exception {
log.info("请求结果:" +JSONObject.toJSONString(result));
}
}
请求后的输出情况
?
|