java 切面日志打印出参入参

切面Controller出入参日志打印

项目结构

切面日志对controller下所有的方法生效
在这里插入图片描述

切面代码

@Slf4j
@Aspect
@Component
public class ControllerLogAspect {

    // 定义一个切点,拦截所有Controller层的public方法
    @Before("execution(public * com.jzt.market.controller..*.*(..))")
    public void logBefore(JoinPoint joinPoint) throws NoSuchMethodException {
        // 获取方法名
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getName();
        Object target = joinPoint.getTarget();
        Method method = target.getClass().getMethod(signature.getName(), signature.getParameterTypes());
        Operation annotation = method.getAnnotation(Operation.class);
        String summary = annotation.summary();
        // 获取入参
        Object[] args = joinPoint.getArgs();
        log.info("进入[{}]方法:=>{},入参:{}", summary, methodName,args);
    }

    @AfterReturning(pointcut = "execution(public * jx.mim.market.controller..*.*(..))", returning = "result")
    public void logAfterReturning(JoinPoint joinPoint, Object result) {
        // 获取方法名
        String methodName = joinPoint.getSignature().getName();
        log.info("结束方法:{},出参:{}", methodName, JsonUtils.toJsonPrettyString(result));
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中实现环绕切面监控接口异常信息并打印logger日志,可以按照以下步骤进行: 1. 创建一个切面类,并标注@Aspect注解,用于定义切面的具体实现代码。 ```java @Aspect @Component public class ExceptionAspect { @Around("execution(* com.example.demo.controller.*.*(..))") public Object handleException(ProceedingJoinPoint joinPoint) throws Throwable { Object result = null; try { result = joinPoint.proceed(); } catch (Exception e) { Logger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass()); logger.error("接口异常信息:{},异常堆栈信息:{}", e.getMessage(), e); throw e; } return result; } } ``` 2. 在切面类中定义一个环绕通知方法,用于拦截指定的接口请求,并处理接口异常信息。在通知方法中,可以使用ProceedingJoinPoint数调用目标方法并获取方法执行结果。如果方法执行过程中现异常,可以使用Logger打印异常信息,并将异常重新抛,以便后续处理。 3. 在切面类上添加@Component注解,将切面类注册到Spring容器中。 4. 在Spring Boot的配置文件中,添加以下配置,启用AOP切面功能。 ```properties spring.aop.auto=true ``` 通过以上步骤,就可以在Spring Boot应用中实现环绕切面监控接口异常信息并打印logger日志的功能。在应用程序执行过程中,如果接口现异常,就会在控制台或日志文件中打印异常信息,方便开发人员进行排查和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值