日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用户请求接口信息日志记录

發布時間:2025/3/21 编程问答 9 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用户请求接口信息日志记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用戶請求接口信息日志記錄

這樣可以詳細了解到用戶的操作記錄更加快捷方便的統計以及排錯

思路
1,定義一個注解
2,日志AOP切面類,把自定義的注解作為切點,當系統執行某一個添加了自定義注解的方法時,AOP會自動獲取該方法名稱以及用戶信息實現日志記錄


自定義注解

package com.sl.cms.annotation;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/** * @company ********* * @description 類的方法描述注解 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface MethodLogAction {/*** 方法描述* @return*/public String mdesc() default "no description";/*** 方法名稱* @return*/public String methodName() default "no method name"; }

@Target(ElementType.TYPE)——接口、類、枚舉、注解
@Target(ElementType.FIELD)——字段、枚舉的常量
@Target(ElementType.METHOD)——方法
@Target(ElementType.PARAMETER)——方法參數
@Target(ElementType.CONSTRUCTOR)——構造函數
@Target(ElementType.LOCAL_VARIABLE)——局部變量
@Target(ElementType.ANNOTATION_TYPE)——注解
@Target(ElementType.PACKAGE)——包

RetentionPolicy.SOURCE:
注解只保留在源文件,當Java文件編譯成class文件的時候,注解被遺棄;
RetentionPolicy.CLASS:
注解被保留到class文件,但jvm加載class文件時候被遺棄,這是默認的生命周期;
RetentionPolicy.RUNTIME:
注解不僅被保存到class文件中,jvm加載class文件之后,仍然存在;

@Document:說明該注解將被包含在javadoc中

@Inherited:說明子類可以繼承父類中的該注解

日志AOP切面類

@Aspect @Component public class SysLogAspect {private Logger logger = LoggerFactory.getLogger(AuthorizingRealm.class);@Autowiredprivate AdminLogService adminLogService;@Pointcut("@annotation(com.sl.cms.annotation.MethodLogAction)")public void controllerAspect(){}@After("@annotation(methodLogAction)")public void insertLog(JoinPoint joinPoint, MethodLogAction methodLogAction) throws Throwable {AdminLog log = new AdminLog();// 獲取注解中的描述String desc = methodLogAction.methodName();// 獲取請求的類名String className = joinPoint.getTarget().getClass().getName();// 獲取請求的方法名String method = joinPoint.getSignature().getName();// 獲取請求的參數Object[] args = joinPoint.getArgs();// shiro獲取用戶Authentication userinfo = SecurityContextHolder.getContext().getAuthentication();//===== 業務邏輯} }

方法上添加注解

@MethodLogAction(methodName = "方法的名稱",mdesc = "方法的描述")

總結

以上是生活随笔為你收集整理的用户请求接口信息日志记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。