Interface LogProvider

All Known Implementing Classes:
DefaultLogProvider

public interface LogProvider
Log Provider SPI Interface - Pluggable Logging Engine 日志提供者 SPI 接口 - 可插拔日志引擎

This interface defines the SPI contract for logging providers. Implementations adapt to specific logging frameworks like SLF4J, Log4j2, or JUL.

此接口定义日志提供者的 SPI 契约。实现适配特定的日志框架,如 SLF4J、Log4j2 或 JUL。

Implementation Guidelines | 实现指南:

  • Implementations must be thread-safe - 实现必须线程安全
  • isAvailable() should check for framework presence - isAvailable() 应检查框架是否存在
  • Lower priority values are preferred - 较低的优先级值优先
  • Provider should be registered via ServiceLoader - 提供者应通过 ServiceLoader 注册

Features | 主要功能:

  • SPI contract for pluggable logging engines - 可插拔日志引擎的 SPI 契约
  • Priority-based provider selection - 基于优先级的提供者选择
  • Lifecycle management (initialize/shutdown) - 生命周期管理(初始化/关闭)
  • MDC and NDC adapter provisioning - MDC 和 NDC 适配器提供

Usage Examples | 使用示例:

// Implement custom provider
public class MyLogProvider implements LogProvider {
    @Override
    public String getName() { return "MY_LOG"; }
    @Override
    public boolean isAvailable() { return true; }
    @Override
    public Logger getLogger(String name) { ... }
    @Override
    public MDCAdapter getMDCAdapter() { ... }
}
Since:
JDK 25, opencode-base-log V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getName

      String getName()
      Returns the name of this provider. 返回此提供者的名称。
      Returns:
      the provider name (e.g., "SLF4J", "Log4j2", "JUL") - 提供者名称
    • getPriority

      default int getPriority()
      Returns the priority of this provider. 返回此提供者的优先级。

      Lower values indicate higher priority. Default is 100.

      较低的值表示较高的优先级。默认为 100。

      Returns:
      the priority (lower = higher priority) - 优先级(越低越优先)
    • isAvailable

      boolean isAvailable()
      Checks if this provider is available. 检查此提供者是否可用。

      Should check if the underlying logging framework is present on the classpath.

      应检查底层日志框架是否存在于类路径中。

      Returns:
      true if available - 如果可用返回 true
    • getLogger

      Logger getLogger(String name)
      Gets a logger for the specified name. 获取指定名称的日志记录器。
      Parameters:
      name - the logger name - 日志记录器名称
      Returns:
      the logger instance - 日志记录器实例
    • getLogger

      default Logger getLogger(Class<?> clazz)
      Gets a logger for the specified class. 获取指定类的日志记录器。
      Parameters:
      clazz - the class - 类
      Returns:
      the logger instance - 日志记录器实例
    • getMDCAdapter

      MDCAdapter getMDCAdapter()
      Returns the MDC adapter for this provider. 返回此提供者的 MDC 适配器。
      Returns:
      the MDC adapter - MDC 适配器
    • getNDCAdapter

      default NDCAdapter getNDCAdapter()
      Returns the NDC adapter for this provider. 返回此提供者的 NDC 适配器。
      Returns:
      the NDC adapter, or null if not supported - NDC 适配器,如果不支持则返回 null
    • getLogAdapter

      default LogAdapter getLogAdapter()
      Returns the log adapter for this provider. 返回此提供者的日志适配器。
      Returns:
      the log adapter - 日志适配器
    • initialize

      default void initialize()
      Initializes the provider. 初始化提供者。

      Called once when the provider is first loaded.

      当提供者首次加载时调用一次。

    • shutdown

      default void shutdown()
      Shuts down the provider. 关闭提供者。

      Called when the logging system is shutting down.

      当日志系统关闭时调用。