当前位置: 代码迷 >> 综合 >> spring boot @Aspect 统一日志处理
  详细解决方案

spring boot @Aspect 统一日志处理

热度:97   发布时间:2024-02-21 13:11:02.0

参考文章,大谢

https://www.imooc.com/video/14346/0

编写HttpAspect类

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.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.fastjson.JSON;@Aspect
@Component
public class HttpAspect {@Pointcut("execution(public * com.**.controller..*.*(..))")public void log() {}@AfterReturning(returning = "object", pointcut = "log()")public void doAfterReturning(JoinPoint joinPoint, Object object) {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();// urlSystem.err.println(request.getRequestURL());// methodSystem.err.println(request.getMethod());// ipSystem.err.println(request.getRemoteAddr());// 类+方法System.err.println(joinPoint.getSignature().getDeclaringTypeName() + joinPoint.getSignature().getName());// 请求参数System.err.println(JSON.toJSONString(joinPoint.getArgs()));// 返回值System.err.println(JSON.toJSONString(object));}
}
  相关解决方案