Class AuditLogAPMEventListener

All Implemented Interfaces:
EventListener, org.springframework.context.ApplicationListener<APMEvent>

public class AuditLogAPMEventListener extends AbstractAuditLogAPMEventListener
审计日志APM事件监听器实现类

继承自AbstractAuditLogAPMEventListener,提供默认的审计日志处理逻辑

主要功能: 1. 从Swagger注解中提取业务模块标签(@Tag.description) 2. 从Swagger注解中提取业务操作名称(@Operation.summary) 3. 根据@APMIgnore注解或Swagger注解是否存在来决定是否跳过记录 4. 使用Nutz Dao进行数据库操作(创建表和插入数据)

跳过记录的条件: - 方法为null - 方法标记了@APMIgnore注解 - 无法从@Tag注解获取业务模块标签 - 无法从@Operation注解获取业务操作名称

使用场景: - 适用于使用Swagger/OpenAPI注解的项目 - 可以直接复用,无需自定义实现

Author:
Kerbores(kerbores@riemann.tech)
  • Constructor Details

    • AuditLogAPMEventListener

      public AuditLogAPMEventListener(org.nutz.dao.Dao dao, String application, TableSuffixProvider tableSuffixProvider, boolean writeToDatabase)
      构造审计日志监听器
      Parameters:
      dao - Nutz Dao对象
      application - 应用名称
      tableSuffixProvider - 表名后缀提供者
      writeToDatabase - 是否将审计日志写入数据库
  • Method Details

    • tagName

      public String tagName(@Nullable Class<?> clazz, @Nullable Method method)
      获取业务模块标签

      从类的@Tag注解中提取description作为业务模块标签

      Specified by:
      tagName in class AbstractAuditLogAPMEventListener
      Parameters:
      clazz - 类
      method - 方法
      Returns:
      业务模块标签,如果无法获取则返回空字符串
    • operationName

      public String operationName(@Nullable Class<?> clazz, @Nullable Method method)
      获取业务操作名称

      从方法的@Operation注解中提取summary作为业务操作名称

      Specified by:
      operationName in class AbstractAuditLogAPMEventListener
      Parameters:
      clazz - 类
      method - 方法
      Returns:
      业务操作名称,如果无法获取则返回空字符串
    • ignore

      public boolean ignore(APMLog log, @Nullable Class<?> clazz, @Nullable Method method)
      判断是否跳过记录审计日志

      跳过条件: 1. 方法为null 2. 方法标记了@APMIgnore注解 3. 无法获取业务模块标签(tagName为空) 4. 无法获取业务操作名称(operationName为空)

      Specified by:
      ignore in class AbstractAuditLogAPMEventListener
      Parameters:
      log - APM日志
      clazz - 类
      method - 方法
      Returns:
      true表示跳过,false表示记录
    • createTable

      public void createTable(String suffix)
      创建审计日志分表

      使用Nutz Dao根据AuditLog实体类创建表结构

      M30: 同一分表后缀只建一次——已建表直接跳过 DDL,避免每条日志重复建表

      Specified by:
      createTable in class AbstractAuditLogAPMEventListener
      Parameters:
      suffix - 表名后缀
    • saveLog

      public void saveLog(String suffix, AuditLog log)
      保存审计日志

      使用Nutz Dao将审计日志插入到数据库

      Specified by:
      saveLog in class AbstractAuditLogAPMEventListener
      Parameters:
      suffix - 表名后缀
      log - 审计日志对象