Class LogMetrics
java.lang.Object
cloud.opencode.base.log.enhance.LogMetrics
Log Metrics - Logging with Built-in Metrics Collection
日志指标 - 带内置指标收集的日志
Provides logging methods that simultaneously record metrics, useful for integrating logging with monitoring systems.
提供同时记录指标的日志方法,用于将日志与监控系统集成。
Features | 功能:
- Log and count events - 记录日志并计数事件
- Log and time operations - 记录日志并计时操作
- Retrieve log statistics - 获取日志统计
- Thread-safe counters - 线程安全计数器
Usage Example | 使用示例:
// Log and count
LogMetrics.errorAndCount("payment.failed", "Payment failed: orderId={}", orderId);
// Records ERROR log and increments payment.failed counter
// Log and time
Object result = LogMetrics.infoAndTime("db.query", "Execute query", () -> {
return database.query(sql);
});
// Records INFO log with duration and tracks db.query timing
// Get statistics
LogStats stats = LogMetrics.getStats();
System.out.println("Total errors: " + stats.errorCount());
- Since:
- JDK 25, opencode-base-log V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordLog Statistics Snapshot 日志统计快照static classTiming Statistics 计时统计 -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddebugAndCount(String metricName, String message, Object... args) Logs at DEBUG level and increments counter 以 DEBUG 级别记录日志并增加计数器static <T> TdebugAndTime(String metricName, String message, Callable<T> task) Logs at DEBUG level with timing and records duration metric 以 DEBUG 级别带计时记录日志并记录耗时指标static voiderrorAndCount(String metricName, String message, Object... args) Logs at ERROR level and increments counter 以 ERROR 级别记录日志并增加计数器static voiderrorAndCount(String metricName, String message, Throwable throwable) Logs at ERROR level with exception and increments counter 以 ERROR 级别带异常记录日志并增加计数器static longGets the count for a specific metric 获取特定指标的计数static LogMetrics.LogStatsgetStats()Gets overall log statistics 获取整体日志统计static LogMetrics.TimingStatsgetTimingStats(String metricName) Gets timing statistics for a specific metric 获取特定指标的计时统计static voidinfoAndCount(String metricName, String message, Object... args) Logs at INFO level and increments counter 以 INFO 级别记录日志并增加计数器static voidinfoAndTime(String metricName, String message, Runnable task) Logs at INFO level with timing (void return) 以 INFO 级别带计时记录日志(无返回值)static <T> TinfoAndTime(String metricName, String message, Callable<T> task) Logs at INFO level with timing and records duration metric 以 INFO 级别带计时记录日志并记录耗时指标static voidreset()Resets all statistics (useful for testing) 重置所有统计(用于测试)static voidwarnAndCount(String metricName, String message, Object... args) Logs at WARN level and increments counter 以 WARN 级别记录日志并增加计数器
-
Method Details
-
debugAndCount
-
infoAndCount
-
warnAndCount
-
errorAndCount
-
errorAndCount
-
debugAndTime
public static <T> T debugAndTime(String metricName, String message, Callable<T> task) throws Exception Logs at DEBUG level with timing and records duration metric 以 DEBUG 级别带计时记录日志并记录耗时指标- Type Parameters:
T- the result type | 结果类型- Parameters:
metricName- the metric name | 指标名message- the log message | 日志消息task- the task to execute | 要执行的任务- Returns:
- the task result | 任务结果
- Throws:
Exception- if task fails | 如果任务失败
-
infoAndTime
public static <T> T infoAndTime(String metricName, String message, Callable<T> task) throws Exception Logs at INFO level with timing and records duration metric 以 INFO 级别带计时记录日志并记录耗时指标- Type Parameters:
T- the result type | 结果类型- Parameters:
metricName- the metric name | 指标名message- the log message | 日志消息task- the task to execute | 要执行的任务- Returns:
- the task result | 任务结果
- Throws:
Exception- if task fails | 如果任务失败
-
infoAndTime
-
getCount
Gets the count for a specific metric 获取特定指标的计数- Parameters:
metricName- the metric name | 指标名- Returns:
- the count | 计数
-
getTimingStats
Gets timing statistics for a specific metric 获取特定指标的计时统计- Parameters:
metricName- the metric name | 指标名- Returns:
- the timing stats or null | 计时统计或 null
-
getStats
Gets overall log statistics 获取整体日志统计- Returns:
- log stats | 日志统计
-
reset
public static void reset()Resets all statistics (useful for testing) 重置所有统计(用于测试)
-