Class ExceptionLog
java.lang.Object
cloud.opencode.base.log.enhance.ExceptionLog
Exception Logging Enhancement - Smart Exception Logging
异常日志增强 - 智能异常日志
Provides enhanced exception logging capabilities including:
提供增强的异常日志能力,包括:
- Root cause analysis - 根因分析
- Exception chain formatting - 异常链格式化
- Deduplication (same exception logged only once) - 去重(相同异常仅记录一次)
- Rate limiting - 限速
- Exception summarization - 异常摘要
Usage Example | 使用示例:
try {
processOrder(order);
} catch (Exception e) {
// Smart logging with root cause analysis
ExceptionLog.error("Order processing failed: orderId=" + orderId, e);
// Output:
// ERROR Order processing failed: orderId=123
// Root Cause: SQLException: Connection refused
// Exception Chain: OrderProcessException -> ServiceException -> SQLException
// Log once (avoid log flooding)
ExceptionLog.errorOnce("Service unavailable", e);
// Rate-limited logging
ExceptionLog.errorRateLimited("DB connection failed", e, Duration.ofMinutes(1));
}
Features | 主要功能:
- Root cause analysis and exception chain formatting - 根因分析和异常链格式化
- Exception deduplication (log once per unique signature) - 异常去重(每个唯一签名仅记录一次)
- Rate-limited exception logging - 限速异常日志
- Compact stack trace with configurable depth - 可配置深度的紧凑堆栈跟踪
Security | 安全性:
- Thread-safe: Yes (ConcurrentHashMap + AtomicLong) - 线程安全: 是(ConcurrentHashMap + AtomicLong)
- Null-safe: Yes (handles null throwable) - 空值安全: 是(处理 null 异常)
- Since:
- JDK 25, opencode-base-log V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidClears the deduplication tracking (useful for testing) 清除去重追踪(用于测试)static voidClears the rate limit tracking (useful for testing) 清除限速追踪(用于测试)static booleancontainsExceptionType(Throwable throwable, Class<? extends Throwable> targetType) Checks if an exception contains a specific exception type in its chain 检查异常链中是否包含特定异常类型static voidLogs an error with root cause analysis 带根因分析记录错误static voidLogs an error only once per unique exception signature 每个唯一异常签名仅记录一次错误static voiderrorRateLimited(String message, Throwable throwable, Duration interval) Logs an error with rate limiting 带限速记录错误static <T extends Throwable>
TfindExceptionOfType(Throwable throwable, Class<T> targetType) Finds an exception of specific type in the causal chain 在因果链中查找特定类型的异常static StringformatExceptionChain(Throwable throwable) Formats the exception chain as a string 将异常链格式化为字符串getCausalChain(Throwable throwable) Gets the causal chain of an exception 获取异常的因果链static StringgetCompactStackTrace(Throwable throwable, int maxDepth) Gets a compact stack trace (limited depth) 获取紧凑的堆栈跟踪(有限深度)static StringgetExceptionSignature(Throwable throwable) Gets a unique signature for an exception (for deduplication) 获取异常的唯一签名(用于去重)static ThrowablegetRootCause(Throwable throwable) Gets the root cause of an exception 获取异常的根因static StringgetStackTraceString(Throwable throwable) Gets the stack trace as a string 获取堆栈跟踪字符串static StringCreates a summary of an exception 创建异常摘要static voidLogs a warning with root cause analysis 带根因分析记录警告
-
Method Details
-
error
-
errorOnce
Logs an error only once per unique exception signature 每个唯一异常签名仅记录一次错误Note: Tracks at most 10,000 unique signatures to prevent memory leaks. When the limit is reached, the tracking set is cleared and logging resumes.
- Parameters:
message- the log message | 日志消息throwable- the exception | 异常
-
errorRateLimited
-
warn
-
getRootCause
-
getCausalChain
-
formatExceptionChain
-
summarize
-
getStackTraceString
-
getCompactStackTrace
-
getExceptionSignature
-
containsExceptionType
public static boolean containsExceptionType(Throwable throwable, Class<? extends Throwable> targetType) Checks if an exception contains a specific exception type in its chain 检查异常链中是否包含特定异常类型- Parameters:
throwable- the exception | 异常targetType- the type to check for | 要检查的类型- Returns:
- true if found | 如果找到返回 true
-
findExceptionOfType
Finds an exception of specific type in the causal chain 在因果链中查找特定类型的异常- Type Parameters:
T- the exception type | 异常类型- Parameters:
throwable- the exception | 异常targetType- the type to find | 要查找的类型- Returns:
- the found exception or null | 找到的异常或 null
-
clearDeduplicationTracking
public static void clearDeduplicationTracking()Clears the deduplication tracking (useful for testing) 清除去重追踪(用于测试) -
clearRateLimitTracking
public static void clearRateLimitTracking()Clears the rate limit tracking (useful for testing) 清除限速追踪(用于测试)
-