SpringBoot使用AOP(或者elk)统一处理web请求日志
SpringBoot使用AOP(或者elk)统一处理web请求日志
制作人:全心全意
SpringBoot使用AOP(或者elk)统一处理web请求日志
作用:在我们的方法的前后实现拦截,减少打印日志代码的冗余性问题
引入依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
创建日志监听类
package com.zq.aop; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import lombok.extern.slf4j.Slf4j; @Aspect @Component @Slf4j public class WebLogAspect { /** * 第一个.*:相当于com.zq.controller下的所有类 第二个。*:相当于类下的所有方法 (..):所有方法的参数 */ //可监听在controller包中,也可监听server包中 @Pointcut("execution(public * com.zq.controller.*.*(..))") public void webLog() { } /** * 前置通知,请求方法之前去做拦截,走完前置(打印请求信息),才会进行目标方法 * * @param joinPoint */ @Before("webLog()") public void doBefore(JoinPoint joinPoint) { // 收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 log.info("URL:" + request.getRequestURL().toString()); log.info("HTTP_METHOD:" + request.getMethod()); log.info("IP:" + request.getRemoteAddr()); Enumeration<String> enu = request.getParameterNames(); while (enu.hasMoreElements()) { String name = (String) enu.nextElement(); log.info("name:{},value:{}", name, request.getParameter(name)); } } /** * 后置通知,调用完目标方法后,执行此方法(打印返回信息) * * @param ret */ @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) { // 处理完请求,返回内容 log.info("RESPONSE:" + ret); } }
运行项目,监听包中的方法被访问即可记录日志
package com.zq.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import lombok.extern.slf4j.Slf4j; @RestController @Slf4j // 日志的注解 public class Mylogback { @RequestMapping("/getlog4j") public String getlog4j(String userName, Integer age) { log.info("name:{},age:{}", userName, age); log.debug("123"); return userName; } }
前置============================ 2020-12-21-12-32 [http-nio-8012-exec-1] [com.zq.aop.WebLogAspect] [INFO] - URL:http://127.0.0.1:8012/getlog4j 2020-12-21-12-32 [http-nio-8012-exec-1] [com.zq.aop.WebLogAspect] [INFO] - HTTP_METHOD:GET 2020-12-21-12-32 [http-nio-8012-exec-1] [com.zq.aop.WebLogAspect] [INFO] - IP:127.0.0.1 2020-12-21-12-32 [http-nio-8012-exec-1] [com.zq.aop.WebLogAspect] [INFO] - name:userName,value:123 2020-12-21-12-32 [http-nio-8012-exec-1] [com.zq.controller.Mylogback] [INFO] - name:123,age:null 2020-12-21-12-32 [http-nio-8012-exec-1] [com.zq.controller.Mylogback] [DEBUG] - 123 后置============================ 2020-12-21-12-32 [http-nio-8012-exec-1] [com.zq.aop.WebLogAspect] [INFO] - RESPONSE:123 2020-12-21-12-32 [http-nio-8012-exec-1] [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor] [DEBUG] - Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [text/plain, */*, text/plain, */*, application/json, application/*+json, application/json, application/*+json] 2020-12-21-12-32 [http-nio-8012-exec-1] [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor] [DEBUG] - Writing ["123"] 2020-12-21-12-32 [http-nio-8012-exec-1] [org.springframework.web.servlet.DispatcherServlet] [DEBUG] - Completed 200 OK 2020-12-21-12-32 [http-nio-8012-exec-2] [org.springframework.web.servlet.DispatcherServlet] [DEBUG] - GET "/favicon.ico", parameters={} 2020-12-21-12-32 [http-nio-8012-exec-2] [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] [DEBUG] - Mapped to ResourceHttpRequestHandler [class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []] 2020-12-21-12-32 [http-nio-8012-exec-2] [org.springframework.web.servlet.DispatcherServlet] [DEBUG] - Completed 200 OK