springboot整合aop实现日志记录


@Component
//切面
@Aspect
public class LoggerConfig {
    private Logger logger = LoggerFactory.getLogger(LoggerConfig.class);

    //切入点
//    @Pointcut("execution(* com.qfedu.service.impl..*.*(..))")
    @Pointcut("@annotation(com.acedu.aop.SysLogger)")
    public void pt1() {
    }//签名

    @Around("pt1()")
    public Object arround(ProceedingJoinPoint proceedingJoinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //请求的内容,记录url、method、ip
        System.out.println("URL==>" + request.getRequestURL().toString() + "," +
                "METHOD==>" + request.getMethod() + "," +
                "IP==>" + request.getRemoteAddr());
        //1.记录请求的接口,以及时间
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();
        String[] parameterNames = signature.getParameterNames();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat retrunSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //1.logger日志 5个级别   info debug,error,waring,trace
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("===>进入了方法:" + method.getName());
        stringBuffer.append("===>参数为:{}" + Arrays.toString(parameterNames));
        stringBuffer.append("===>当前时间为" + simpleDateFormat.format(new Date()));
//        logger.info(stringBuffer.toString());
        logger.debug(stringBuffer.toString());
        Object proceed = null;
        try {
            proceed = proceedingJoinPoint.proceed();
            logger.debug("方法执行完成,时间为:{}" + retrunSimpleDateFormat.format(new Date()));
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error("当前接口出现异常:" + throwable.getMessage());
        }
        return proceed;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值