预期效果
之前尝试调试成功的 若依 项目,在访问index页面并登陆成功后,后台日志会有如下显示:
这是通过AOP切面的形式添加的功能
我们目标将如下参数打印在日志中:
- URL: 请求接口地址;
- HTTP Method: 请求的方法,是 POST, GET, 还是 DELETE 等;
- Class Method: 对应 Controller 的全路径以及调用的哪个方法;
- IP: 请求 IP 地址;
- Request Args: 请求入参,以 JSON 格式输出;
上手实现
在此我们使用了Log4j2作为日志工具,配合Lombok插件
配置AOP切面 和 日志封装类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package com.ray.blog.aspect;
import lombok.AllArgsConstructor; import lombok.ToString; import lombok.extern.log4j.Log4j2; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
@Aspect @Component @Log4j2 public class LogAspect { @Pointcut("execution(* com.ray.blog.web.*.*(..))") public void log(){}
@Before("log()") public void doBefore(JoinPoint joinPoint){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); String url = request.getRequestURL().toString(); String ip = request.getRemoteAddr(); String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); RequestLog requestLog = new RequestLog(url, ip, classMethod, args); log.info("Request: {}", requestLog);
} @After("log()") public void doAfter(){
} @AfterReturning(returning = "result", pointcut = "log()") public void doAfterReturn(Object result){
}
@AllArgsConstructor @ToString private class RequestLog{ private String url; private String ip; private String classMethod; private Object[] args; } }
|
配置Servlet Controller:
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.ray.blog.web;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable;
@Controller public class IndexController { @GetMapping("/{a}/{b}") public String index(@PathVariable String a, @PathVariable String b){ return "index"; } }
|
例如当我访问127.0.0.1:8080/ray/123456
时
面板会产生如下日志,表示AOP已经正常运行
1 2
| 20:18:39.973 [http-nio-8080-exec-4] INFO com.ray.blog.aspect.LogAspect - Request: LogAspect.RequestLog(url=http://127.0.0.1:8080/ray/123456, ip=127.0.0.1, classMethod=com.ray.blog.web.IndexController.index, args=[ray, 123456])
|