Class ConditionalLog
java.lang.Object
cloud.opencode.base.log.enhance.ConditionalLog
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classLogger that only logs when a condition is true 仅在条件为真时记录日志的日志器static classLogger that logs only once per call site 每个调用点只记录一次的日志器static classLogger that logs at most once per specified duration 每个指定时间段最多记录一次的日志器 -
Method Summary
Modifier and TypeMethodDescriptionatMostEvery(Duration duration) Returns a logger that logs at most once per specified duration 返回每个指定时间段最多记录一次的日志器static voidClears the once-only log tracking (useful for testing) 清除仅一次日志追踪(用于测试)static voidClears the rate limit tracking (useful for testing) 清除限速追踪(用于测试)devOnly()Returns a logger that only logs in development environment 返回仅在开发环境记录日志的日志器static booleanisDev()Checks if current environment is development 检查当前环境是否为开发环境static booleanisProd()Checks if current environment is production 检查当前环境是否为生产环境static booleanisTest()Checks if current environment is test 检查当前环境是否为测试环境static ConditionalLog.OnceLoggeronce()Returns a logger that logs only once per call site 返回每个调用点只记录一次的日志器prodOnly()Returns a logger that only logs in production environment 返回仅在生产环境记录日志的日志器testOnly()Returns a logger that only logs in test environment 返回仅在测试环境记录日志的日志器when(boolean condition) Returns a logger that only logs when condition is true 返回仅在条件为真时记录日志的日志器Returns a logger that only logs when condition supplier returns true 返回仅在条件供应者返回真时记录日志的日志器
-
Method Details
-
devOnly
Returns a logger that only logs in development environment 返回仅在开发环境记录日志的日志器- Returns:
- conditional logger | 条件日志器
-
testOnly
Returns a logger that only logs in test environment 返回仅在测试环境记录日志的日志器- Returns:
- conditional logger | 条件日志器
-
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
Returns a logger that only logs when condition is true 返回仅在条件为真时记录日志的日志器- Parameters:
condition- the condition | 条件- Returns:
- conditional logger | 条件日志器
-
when
Returns a logger that only logs when condition supplier returns true 返回仅在条件供应者返回真时记录日志的日志器- Parameters:
condition- the condition supplier | 条件供应者- Returns:
- conditional logger | 条件日志器
-
once
Returns a logger that logs only once per call site 返回每个调用点只记录一次的日志器- Returns:
- once-only logger | 仅一次日志器
-
atMostEvery
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) 清除限速追踪(用于测试)
-