基于注解主动生成API日志

基于注解主动生成API日志

第一次写博客写的不好请大家多多指导

此功能用于记录标注该注解的用户的浏览记录,使用AOP来实现功能,切到对应注解后,获取当前请求的信息,然后再通过Event异步入库.

1 首先是定义注解:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UserLog {
	String value() default "用户日志记录";
}

2 然后是Aop代码

@Aspect
@Component
public class HUserLogAspect {
	private static final Logger log = LoggerFactory.getLogger(HUserLogAspect.class);

	public HUserLogAspect() {
	}

	@Around("@annotation(userLog)")
	public Object around(ProceedingJoinPoint point, UserLog userLog) throws Throwable {
		String className = point.getTarget().getClass().getName();
		String methodName = point.getSignature().getName();
		long beginTime = System.currentTimeMillis();
		Object result = point.proceed();
		long time = System.currentTimeMillis() - beginTime;
		HTenantUserLogPublish.TenantUserLogPublishEvent(methodName, className, userLog, time);
		return result;
	}
}

3 接着是日志发送

@Component
public class HTenantUserLogPublish {
	public HTenantUserLogPublish() {
	}

	public static void TenantUserLogPublishEvent(String methodName, String methodClass, UserLog userLog, long time) {
		HttpServletRequest request = WebUtil.getRequest();
		HTenantUserLog HTenantUserLog = new HTenantUserLog();
		HTenantUserLog.setType("1");
		HTenantUserLog.setTitle(userLog.value());
		HTenantUserLog.setTime(String.valueOf(time));
		HTenantUserLog.setMethodClass(methodClass);
		HTenantUserLog.setMethodName(methodName);
		Map<String, Object> event = new HashMap(16);
		event.put("log", HTenantUserLog);
		event.put("request", request);
		SpringUtil.publishEvent(new HTenantUserLogEvent(event));
	}
}

4 日志事件

public class HTenantUserLogEvent extends ApplicationEvent {
	public HTenantUserLogEvent(Object source) {
		super(source);
	}
}

5 日志监听器

@Slf4j
@AllArgsConstructor
@Component
public class HUserLogListener {
	private final org.springblade.h5.log.service.HTenantUserLogService HTenantUserLogService;
	private final ServerInfo serverInfo;
	private final Properties Properties;

	@Async
	@Order
	@EventListener(HTenantUserLogEvent.class)
	public void saveTenantUserLog(HTenantUserLogEvent event) {
		Map<String, Object> source = (Map<String, Object>) event.getSource();
		HTenantUserLog HTenantUserLog = (HTenantUserLog) source.get(EventConstant.EVENT_LOG);
		HttpServletRequest request = (HttpServletRequest) source.get(EventConstant.EVENT_REQUEST);
		HTenantUserLog.setServiceId(Properties.getName());
		HTenantUserLog.setMethod(request.getMethod());
		...
		HTenantUserLog.setParams(WebUtil.getRequestParamString(request));
		HTenantUserLog.setCreateBy(SecurityUtils.getUserAccount(request));
		HTenantUserLog.setCreateTime(LocalDateTime.now());
		HTenantUserLogService.save(HTenantUserLog);
	}
}

6 然后再需要记录日志的地方添加注解

    @UserLog("待参加的活动列表")
    @GetMapping("/noStartActivity")
	@ApiOperation(value = "待参加的活动列表", notes = "wchatAccount", position = 1)
	public ResponseEntity<List<CommonActivityVO>> noStartActivity(String wechatAccount) {
		List<CommonActivityVO> list = myActivityService.getNoStartActivityList(wechatAccount);
		return ResponseEntity.data(list);
	}

7 重启服务然后查看日志
在这里插入图片描述
8 查看数据库
在这里插入图片描述
至此 添加一个注解就会将访问过的接口记录到日志了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值