Class DefaultLogProvider
java.lang.Object
cloud.opencode.base.log.spi.DefaultLogProvider
- All Implemented Interfaces:
LogProvider
Default Log Provider - Console-based Fallback Provider
默认日志提供者 - 基于控制台的回退提供者
This provider is used when no other logging framework is available. It outputs logs to the console with basic formatting.
当没有其他日志框架可用时使用此提供者。它将日志输出到控制台,带有基本格式。
Features | 主要功能:
- Console-based fallback logging provider - 基于控制台的回退日志提供者
- Automatic message formatting with {} placeholders - 使用 {} 占位符的自动消息格式化
- Built-in MDC and NDC adapter implementations - 内置 MDC 和 NDC 适配器实现
Security | 安全性:
- Thread-safe: Yes (ConcurrentHashMap + ThreadLocal) - 线程安全: 是(ConcurrentHashMap + ThreadLocal)
- Null-safe: Yes (handles null args) - 空值安全: 是(处理 null 参数)
- Since:
- JDK 25, opencode-base-log V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGets a logger for the specified name.Returns the MDC adapter for this provider.getName()Returns the name of this provider.Returns the NDC adapter for this provider.intReturns the priority of this provider.booleanChecks if this provider is available.static RunnablewrapRunnable(Runnable task) Wraps aRunnableto automatically clear MDC context after execution.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface LogProvider
getLogAdapter, getLogger, initialize, shutdown
-
Constructor Details
-
DefaultLogProvider
public DefaultLogProvider()
-
-
Method Details
-
getName
Description copied from interface:LogProviderReturns the name of this provider. 返回此提供者的名称。- Specified by:
getNamein interfaceLogProvider- Returns:
- the provider name (e.g., "SLF4J", "Log4j2", "JUL") - 提供者名称
-
getPriority
public int getPriority()Description copied from interface:LogProviderReturns the priority of this provider. 返回此提供者的优先级。Lower values indicate higher priority. Default is 100.
较低的值表示较高的优先级。默认为 100。
- Specified by:
getPriorityin interfaceLogProvider- Returns:
- the priority (lower = higher priority) - 优先级(越低越优先)
-
isAvailable
public boolean isAvailable()Description copied from interface:LogProviderChecks if this provider is available. 检查此提供者是否可用。Should check if the underlying logging framework is present on the classpath.
应检查底层日志框架是否存在于类路径中。
- Specified by:
isAvailablein interfaceLogProvider- Returns:
- true if available - 如果可用返回 true
-
getLogger
Description copied from interface:LogProviderGets a logger for the specified name. 获取指定名称的日志记录器。- Specified by:
getLoggerin interfaceLogProvider- Parameters:
name- the logger name - 日志记录器名称- Returns:
- the logger instance - 日志记录器实例
-
getMDCAdapter
Description copied from interface:LogProviderReturns the MDC adapter for this provider. 返回此提供者的 MDC 适配器。- Specified by:
getMDCAdapterin interfaceLogProvider- Returns:
- the MDC adapter - MDC 适配器
-
getNDCAdapter
Description copied from interface:LogProviderReturns the NDC adapter for this provider. 返回此提供者的 NDC 适配器。- Specified by:
getNDCAdapterin interfaceLogProvider- Returns:
- the NDC adapter, or null if not supported - NDC 适配器,如果不支持则返回 null
-
wrapRunnable
Wraps aRunnableto automatically clear MDC context after execution. 包装Runnable以在执行后自动清理 MDC 上下文。Use this when submitting tasks to thread pools or virtual thread executors to prevent MDC context leakage between tasks.
在向线程池或虚拟线程执行器提交任务时使用此方法,以防止任务之间的 MDC 上下文泄漏。
Example | 示例:
executor.submit(DefaultLogProvider.wrapRunnable(() -> { MDC.put("requestId", id); // ... task logic ... }));- Parameters:
task- the original runnable - 原始 Runnable- Returns:
- a wrapped runnable that clears MDC in a finally block - 在 finally 块中清理 MDC 的包装 Runnable
-