Spring Boot 日志切面配置

依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

LogsAspect.java

/**
 * 日志切面
 *
 * @author 向振华
 * @date  2018/11/29 14:27
 */
@Order(10)
@Slf4j
@Aspect
@Configuration
public class LogsAspect {
    /**
     * 请求地址
     */
    private ThreadLocal<String> requestPath = new ThreadLocal<>();
    /**
     * 传入参数
     */
    private ThreadLocal<String> inputMap = new ThreadLocal<>();
    /**
     * 输出结果
     */
    private ThreadLocal<String> outputMap = new ThreadLocal<>();
    /**
     * 用户ID
     */
    private ThreadLocal<Long> userId = new ThreadLocal<>();
    /**
     * 开始时间
     */
    private ThreadLocal<Long> startTimeMills = new ThreadLocal<>();
    /**
     * 结束时间
     */
    private ThreadLocal<Long> endTimeMills = new ThreadLocal<>();

    @Before("execution(* com.rongyixin.sudaidai.cms.controller..*.*(..))")
    public void doBeforeInServiceLayer() {
        startTimeMills.set(System.currentTimeMillis());
    }

    @Around("execution(* com.rongyixin.sudaidai.cms.controller..*.*(..))")
    public Object getReqAndResInfo(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //请求值
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = servletRequestAttributes.getRequest();
        requestPath.set(request.getRequestURI());
        if (request.getContentType() != null && request.getContentType().toLowerCase().contains("application/json")) {
            inputMap.set(JSONObject.toJSONString(proceedingJoinPoint.getArgs()));
        } else {
            inputMap.set(JSONObject.toJSONString(request.getParameterMap()));
        }
        //响应值
        Object res;
        res = proceedingJoinPoint.proceed();
        if (!(res instanceof ModelAndView)) {
            String result = JSONObject.toJSONString(res);
            if (StringUtils.isNotBlank(result)) {
                if (result.length() > 200) {
                    result = result.substring(0, 200);
                }
                outputMap.set(result);
            }
        }
        //用户
        try {
            CurrentUser currentUser = CurrentUser.get();
            userId.set(currentUser.getUserId());
        } catch (Exception e) {
            userId.set(0L);
        }
        return res;
    }

    @After("execution(* com.rongyixin.sudaidai.cms.controller..*.*(..))")
    public void doAfterInServiceLayer() {
        endTimeMills.set(System.currentTimeMillis());
        if (StringUtils.isNotBlank(outputMap.get())) {
            printLog();
        }
        removeThreadLocal();
    }

    /**
     * 回收ThreadLocal变量
     */
    private void removeThreadLocal() {
        userId.remove();
        requestPath.remove();
        inputMap.remove();
        outputMap.remove();
        startTimeMills.remove();
        endTimeMills.remove();
    }

    /**
     * 输出日志
     */
    private void printLog() {
        log.info("请求日志---> time:[{}],user:[{}],url:[{}],params:{},res:{}", endTimeMills.get() - startTimeMills.get(), userId.get(), requestPath.get(), inputMap.get(), outputMap.get());
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值