POM依赖

  1. <!--日志处理-->
  2. <dependency>
  3. <groupId>com.pig4cloud</groupId>
  4. <artifactId>pig-common-log</artifactId>
  5. <version>x.x.x</version>
  6. </dependency>

@SysLog 注解

  • 接口上使用@SysLog 注释当前接口的作用即可
  1. @SysLog("添加终端")
  2. @PostMapping
  3. @PreAuthorize("@pms.hasPermission('sys_client_add')")
  4. public R add(@Valid @RequestBody SysOauthClientDetails sysOauthClientDetails) {
  5. return new R<>(sysOauthClientDetailsService.save(sysOauthClientDetails));
  6. }

原理讲解

  • AOP 切面获取当前请求的注解值,并 异步 发送时间,减少日志操作的性能损耗
  1. @Aspect
  2. @Slf4j
  3. public class SysLogAspect {
  4. @Around("@annotation(sysLog)")
  5. public Object around(ProceedingJoinPoint point, SysLog sysLog) throws Throwable {
  6. String strClassName = point.getTarget().getClass().getName();
  7. String strMethodName = point.getSignature().getName();
  8. log.debug("[类名]:{},[方法]:{}", strClassName, strMethodName);
  9. SpringContextHolder.publishEvent(new SysLogEvent(logVo));
  10. return obj;
  11. }
  12. }
  • 监听器在接收到日志事件后进行调用feign入口处理
  1. @Slf4j
  2. @AllArgsConstructor
  3. public class SysLogListener {
  4. private final RemoteLogService remoteLogService;
  5. @Async
  6. @Order
  7. @EventListener(SysLogEvent.class)
  8. public void saveSysLog(SysLogEvent event) {
  9. SysLog sysLog = (SysLog) event.getSource();
  10. remoteLogService.saveLog(sysLog, SecurityConstants.FROM_IN);
  11. }
  12. }

异步操作说明 @EnableAsync

@EnableAsync 注解启用了 Spring 异步方法执行功能,在 Spring Framework API 中有详细介绍。

❤ 问题咨询

手势点击蓝字求关注简约风动态引导关注__2022-09-07+23_18_38.gif