Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to print Interface Log by AOP

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to print interface log in AOP, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

What is the use of interface log

In our daily development process, we can view some details of this interface through the interface log. For example, the IP of the client, the type of the client, the response time, the type of the request, the interface method of the request and so on, we can statistically analyze these data and extract the information we want.

How to get the interface log

Here, we use the AOP of the two killers of Spring. We define the pointcut in the Controller layer, then analyze the request object to obtain the interface information, and open a ThreadLocal to record the response time.

Notes on AOP

Aspect: define a class as a facet class.

Pointcut: define a pointcut.

Before: enter the content at the beginning of the pointcut.

After: enter the content at the end of the pointcut.

AfterReturning: enter the content after the pointcut returns (it can be used to do some processing on the returned value.

Around: enter the content before and after the pointcut, and control when the pointcut's own content is executed

AfterThrowing: used to handle the handling logic after an exception is thrown in the content section.

@ Order: the operation before the pointcut is performed according to the value of order, and the operation after the pointcut is performed from the value of order to the lowest.

Practical application 1: introducing dependency

First, we need to add a dependency that introduces aop, a UserAgentUtils package for analyzing client information, and a package for Lombok for @ Slf4j to print logs:

Org.springframework.boot spring-boot-starter-aop eu.bitwalker UserAgentUtils 1.20 II: define a ResponseAop aspect class

We have defined this class in the previous unified return value and exception handling, and this is to improve it. Here I'll write the code again:

@ Aspect@Order (5) @ Component@Slf4jpublic class ResponseAop 3: define a ThreadLocal variable

Defining the basic type directly here will have synchronization problems, so we define a ThreadLocal object to record the elapsed time.

ThreadLocal startTime = new ThreadLocal (); IV: define tangent points

What needs to be paid attention to here is the writing of tangent points, which must be correct in order to ensure the effectiveness of AOP! Some simple writing methods are attached here, and a separate chapter will be opened to explain the writing of execution expressions.

Any public method:

Execution (public * *)

Execution of any method that starts with "set":

Execution (* set* (..))

Execution of any method of the Service interface:

Execution (* com.xyz.service.Service.* (..))

Execution of any method defined in the service package:

Execution (* com.xyz.service.. (..))

Execution of any method of any class defined in the service package and all subpackages:

Execution (* com.xyz.service... (..))

/ * tangent point * / @ Pointcut ("execution (public * indi.viyoung.viboot.*.controller..* (..)") Public void httpResponse () {} five: get request information @ Before ("httpResponse ()") public void doBefore (JoinPoint joinPoint) {/ / start timing startTime.set (System.currentTimeMillis ()); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes (); HttpServletRequest request = attributes.getRequest (); / / print request content UserAgent userAgent = UserAgent.parseUserAgentString (request.getHeader ("User-Agent")) / / get User-Agent log.info ("interface path: {}", request.getRequestURL (). ToString ()) in the request header; log.info ("browser: {}", userAgent.getBrowser (). ToString ()); log.info ("browser version: {}", userAgent.getBrowserVersion ()); log.info ("operating system: {}", userAgent.getOperatingSystem (). ToString ()) Log.info ("IP: {}", request.getRemoteAddr ()); log.info ("request type: {}", request.getMethod ()); log.info ("class method:" + joinPoint.getSignature (). GetDeclaringTypeName () + "." + joinPoint.getSignature (). GetName ()); log.info ("request parameters: {}" + Arrays.toString (joinPoint.getArgs () } six: get the return value and execution time of the method in @ AfterReturning @ AfterReturning (returning = "ret", pointcut = "httpResponse ()") public void doAfterReturning (Object ret) {/ / return content log.info ("method return value: {}", ret); log.info ("method execution time: {} milliseconds", (System.currentTimeMillis ()-startTime.get () Seven: test results

Next, we access an interface:

2019-02-21 21 nio-8090-exec-5 03 indi.viyoung.viboot.aop.ResponseAop 31.358 INFO 11788-[nio-8090-exec-5] indi.viyoung.viboot.aop.ResponseAop: interface path: http://localhost:8090/users2019-02-21 21V 03VR 31.359 INFO 11788-[nio-8090-exec-5] indi.viyoung.viboot.aop.ResponseAop: browser: CHROME2019-02-21 21V 03VR 31.359 INFO 11788-[nio- 8090-exec-5] indi.viyoung.viboot.aop.ResponseAop: browser version: 72.0.3626.1092019-02-21 21 INFO 0336 INFO 11788-[nio-8090-exec-5] indi.viyoung.viboot.aop.ResponseAop: operating system: MAC_OS_X2019-02-21 21 21 INFO 0336 INFO 11788-[nio-8090-exec-5] indi.viyoung.viboot.aop.ResponseAop IP: 0nio-8090-exec-5 012019-02-21 21VlV 03360 INFO 11788-[nio-8090-exec-5] indi.viyoung.viboot.aop.ResponseAop: request Type: GET2019-02-21 21VOR 03RH 31.360 INFO 11788-[nio-8090-exec-5] indi.viyoung.viboot.aop.ResponseAop: class method: indi.viyoung.viboot.apilog.controller. UserController.findAll2019-02-21 21 nio-8090-exec-5 0315 INFO 31.360 INFO 11788-[nio-8090-exec-5] indi.viyoung.viboot.aop.ResponseAop: request parameters: {} []... 2019-02-21 21V 03VR 31.393 INFO 11788-[nio-8090-exec-5] indi.viyoung.viboot.aop.ResponseAop: method return value: ReturnVO {code='2000' Message=' operation succeeded', data= [User (id=10000001, password=123456, userName=vi-young), User (id=10000002, password=123456, userName=vi-young), User (id=10000003, password=123123, userName=lxt), User (id=10000004, password=123456, userName=yangwei)]} 2019-02-21 21 User 0393 INFO 11788-- [nio-8090-exec-5] indi.viyoung.viboot.aop.ResponseAop: method execution time: 36 Ms

It can be seen that we have got the information we want.

In the following application practice, we will save this information to the database and use some data analysis tools for analysis.

This is the answer to the question about how to print the interface log in AOP. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report