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

The implementation method of recording request and response logs by springboot-WebLogAspect

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article focuses on "the implementation of springboot-WebLogAspect for recording request and response logs". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the implementation method that springboot-WebLogAspect is used to record request and response log".

Use

Used to record request and response logs for spring boot; aop implementation

Dependence

Lombok-if not, you can create your own log object

Slf4j-

Jackson

Apache common

Realize

Package xxx.xxx.xxx;import com.fasterxml.jackson.databind.ObjectMapper;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestAttributes;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest / * is used to record the web request / response log * / @ Component@Aspect@Slf4jpublic class WebLogAspect {/ * section, where xxx.xxx.xxx.web.ctl is the Controller package name * / @ Pointcut ("execution (* xxx.xxx.xxx.web.ctl..*.* (..)") Before the private void parameterPointCut () {} / * method executes, record the request * @ param joinPoint * / @ Before ("parameterPointCut ()") public void requestLog (JoinPoint joinPoint) {RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes (); / / this RequestContextHolder is something provided by Springmvc to get the request HttpServletRequest request = ((ServletRequestAttributes) requestAttributes). GetRequest (); String queryStr = request.getQueryString () If (StringUtils.isNotEmpty (queryStr)) {log.info ("request address: [{}] {}", request.getMethod (), request.getRequestURI ());} else {log.info ("request address: [{}] {}? {}", request.getMethod (), request.getRequestURI (), queryStr);} printRequestArgs (joinPoint) } private void printRequestArgs (JoinPoint joinPoint) {log.info ("request method: {}", joinPoint.toString ()); Object [] reqArgs = joinPoint.getArgs (); if (null = = reqArgs) {return;} int c = 0; ObjectMapper mapper = new ObjectMapper () For (Object arg: reqArgs) {try {log.info ("request input parameter [{}]: {}", c, mapper.writeValueAsString (arg));} catch (Exception ex) {log.error ("request input parameter conversion exception", ex);} C++ Record the response * @ param joinPoint * @ param ret method execution result injection object * @ return * / @ AfterReturning (returning = "ret", pointcut = "parameterPointCut ()") public Object responeLog (JoinPoint joinPoint, Object ret) {try {ObjectMapper mapper = new ObjectMapper () Log.info ("response output parameter: {}", mapper.writeValueAsString (ret));} catch (Throwable ex) {log.error ("response exception", ex);} return ret;}} so far, I believe you have a deeper understanding of "the implementation method of springboot-WebLogAspect for recording request and response log". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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