Class LogContext
java.lang.Object
cloud.opencode.base.log.context.LogContext
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classContext Snapshot - Immutable snapshot for async propagation. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidapply(LogContext.ContextSnapshot snapshot) Applies a context snapshot.static voidclear()Clears all context.static StringGets a custom context value.getAll()Gets all context values.static StringGets the request ID.static StringGets the tenant ID.static StringGets the trace ID.static StringGets the user ID.static voidSets a custom context value.static voidsetRequestId(String requestId) Sets the request ID.static voidsetTenantId(String tenantId) Sets the tenant ID.static voidsetTraceId(String traceId) Sets the trace ID.static voidSets the user ID.static LogContext.ContextSnapshotsnapshot()Creates a snapshot of the current context.
-
Field Details
-
KEY_TRACE_ID
- See Also:
-
KEY_REQUEST_ID
- See Also:
-
KEY_USER_ID
- See Also:
-
KEY_TENANT_ID
- See Also:
-
KEY_SPAN_ID
- See Also:
-
KEY_PARENT_SPAN_ID
- See Also:
-
-
Method Details
-
setTraceId
Sets the trace ID. 设置追踪 ID。- Parameters:
traceId- the trace ID - 追踪 ID
-
getTraceId
-
setRequestId
Sets the request ID. 设置请求 ID。- Parameters:
requestId- the request ID - 请求 ID
-
getRequestId
-
setUserId
Sets the user ID. 设置用户 ID。- Parameters:
userId- the user ID - 用户 ID
-
getUserId
-
setTenantId
Sets the tenant ID. 设置租户 ID。- Parameters:
tenantId- the tenant ID - 租户 ID
-
getTenantId
-
set
-
get
-
clear
public static void clear()Clears all context. 清空所有上下文。 -
getAll
-
snapshot
Creates a snapshot of the current context. 创建当前上下文的快照。- Returns:
- the context snapshot - 上下文快照
-
apply
Applies a context snapshot. 应用上下文快照。- Parameters:
snapshot- the snapshot to apply - 要应用的快照
-