Class LogContext

java.lang.Object
cloud.opencode.base.log.context.LogContext

public final class LogContext extends Object
Log Context - Unified Context Management 日志上下文 - 统一上下文管理

LogContext provides a unified API for managing log context information such as trace IDs, request IDs, and user IDs. It integrates with MDC and provides convenient methods for common context operations.

LogContext 提供统一的 API 来管理日志上下文信息, 如追踪 ID、请求 ID 和用户 ID。它与 MDC 集成,并为常见上下文操作提供便捷方法。

Standard Keys | 标准键:

  • traceId - Distributed trace ID - 分布式追踪 ID
  • requestId - HTTP request ID - HTTP 请求 ID
  • userId - User identifier - 用户标识符
  • tenantId - Multi-tenant ID - 多租户 ID

Example | 示例:

// Set standard context
LogContext.setTraceId("trace-12345");
LogContext.setUserId("user-001");

try {
    OpenLog.info("Processing request");  // Includes traceId and userId
} finally {
    LogContext.clear();
}

// Snapshot for async propagation
ContextSnapshot snapshot = LogContext.snapshot();
executor.submit(() -> {
    snapshot.runWith(() -> {
        OpenLog.info("Async task");  // Preserves context
    });
});

Features | 主要功能:

  • Unified context management for trace/request/user/tenant IDs - 统一的追踪/请求/用户/租户 ID 上下文管理
  • Context snapshot for async propagation - 用于异步传播的上下文快照
  • Automatic context restore after async execution - 异步执行后自动恢复上下文

Security | 安全性:

  • Thread-safe: Yes (delegates to thread-safe MDC) - 线程安全: 是(委托给线程安全的 MDC)
  • Null-safe: Yes (returns null for missing keys) - 空值安全: 是(缺少的键返回 null)
Since:
JDK 25, opencode-base-log V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Method Details

    • setTraceId

      public static void setTraceId(String traceId)
      Sets the trace ID. 设置追踪 ID。
      Parameters:
      traceId - the trace ID - 追踪 ID
    • getTraceId

      public static String getTraceId()
      Gets the trace ID. 获取追踪 ID。
      Returns:
      the trace ID - 追踪 ID
    • setRequestId

      public static void setRequestId(String requestId)
      Sets the request ID. 设置请求 ID。
      Parameters:
      requestId - the request ID - 请求 ID
    • getRequestId

      public static String getRequestId()
      Gets the request ID. 获取请求 ID。
      Returns:
      the request ID - 请求 ID
    • setUserId

      public static void setUserId(String userId)
      Sets the user ID. 设置用户 ID。
      Parameters:
      userId - the user ID - 用户 ID
    • getUserId

      public static String getUserId()
      Gets the user ID. 获取用户 ID。
      Returns:
      the user ID - 用户 ID
    • setTenantId

      public static void setTenantId(String tenantId)
      Sets the tenant ID. 设置租户 ID。
      Parameters:
      tenantId - the tenant ID - 租户 ID
    • getTenantId

      public static String getTenantId()
      Gets the tenant ID. 获取租户 ID。
      Returns:
      the tenant ID - 租户 ID
    • set

      public static void set(String key, String value)
      Sets a custom context value. 设置自定义上下文值。
      Parameters:
      key - the key - 键
      value - the value - 值
    • get

      public static String get(String key)
      Gets a custom context value. 获取自定义上下文值。
      Parameters:
      key - the key - 键
      Returns:
      the value - 值
    • clear

      public static void clear()
      Clears all context. 清空所有上下文。
    • getAll

      public static Map<String,String> getAll()
      Gets all context values. 获取所有上下文值。
      Returns:
      the context map - 上下文映射
    • snapshot

      public static LogContext.ContextSnapshot snapshot()
      Creates a snapshot of the current context. 创建当前上下文的快照。
      Returns:
      the context snapshot - 上下文快照
    • apply

      public static void apply(LogContext.ContextSnapshot snapshot)
      Applies a context snapshot. 应用上下文快照。
      Parameters:
      snapshot - the snapshot to apply - 要应用的快照