自定义注解标签:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16package com.hzizs.utils.log2; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) @Documented public @interface WebLog { /** * 日志描述信息 * * @return */ String description() default ""; }
定义切面类:
复制代码
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88package com.hzizs.utils.log2; import com.google.gson.Gson; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 public class WebLogAspect { private final static Logger logger = LoggerFactory.getLogger(WebLogAspect.class); /** 以 controller 包下定义的所有请求为切入点 */ @Pointcut("execution(public * com.hzizs.controller..*.*(..))") public void webLog() {} /** * 在切点之前织入 * @param joinPoint * @throws Throwable */ @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { // 开始打印请求日志 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 打印请求相关参数 logger.info("========================================== Start =========================================="); // // 打印请求 url // logger.info("URL : {}", request.getRequestURL().toString()); // // 打印 Http method // logger.info("HTTP Method : {}", request.getMethod()); // // 打印调用 controller 的全路径以及执行方法 // logger.info("Class Method : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); // // 打印请求的 IP // logger.info("IP : {}", request.getRemoteAddr()); // // 打印请求入参 // logger.info("Request Args : {}", new Gson().toJson(joinPoint.getArgs())); // //打印请求时间 // String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); // logger.info("Request time : {} ",format); logger.info("IP:{},URL: {}, HTTP Method:{},Class Method:{}.{},Request Args: {}",request.getRemoteAddr(), request.getRequestURL().toString(),request.getMethod(),joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(),new Gson().toJson(joinPoint.getArgs())); } /** * 在切点之后织入 * @throws Throwable */ @After("webLog()") public void doAfter() throws Throwable { logger.info("=========================================== End ==========================================="); // 每个请求之间空一行 logger.info(""); } /** * 环绕 * @param proceedingJoinPoint * @return * @throws Throwable */ @Around("webLog()") public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { try{ long startTime = System.currentTimeMillis(); Object result = proceedingJoinPoint.proceed(); // 打印出参 logger.info("Response Args : {}", new Gson().toJson(result)); // 执行耗时 logger.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime); return result; }catch (Exception e){ } return new Object(); } }
测试
在controller中方法上加自定义注解@WebLog
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22package com.mt.aoplog.controller; import com.mt.aoplog.log.WebLog; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author: 15aaa2 * @Description: * @Date: Created in 17:45 2021/12/28 */ @RequestMapping @RestController public class TestController { @GetMapping("/test") @WebLog public String test(String name,String age){ return "aaa"; } }
效果
最后
以上就是神勇小懒猪最近收集整理的关于自定义类实现控制台打印请求日志 自定义注解标签:定义切面类:测试的全部内容,更多相关自定义类实现控制台打印请求日志 自定义注解标签内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复