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

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

这样可以详细了解到用户的操作记录更加快捷方便的统计以及排错

思路
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);

    @Autowired
    private 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 = "方法的描述")

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值