springboot整合log日志

时间:2025-04-03 07:06:13
package com.usian.Aspect; //Aop处理日志的类 import lombok.extern.slf4j.Slf4j; 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.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; @Component @Aspect @Slf4j public class MyAspect { @Before("execution(* (..))") public void doBefore(JoinPoint joinPoint) throws Throwable{ System.out.println("前置方法执行了..."); //接收请求内容 ServletRequestAttributes requestAttributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); //获取请求 HttpServletRequest request = requestAttributes.getRequest(); //记录下请求内容 log.info("url:"+request.getRequestURI().toString()); log.info("HTTP_METHOD : "+request.getMethod()); log.info("IP:"+request.getRemoteAddr()); //获取请求参数属性的集合 Enumeration<String> enu = request.getParameterNames(); //遍历集合 while (enu.hasMoreElements()){ //获取请求参数的属性 String name = enu.nextElement(); //(name) log.info("name:{},value:{}",name,request.getParameter(name)); } } /** * returing:该属性指定一个形参名,用于表示Advice方法中可定义与此同名的形参, * 该形参可用于访问目标方法的返回值。除此之外, * 在Advice方法中定义该形参(代表目标方法的返回值)时指定的类型, * 会限制目标方法必须返回指定类型的值或没有返回值。 * @param o */ @AfterReturning(value = "execution(* (..))",returning = "o") public void doAfter(Object o){ System.out.println("后置方法执行了..."); log.info("response"+o.toString()); } }