日志模塊自定義@SkipLogAspect注解跳過切面的操作方法
1.增加原因
因為如果參數(shù)是一些大對象,比如HttpServletRequest request,在轉(zhuǎn)化為json的時候就會導(dǎo)致OOM,所以選擇新增一個注解讓用戶自己靈活的決定是否添加日志切面
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)行時生效 public @interface SkipLogAspect { }
3.LogAspect.java
1.核心改動
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() { // 切點定義 } @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("<== 退出方法: {}.{}() | 耗時: {} 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 新增方法,測試跳過日志切面的注解
/** * 測試跳過日志切面的注解 * * @param msg * @return */ @SkipLogAspect @RequestMapping("/skipLogAspect") public String skipLogAspect(String msg) { return "skip log aspect"; }
3.測試
到此這篇關(guān)于日志模塊自定義@SkipLogAspect注解跳過切面的文章就介紹到這了,更多相關(guān)日志模塊自定義@SkipLogAspect注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Logback 使用TurboFilter實現(xiàn)日志級別等內(nèi)容的動態(tài)修改操作
這篇文章主要介紹了Logback 使用TurboFilter實現(xiàn)日志級別等內(nèi)容的動態(tài)修改操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08SpringBoot 2.6.x整合springfox 3.0報錯問題及解決方案
這篇文章主要介紹了SpringBoot 2.6.x整合springfox 3.0報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01java 判斷一個數(shù)組中的數(shù)值是否連續(xù)相鄰的方法
下面小編就為大家分享一篇java 判斷一個數(shù)組中的數(shù)值是否連續(xù)相鄰的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03@FeignClient注解中屬性contextId的使用說明
這篇文章主要介紹了@FeignClient注解中屬性contextId的使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06