springboot如何使用AOP做訪問(wèn)請(qǐng)求日志
這篇文章主要介紹了springboot如何使用AOP做訪問(wèn)請(qǐng)求日志,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
springboot中使用AOP做訪問(wèn)請(qǐng)求日志:這次引入springboot的aop和日志
1、pom.xml引入:
<!--springBoot的aop,已經(jīng)集成了spring aop和AspectJ--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-logging --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
2、切面類(lèi)配置:
@Component @Aspect public class LogAspect { private static final Logger logger = LoggerFactory.getLogger(LogAspect.class); //切入點(diǎn)表達(dá)式,com.springboot.controller自己controller包的路徑 @Pointcut("execution(public * com.springboot.controller..*.*(..))") public void pointCut(){ } @Before("pointCut()") public void beforeMethod(JoinPoint joinPoint){ ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); //獲取需要打印的參數(shù)信息 String requestURI = request.getRequestURI(); String method = request.getMethod(); String remoteAddr = request.getRemoteAddr(); //這里使用的是阿里的fastjson String jsonString = JSON.toJSONString(joinPoint.getArgs()); //打印信息 logger.info("------------------------請(qǐng)求信息----------------------------------"); logger.info("請(qǐng)求時(shí)間 :{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); logger.info("remoteAddr: {} ",remoteAddr); logger.info("requestURI : {}",requestURI); logger.info("Controller : {}", joinPoint.getTarget().getClass()); logger.info("method type: {}" ,method); logger.info("req paras: {}",jsonString); logger.info("------------------------請(qǐng)求信息-----------------------------------"); } }
效果:
com.springboot.common.aop.LogAspect : ------------------------請(qǐng)求信息---------------------------------- com.springboot.common.aop.LogAspect : 請(qǐng)求時(shí)間 :2020-01-02 22:38:40 com.springboot.common.aop.LogAspect : remoteAddr: 0:0:0:0:0:0:0:1 com.springboot.common.aop.LogAspect : requestURI : /user/10001 com.springboot.common.aop.LogAspect : Controller : class com.springboot.controller.UserController com.springboot.common.aop.LogAspect : method type: GET com.springboot.common.aop.LogAspect : req paras: [10001] com.springboot.common.aop.LogAspect : ------------------------請(qǐng)求信息---------------------------------
這里只做訪問(wèn)請(qǐng)求的日志打印,還可以后置通知,打印響應(yīng)信息,結(jié)合環(huán)繞通知可以打印程序執(zhí)行的時(shí)間等其他操作。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot通過(guò)jar包啟動(dòng)中文日志亂碼問(wèn)題及解決
這篇文章主要介紹了springboot通過(guò)jar包啟動(dòng)中文日志亂碼問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06token工作機(jī)制及原理附Java生成token工具類(lèi)
這篇文章介紹了token工作機(jī)制及原理,內(nèi)附Java生成token工具類(lèi)Demo。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12java中以DES的方式實(shí)現(xiàn)對(duì)稱(chēng)加密并提供密鑰的實(shí)例
這篇文章主要介紹了java中以DES的方式實(shí)現(xiàn)對(duì)稱(chēng)加密并提供密鑰的實(shí)例的相關(guān)資料,這里提供實(shí)例幫助大家學(xué)習(xí)理解這部分知識(shí),需要的朋友可以參考下2017-08-08Java調(diào)用WebService接口作測(cè)試
這篇文章主要介紹了Java調(diào)用WebService接口作測(cè)試,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11關(guān)于Mybatis-Plus?Update更新策略問(wèn)題
這篇文章主要介紹了關(guān)于Mybatis-Plus?Update更新策略問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11