Class ConditionalLog

java.lang.Object
cloud.opencode.base.log.enhance.ConditionalLog

public final class ConditionalLog extends Object
Conditional Logging - Environment and Rate-based Logging 条件日志 - 基于环境和速率的日志

Provides conditional logging capabilities including:

提供条件日志能力,包括:

  • Environment-based logging (dev/test/prod) - 基于环境的日志
  • Condition-based logging - 基于条件的日志
  • Once-only logging (per call site) - 仅一次日志(每个调用点)
  • Rate-limited logging - 限速日志

Usage Example | 使用示例:

// Development-only logging
ConditionalLog.devOnly().info("Debug info: {}", debugData);

// Conditional logging
ConditionalLog.when(config.isVerbose())
    .info("Verbose output: {}", detail);

// Log once per call site
ConditionalLog.once().warn("Deprecated: {}", deprecatedKey);

// Rate-limited logging
ConditionalLog.atMostEvery(Duration.ofMinutes(1))
    .warn("System load high: {}%", load);

Features | 主要功能:

  • Environment-based logging (dev/test/prod) - 基于环境的日志(开发/测试/生产)
  • Condition-based and lambda-condition logging - 基于条件和 Lambda 条件的日志
  • Once-only logging per call site - 每个调用点仅一次日志
  • Rate-limited logging with configurable interval - 可配置间隔的限速日志

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap + AtomicLong) - 线程安全: 是(ConcurrentHashMap + AtomicLong)
  • Null-safe: Yes (no-op when condition false) - 空值安全: 是(条件为 false 时无操作)
Since:
JDK 25, opencode-base-log V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • devOnly

      public static ConditionalLog.ConditionalLogger devOnly()
      Returns a logger that only logs in development environment 返回仅在开发环境记录日志的日志器
      Returns:
      conditional logger | 条件日志器
    • testOnly

      public static ConditionalLog.ConditionalLogger testOnly()
      Returns a logger that only logs in test environment 返回仅在测试环境记录日志的日志器
      Returns:
      conditional logger | 条件日志器
    • prodOnly

      public static ConditionalLog.ConditionalLogger prodOnly()
      Returns a logger that only logs in production environment 返回仅在生产环境记录日志的日志器
      Returns:
      conditional logger | 条件日志器
    • isDev

      public static boolean isDev()
      Checks if current environment is development 检查当前环境是否为开发环境
      Returns:
      true if development | 如果是开发环境返回 true
    • isTest

      public static boolean isTest()
      Checks if current environment is test 检查当前环境是否为测试环境
      Returns:
      true if test | 如果是测试环境返回 true
    • isProd

      public static boolean isProd()
      Checks if current environment is production 检查当前环境是否为生产环境
      Returns:
      true if production | 如果是生产环境返回 true
    • when

      public static ConditionalLog.ConditionalLogger when(boolean condition)
      Returns a logger that only logs when condition is true 返回仅在条件为真时记录日志的日志器
      Parameters:
      condition - the condition | 条件
      Returns:
      conditional logger | 条件日志器
    • when

      public static ConditionalLog.ConditionalLogger when(Supplier<Boolean> condition)
      Returns a logger that only logs when condition supplier returns true 返回仅在条件供应者返回真时记录日志的日志器
      Parameters:
      condition - the condition supplier | 条件供应者
      Returns:
      conditional logger | 条件日志器
    • once

      public static ConditionalLog.OnceLogger once()
      Returns a logger that logs only once per call site 返回每个调用点只记录一次的日志器
      Returns:
      once-only logger | 仅一次日志器
    • atMostEvery

      public static ConditionalLog.RateLimitedLogger atMostEvery(Duration duration)
      Returns a logger that logs at most once per specified duration 返回每个指定时间段最多记录一次的日志器
      Parameters:
      duration - the minimum interval between logs | 日志之间的最小间隔
      Returns:
      rate-limited logger | 限速日志器
    • clearOnceTracking

      public static void clearOnceTracking()
      Clears the once-only log tracking (useful for testing) 清除仅一次日志追踪(用于测试)
    • clearRateLimitTracking

      public static void clearRateLimitTracking()
      Clears the rate limit tracking (useful for testing) 清除限速追踪(用于测试)