溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot-WebLogAspect用于記錄請求和響應(yīng)日志的實現(xiàn)方法

發(fā)布時間:2021-06-23 09:09:37 來源:億速云 閱讀:449 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“springboot-WebLogAspect用于記錄請求和響應(yīng)日志的實現(xiàn)方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“springboot-WebLogAspect用于記錄請求和響應(yīng)日志的實現(xiàn)方法”吧!

  • 用途

    用于記錄spring boot的請求和響應(yīng)日志; aop實現(xiàn);

  • 依賴

    1. lombok -如果沒有,可以自已創(chuàng)建log對象

    2. Slf4j -

    3. jackson

    4. apache common

  • 實現(xiàn)

package xxx.xxx.xxx;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 用于記錄web請求/響應(yīng)日志
 */
@Component
@Aspect
@Slf4j
public class WebLogAspect {
    /**
     * 切面, xxx.xxx.xxx.web.ctl是Controller包名
     */
    @Pointcut("execution(* xxx.xxx.xxx.web.ctl..*.*(..))")
    private void parameterPointCut() {
    }

    /**
     * 方法執(zhí)行前,記錄請求
     * @param joinPoint
     */
    @Before("parameterPointCut()")
    public void requestLog(JoinPoint joinPoint){
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();//這個RequestContextHolder是Springmvc提供來獲得請求的東西
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        String queryStr = request.getQueryString();
        if(StringUtils.isNotEmpty(queryStr)){
            log.info("請求地址: [{}] {}",  request.getMethod(), request.getRequestURI());
        }else{
            log.info("請求地址: [{}] {}?{} ", request.getMethod(), request.getRequestURI(), queryStr);
        }

        printRequestArgs(joinPoint);
    }

    private void printRequestArgs(JoinPoint joinPoint){
        log.info("請求方法: {}", joinPoint.toString());

        Object[] reqArgs = joinPoint.getArgs();
        if(null == reqArgs){
            return;
        }

        int c = 0;
        ObjectMapper mapper = new ObjectMapper();
        for(Object arg: reqArgs){
            try{
                log.info("請求入?yún){}]: {}", c, mapper.writeValueAsString(arg));
            }catch (Exception ex){
                log.error("請求入?yún)⑥D(zhuǎn)換異常", ex);
            }
            c++;
        }
    }

    /**
     * 方法執(zhí)行后,記錄響應(yīng)
     * @param joinPoint
     * @param ret 方法執(zhí)行結(jié)果注入對象
     * @return
     */
    @AfterReturning(returning = "ret",pointcut = "parameterPointCut()")
    public Object responeLog(JoinPoint joinPoint, Object ret){
        try {
            ObjectMapper mapper = new ObjectMapper();
            log.info("響應(yīng)出參: {}", mapper.writeValueAsString(ret));
        } catch (Throwable ex) {
            log.error("響應(yīng)異常", ex);
        }
        return ret;
    }
}

到此,相信大家對“springboot-WebLogAspect用于記錄請求和響應(yīng)日志的實現(xiàn)方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI