切面打印日志

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @ClassName CommunicationServiceAspect
 * @Description TODO
 * @Author dada
 * @Date 9/18/2019 5:42 PM
 * @Version 1.0
 **/
@Aspect
@Component
public class CommunicationServiceAspect {

    private static CxLogger logger = CxLogManager.getLogger("CommunicationServiceAspect.class").defaultTag("Trag", "CommunicationServiceAspect");

    @Pointcut("@annotation(CommunicationService)")
    public void emailService() {

    }

    @Around(value = "emailService()")
    public Object communicationService(ProceedingJoinPoint joinPoint) throws Throwable {
        /*long startTime = System.currentTimeMillis();
        String name = joinPoint.getSignature().toString();
        joinPoint.proceed();
        long sendEmailTime = System.currentTimeMillis() - startTime;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("Communication Service: ");
        stringBuilder.append(name);
        InfoLogUtil.info(stringBuilder.toString(), sendEmailTime, logger);*/

        long start = System.currentTimeMillis();
        try {
            Object result = joinPoint.proceed();
//            long end = System.currentTimeMillis();
//            InfoLogUtil.info(joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + parseParames(joinPoint.getArgs()) , "INBOUND", (end - start), logger);
            return result;
        } catch (Throwable e) {
            InfoLogUtil.error(joinPoint + "listener-throw exception :" + e.getMessage(), parseParames(joinPoint.getArgs()), logger);
            throw e;
        } finally {
            long end = System.currentTimeMillis();
            InfoLogUtil.info(joinPoint.getSignature().toShortString(), "INBOUND", end - start, logger);
        }
    }

    private String parseParames(Object[] parames) {

        if (null == parames || parames.length <= 0) {
            return "no parameters";

        }
        StringBuffer param = new StringBuffer("the request parameters is # :[ ");
        int i = 0;
        for (Object obj : parames) {
            i++;
            if (i == 1) {
                param.append(obj.toString());
                continue;
            }
            param.append(" ,").append(obj.toString());
        }
        return param.append(" ]").toString().replace("#", String.valueOf(i));
    }
}

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值