用户请求接口信息日志记录
用戶請求接口信息日志記錄
這樣可以詳細了解到用戶的操作記錄更加快捷方便的統計以及排錯
思路
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 = "方法的描述")
總結
以上是生活随笔為你收集整理的用户请求接口信息日志记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构 - 递归
- 下一篇: 结果集ResultDTO