基于注解主动生成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 查看数据库
至此 添加一个注解就会将访问过的接口记录到日志了