添加日志_第五章springboot2.0添加aop日志实现记录请求地址
生活随笔
收集整理的這篇文章主要介紹了
添加日志_第五章springboot2.0添加aop日志实现记录请求地址
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 添加spring-boot-starter-aop包
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
<version>2.0.0.RELEASEversion>
dependency>
2. 新建WebLogAspect類
3. 添加@Aspect @Component注解
4. 實現方法
package com.dyp.config;import java.util.Arrays;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.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;@Aspect@Componentpublic class WebLogAspect { private Logger logger = LoggerFactory.getLogger(getClass()); //定義execution表達式使用 @Pointcut("execution(public * com.dyp.controller..*.*(..))") public void webLog(){} @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { // 接收到請求,記錄請求內容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 記錄下請求內容 logger.info("URL : " + request.getRequestURL().toString()); logger.info("HTTP_METHOD : " + request.getMethod()); logger.info("IP : " + request.getRemoteAddr()); logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret總結
以上是生活随笔為你收集整理的添加日志_第五章springboot2.0添加aop日志实现记录请求地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下openldap版本查询,用
- 下一篇: 文本解析 python 多行,关于pyt