解析springboot集成AOP實(shí)現(xiàn)日志輸出的方法
開發(fā)接口系統(tǒng)中主要的一環(huán)就是日志輸出,如果系統(tǒng)出現(xiàn)問題,日志能幫我們?nèi)ザㄎ粏栴},最常見的日志是調(diào)用方 所調(diào)用的IP 接口地址 對應(yīng)方法 參數(shù)值 以及接口方接收到請求 所返回的參數(shù)。如果這需要在每一個(gè)controller層去寫的話代碼過于重復(fù),于是就使用AOP定義切面 對其接口調(diào)用前后進(jìn)行攔截日志輸出。
1、加入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2、定義切面組件
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import io.swagger.annotations.ApiOperation; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; import java.util.Arrays; /** * 日志切面工具 * @author tongyao */ @Aspect @Component public class LogAspect { private final Logger logger = LoggerFactory.getLogger(LogAspect.class); /** * 定義切入點(diǎn) 就是需要攔截的切面 */ @Pointcut("execution(public * cn.com.antMap.tree.controller.*.*(..))") public void controllerMethod() {} /** * 進(jìn)入方法請求執(zhí)行前 * * @param joinPoint * @throws Exception */ @Before("controllerMethod()") public void LogRequestInfo(JoinPoint joinPoint) { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest(); StringBuilder requestLog = new StringBuilder(); Signature signature = joinPoint.getSignature(); // 打印請求內(nèi)容 logger.info("===============請求內(nèi)容開始==============="); logger.info("請求地址:" + request.getRequestURL().toString()); logger.info("請求IP:" + request.getRemoteAddr()); logger.info("請求方式:" + request.getMethod()); logger.info("請求類方法:" + joinPoint.getSignature()); logger.info("請求類方法參數(shù)值:" + Arrays.toString(joinPoint.getArgs())); // 處理請求參數(shù) String[] paramNames = ((MethodSignature) signature).getParameterNames(); Object[] paramValues = joinPoint.getArgs(); int paramLength = null == paramNames ? 0 : paramNames.length; if (paramLength == 0) { requestLog.append("請求參數(shù) = {} "); } else { requestLog.append("請求參數(shù) = ["); for (int i = 0; i < paramLength - 1; i++) { requestLog.append(paramNames[i]).append("=").append(JSONObject.toJSONString(paramValues[i])).append(","); } requestLog.append(paramNames[paramLength - 1]).append("=").append(JSONObject.toJSONString(paramValues[paramLength - 1])).append("]"); } logger.info("請求參數(shù)明細(xì):"+requestLog.toString()); logger.info("===============請求內(nèi)容結(jié)束==============="); } /** * 進(jìn)入方法請求執(zhí)行后 * * @param o * @throws Exception */ @AfterReturning(returning = "o", pointcut = "controllerMethod()") public void logResultVOInfo(Object o){ logger.info("--------------返回內(nèi)容開始----------------"); logger.info("Response內(nèi)容:" + JSON.toJSONString(o)); logger.info("--------------返回內(nèi)容結(jié)束----------------"); } /** * 該切面發(fā)生異常信息時(shí)進(jìn)行攔截 * @param joinPoint * @param ex */ @AfterThrowing(pointcut = "controllerMethod()", throwing = "ex") public void doAfterThrowing(JoinPoint joinPoint, Exception ex) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("連接點(diǎn)方法為:" + methodName + ",參數(shù)為:" + args + ",異常為:" + ex); } }
其中spring.aop.auto屬性默認(rèn)是開啟的,也就是說只要引入了AOP依賴后,默認(rèn)已經(jīng)增加了@EnableAspectJAutoProxy。
aop組件中用到了 @Pointcut(“execution(public * cn.com.antMap.tree.controller..(…))”)定義切面切入點(diǎn) 就是攔截這個(gè)包路徑下的所有請求的方法
@Before(“controllerMethod()”) 切入點(diǎn)調(diào)用邏輯之前進(jìn)行攔截
@AfterReturning(returning = “o”, pointcut = “controllerMethod()”) 切入點(diǎn)調(diào)用邏輯完成后進(jìn)行攔截輸出
@AfterThrowing(pointcut = “controllerMethod()”, throwing = “ex”) 切入點(diǎn)發(fā)生異常是進(jìn)行攔截
到此這篇關(guān)于springboot集成AOP實(shí)現(xiàn)日志輸出的文章就介紹到這了,更多相關(guān)springboot日志輸出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
maven中snapshot相關(guān)jar無法拉取問題及解決方案(常用方案)
Maven中的SNAPSHOT版本是指正在開發(fā)中的版本,這些版本可能會頻繁地更新,在使用Maven構(gòu)建項(xiàng)目時(shí),有時(shí)會遇到無法拉取SNAPSHOT相關(guān)jar的問題,下面給大家分享maven中snapshot相關(guān)jar無法拉取問題及解決方案,感興趣的朋友一起看看吧2024-06-06JAVA求兩直線交點(diǎn)和三角形內(nèi)外心的方法
本文提供了JAVA求兩直線交點(diǎn)、三角形外心、三角形內(nèi)心的代碼和算法講解,大家可以參考使用2013-11-11spring boot security設(shè)置忽略地址不生效的解決
這篇文章主要介紹了spring boot security設(shè)置忽略地址不生效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07mybatisplus?selectOne查詢,有數(shù)據(jù),但返回為null問題
這篇文章主要介紹了mybatisplus?selectOne查詢,有數(shù)據(jù),但返回為null問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11Java fastjson解析json字符串實(shí)現(xiàn)過程解析
這篇文章主要介紹了Java fastjson解析json字符串實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Springboot-Starter造輪子之自動鎖組件lock-starter實(shí)現(xiàn)
這篇文章主要為大家介紹了Springboot-Starter造輪子之自動鎖組件lock-starter實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05