轻量级前端框架助力开发者提升项目效率与性能
451
2023-02-22
springboot日志切面通用类实例详解
1.pom.xml文件导入AOP依赖
2.pom.xml导入Lombok依赖
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.slVEZZFf4j.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("---------doAftehttp://r-------");
}
@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版本升级冲突,破坏部分封装性等缺陷,使用前最好和团队商量好
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~