这篇文章主要介绍了springboot日志切面通用类,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
1.pom.xml文件导入AOP依赖
org.springframework.bootspring-boot-starter-aop2.3.0.RELEASE
2.pom.xml导入Lombok依赖
org.projectlomboklomboktrue
package jmu.rjc.aspect; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; 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; import javax.servlet.http.HttpServletRequest; //日志切面 @Aspect @Component public class LogAspect { private final Logger logger = LoggerFactory.getLogger(this.getClass()); //包名配置为自己的项目包名即可 @pointcut("execution(* jmu.rjc.web.*.*(..))") public void log(){} @Before("log()") public void dobefore(JoinPoint joinPoint){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); String url = request.getRequestURL().toString(); String ip = request.getRemoteAddr(); String classMethod = joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName(); Object[] args = joinPoint.getArgs(); RequestLog requestLog = new RequestLog(url,ip,classMethod,args); logger.info("Request : {}",requestLog); } @After("log()") public void doAfter(){ logger.info("---------doAfter-------"); } @AfterReturning(returning = "result",pointcut = "log()") public void doAfterReturn(Object result){ logger.info("Result : {}"+result); } @Data @ToString @AllArgsConstructor @NoArgsConstructor private class RequestLog{ private String url; private String ip; private String classMethod; private Object[] args; } }
3.关于Lombok的注意点
Lombok的使用大大简化了开发过程,提高了开发效率,但是也存在一些隐患.比如强行侵入jar包,JDK版本升级冲突,破坏部分封装性等缺陷,使用前最好和团队商量好
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。