Class ScopedLogContext
java.lang.Object
cloud.opencode.base.log.enhance.ScopedLogContext
Scoped Log Context - JDK 25+ ScopedValue-based Context
作用域日志上下文 - 基于 JDK 25+ ScopedValue 的上下文
Provides log context management using ScopedValue, which is more efficient and suitable for Virtual Threads than traditional ThreadLocal-based MDC.
使用 ScopedValue 提供日志上下文管理,比传统基于 ThreadLocal 的 MDC 更高效且更适合虚拟线程。
Advantages over ThreadLocal | 相比 ThreadLocal 的优势:
- Automatic inheritance to child Virtual Threads - 自动继承到子虚拟线程
- Immutable within scope - 作用域内不可变
- No explicit cleanup needed - 无需显式清理
- Better performance with Virtual Threads - 虚拟线程性能更好
Usage Example | 使用示例:
// Bind and run
ScopedLogContext.where(ScopedLogContext.TRACE_ID, "trace-123")
.and(ScopedLogContext.USER_ID, "user-456")
.run(() -> {
// Context is available here
OpenLog.info("Processing request");
// Nested scope
ScopedLogContext.where(ScopedLogContext.REQUEST_ID, "req-789")
.run(() -> {
OpenLog.info("Nested processing");
});
});
Features | 主要功能:
- ScopedValue-based context (JDK 25+) - 基于 ScopedValue 的上下文(JDK 25+)
- Automatic inheritance to child virtual threads - 自动继承到子虚拟线程
- Immutable within scope, no cleanup needed - 作用域内不可变,无需清理
- Fluent carrier API for multiple bindings - 流式载体 API 支持多个绑定
Security | 安全性:
- Thread-safe: Yes (ScopedValue is inherently thread-safe) - 线程安全: 是(ScopedValue 本身线程安全)
- Null-safe: No (throws on null bindings) - 空值安全: 否(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 classCarrier for binding multiple values and executing tasks 用于绑定多个值和执行任务的载体 -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ScopedValue<String> Operation name for current operation 当前操作的操作名static final ScopedValue<String> Request ID for request tracking 请求追踪的请求 IDstatic final ScopedValue<String> Session ID for session tracking 会话追踪的会话 IDstatic final ScopedValue<String> Span ID for distributed tracing spans 分布式追踪跨度的跨度 IDstatic final ScopedValue<String> Tenant ID for multi-tenant applications 多租户应用的租户 IDstatic final ScopedValue<String> Trace ID for distributed tracing 分布式追踪的追踪 IDstatic final ScopedValue<String> User ID for user identification 用户标识的用户 ID -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TCalls a task with trace ID and user ID 使用追踪 ID 和用户 ID 调用任务get(ScopedValue<String> key) Gets a value as Optional 获取值作为 Optionalstatic StringGets the current operation name 获取当前操作名static StringGets the current request ID 获取当前请求 IDstatic StringGets the current session ID 获取当前会话 IDstatic StringGets the current span ID 获取当前跨度 IDstatic StringGets the current tenant ID 获取当前租户 IDstatic StringGets the current trace ID 获取当前追踪 IDstatic StringGets the current user ID 获取当前用户 IDstatic voidRuns a task with trace ID and user ID 使用追踪 ID 和用户 ID 运行任务static ScopedLogContext.Carrierwhere(ScopedValue<String> key, String value) Creates a carrier with a single binding 创建带单个绑定的载体static ScopedLogContext.Carrierwhere(Map<ScopedValue<String>, String> bindings) Creates a carrier with multiple bindings from a map 从映射创建带多个绑定的载体
-
Field Details
-
TRACE_ID
Trace ID for distributed tracing 分布式追踪的追踪 ID -
USER_ID
User ID for user identification 用户标识的用户 ID -
REQUEST_ID
Request ID for request tracking 请求追踪的请求 ID -
TENANT_ID
Tenant ID for multi-tenant applications 多租户应用的租户 ID -
SESSION_ID
Session ID for session tracking 会话追踪的会话 ID -
SPAN_ID
Span ID for distributed tracing spans 分布式追踪跨度的跨度 ID -
OPERATION
Operation name for current operation 当前操作的操作名
-
-
Method Details
-
where
Creates a carrier with a single binding 创建带单个绑定的载体- Parameters:
key- the scoped value key | 作用域值键value- the value to bind | 要绑定的值- Returns:
- carrier for chaining | 用于链式调用的载体
-
where
Creates a carrier with multiple bindings from a map 从映射创建带多个绑定的载体- Parameters:
bindings- the key-value bindings | 键值绑定- Returns:
- carrier for chaining | 用于链式调用的载体
-
run
-
call
Calls a task with trace ID and user ID 使用追踪 ID 和用户 ID 调用任务- Type Parameters:
T- the result type | 结果类型- Parameters:
traceId- the trace ID | 追踪 IDuserId- the user ID | 用户 IDtask- the task to call | 要调用的任务- Returns:
- the result | 结果
- Throws:
Exception- if task fails | 如果任务失败
-
getTraceId
Gets the current trace ID 获取当前追踪 ID- Returns:
- trace ID or null if not set | 追踪 ID,未设置时返回 null
-
getUserId
Gets the current user ID 获取当前用户 ID- Returns:
- user ID or null if not set | 用户 ID,未设置时返回 null
-
getRequestId
Gets the current request ID 获取当前请求 ID- Returns:
- request ID or null if not set | 请求 ID,未设置时返回 null
-
getTenantId
Gets the current tenant ID 获取当前租户 ID- Returns:
- tenant ID or null if not set | 租户 ID,未设置时返回 null
-
getSessionId
Gets the current session ID 获取当前会话 ID- Returns:
- session ID or null if not set | 会话 ID,未设置时返回 null
-
getSpanId
Gets the current span ID 获取当前跨度 ID- Returns:
- span ID or null if not set | 跨度 ID,未设置时返回 null
-
getOperation
Gets the current operation name 获取当前操作名- Returns:
- operation name or null if not set | 操作名,未设置时返回 null
-
get
Gets a value as Optional 获取值作为 Optional- Parameters:
key- the scoped value key | 作用域值键- Returns:
- optional containing value or empty | 包含值的 Optional 或空
-