日志模塊自定義@SkipLogAspect注解跳過切面的操作方法
1.增加原因
因?yàn)槿绻麉?shù)是一些大對(duì)象,比如HttpServletRequest request,在轉(zhuǎn)化為json的時(shí)候就會(huì)導(dǎo)致OOM,所以選擇新增一個(gè)注解讓用戶自己靈活的決定是否添加日志切面
2.common-log4j2-starter
1.目錄結(jié)構(gòu)
2.SkipLogAspect.java 自定義注解
package cn.sunxiansheng.log4j2.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Description: 跳過日志切面的注解 * * @Author sun * @Create 2025/1/24 18:08 * @Version 1.0 */ @Target({ElementType.METHOD, ElementType.TYPE}) // 可以標(biāo)注在類或方法上 @Retention(RetentionPolicy.RUNTIME) // 運(yùn)行時(shí)生效 public @interface SkipLogAspect { }
3.LogAspect.java
1.核心改動(dòng)
2.完整代碼
package cn.sunxiansheng.log4j2.aspectj; import cn.sunxiansheng.log4j2.annotation.SkipLogAspect; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import java.lang.reflect.Method; /** * 日志切面 * * @author sunxiansheng */ @Aspect @Slf4j public class LogAspect { private static final Gson GSON = new GsonBuilder() .setPrettyPrinting() .disableHtmlEscaping() .create(); private static final String ANSI_RESET = "\u001B[0m"; private static final String ANSI_CYAN = "\u001B[92m"; @Pointcut("execution(public * *..controller..*(..)) || " + "execution(public * *..service..*(..))") public void applicationPackagePointcut() { // 切點(diǎn)定義 } @Around("applicationPackagePointcut()") public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { // 獲取目標(biāo)類和方法 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); Class<?> targetClass = joinPoint.getTarget().getClass(); // 檢查是否存在 @SkipLogAspect 注解 if (method.isAnnotationPresent(SkipLogAspect.class) || targetClass.isAnnotationPresent(SkipLogAspect.class)) { // 直接執(zhí)行目標(biāo)方法,不記錄日志 return joinPoint.proceed(); } // 正常記錄日志邏輯 String className = signature.getDeclaringTypeName(); String methodName = signature.getName(); Object[] args = joinPoint.getArgs(); String requestParams = GSON.toJson(args); log.info("==> 進(jìn)入方法: {}.{}()", className, methodName); log.info("參數(shù):\n" + ANSI_CYAN + "{}" + ANSI_RESET, requestParams); long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long endTime = System.currentTimeMillis(); String response = GSON.toJson(result); log.info("<== 退出方法: {}.{}() | 耗時(shí): {} ms", className, methodName, endTime - startTime); log.info("返回值:\n" + ANSI_CYAN + "{}" + ANSI_RESET, response); return result; } }
3.common-log4j2-starter-demo
1.目錄結(jié)構(gòu)
2.TraceController.java 新增方法,測(cè)試跳過日志切面的注解
/** * 測(cè)試跳過日志切面的注解 * * @param msg * @return */ @SkipLogAspect @RequestMapping("/skipLogAspect") public String skipLogAspect(String msg) { return "skip log aspect"; }
3.測(cè)試
到此這篇關(guān)于日志模塊自定義@SkipLogAspect注解跳過切面的文章就介紹到這了,更多相關(guān)日志模塊自定義@SkipLogAspect注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入了解Java SpringBoot自動(dòng)裝配原理
在使用springboot時(shí),很多配置我們都沒有做,都是springboot在幫我們完成,這很大一部分歸功于springboot自動(dòng)裝配。本文將詳細(xì)為大家講解SpringBoot的自動(dòng)裝配原理,需要的可以參考一下2022-03-03Logback 使用TurboFilter實(shí)現(xiàn)日志級(jí)別等內(nèi)容的動(dòng)態(tài)修改操作
這篇文章主要介紹了Logback 使用TurboFilter實(shí)現(xiàn)日志級(jí)別等內(nèi)容的動(dòng)態(tài)修改操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java線程中斷及線程中斷的幾種使用場(chǎng)景小結(jié)
在并發(fā)編程中,合理使用線程中斷機(jī)制可以提高程序的魯棒性和可維護(hù)性,本文主要介紹了Java線程中斷及線程中斷的幾種使用場(chǎng)景小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01Spring?Boot項(xiàng)目Jar包加密實(shí)戰(zhàn)教程
本文詳細(xì)介紹了如何在Spring?Boot項(xiàng)目中實(shí)現(xiàn)Jar包加密,我們首先了解了Jar包加密的基本概念和作用,然后學(xué)習(xí)了如何使用Spring?Boot的Jar工具和第三方庫來實(shí)現(xiàn)Jar包的加密和解密,感興趣的朋友一起看看吧2024-02-02SpringBoot 2.6.x整合springfox 3.0報(bào)錯(cuò)問題及解決方案
這篇文章主要介紹了SpringBoot 2.6.x整合springfox 3.0報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01java 判斷一個(gè)數(shù)組中的數(shù)值是否連續(xù)相鄰的方法
下面小編就為大家分享一篇java 判斷一個(gè)數(shù)組中的數(shù)值是否連續(xù)相鄰的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03@FeignClient注解中屬性contextId的使用說明
這篇文章主要介紹了@FeignClient注解中屬性contextId的使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06