Spring boot 使用AOP统一处理日志

本文介绍了如何在Spring Boot应用中使用AOP来统一处理日志。通过在pom文件添加相关依赖,结合自定义注解,可以实现对特定方法的环绕通知,从而在目标方法执行前后记录日志。示例展示了无注解方法和添加注解后的不同日志输出效果。
摘要由CSDN通过智能技术生成

在 Spring boot里配置AOP其实很简单,只需在pom文件里加入

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
然后配置切面类,下面是配置了一个环绕通知
@Aspect
@Component
public class LoggerInterceptor {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Around("within(com..*) && @annotation(log)")
    public Object doAroundMethod(ProceedingJoinPoint pjd, Log log) throws Throwable {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request =  attributes.getRequest();
        //路径
        logger.info("方法路径:{}",pjd.getSignature().getDeclaringTypeName()+'.'+ pjd.getSignature().getName());//获取类名及类方法
        //ip
        logger.info(&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值