Class AuditLog

java.lang.Object
cloud.opencode.base.log.audit.AuditLog

public final class AuditLog extends Object
AuditLog - Audit Logging Utility AuditLog - 审计日志工具

AuditLog provides convenient static methods for recording audit events such as user actions, login/logout, and data changes.

AuditLog 提供方便的静态方法来记录审计事件,如用户操作、登录/注销和数据更改。

Example | 示例:

// Simple logging
AuditLog.log("user123", "LOGIN", "system", "SUCCESS");

// Using builder
AuditLog.event("UPDATE_USER")
    .userId("admin")
    .target("User")
    .targetId("user456")
    .success()
    .detail("field", "email")
    .build();  // Automatically logged

// Record login
AuditLog.logLogin("user123", true, "192.168.1.1");

// Record data change
AuditLog.logDataChange("admin", "Order", "ORD-001", "UPDATE", oldOrder, newOrder);

Features | 主要功能:

  • Static facade for audit logging - 审计日志的静态门面
  • Pluggable AuditLogger backend - 可插拔的 AuditLogger 后端
  • Convenience methods for login/logout/data change - 登录/注销/数据变更的便捷方法
  • Auto-logging builder pattern - 自动记录的构建器模式

Security | 安全性:

  • Thread-safe: Yes (AtomicReference for logger swap) - 线程安全: 是(AtomicReference 用于记录器切换)
  • Null-safe: Yes (null logger falls back to default) - 空值安全: 是(null 记录器回退到默认)
Since:
JDK 25, opencode-base-log V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • log

      public static void log(String userId, String action, String target, String result)
      Logs a simple audit event. 记录简单的审计事件。
      Parameters:
      userId - the user ID - 用户 ID
      action - the action - 操作
      target - the target - 目标
      result - the result - 结果
    • log

      public static void log(String userId, String action, String target, String result, Map<String,Object> details)
      Logs an audit event with details. 记录带详情的审计事件。
      Parameters:
      userId - the user ID - 用户 ID
      action - the action - 操作
      target - the target - 目标
      result - the result - 结果
      details - the details - 详情
    • log

      public static void log(AuditEvent event)
      Logs an audit event. 记录审计事件。
      Parameters:
      event - the audit event - 审计事件
    • event

      public static AuditLog.LoggingBuilder event(String action)
      Creates an audit event builder. 创建审计事件构建器。
      Parameters:
      action - the action - 操作
      Returns:
      the builder - 构建器
    • logLogin

      public static void logLogin(String userId, boolean success, String ip)
      Logs a login event. 记录登录事件。
      Parameters:
      userId - the user ID - 用户 ID
      success - whether login was successful - 登录是否成功
      ip - the IP address - IP 地址
    • logLogout

      public static void logLogout(String userId)
      Logs a logout event. 记录注销事件。
      Parameters:
      userId - the user ID - 用户 ID
    • logDataChange

      public static void logDataChange(String userId, String entity, String entityId, String action, Object before, Object after)
      Logs a data change event. 记录数据更改事件。
      Parameters:
      userId - the user ID - 用户 ID
      entity - the entity name - 实体名称
      entityId - the entity ID - 实体 ID
      action - the action (CREATE/UPDATE/DELETE) - 操作
      before - the value before change - 更改前的值
      after - the value after change - 更改后的值
    • getLogger

      public static AuditLogger getLogger()
      Gets the audit logger. 获取审计记录器。
      Returns:
      the audit logger - 审计记录器
    • setLogger

      public static void setLogger(AuditLogger logger)
      Sets the audit logger. 设置审计记录器。
      Parameters:
      logger - the audit logger - 审计记录器