Class LogMetrics

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

public final class LogMetrics extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static final record 
    Log Statistics Snapshot 日志统计快照
    static class 
    Timing Statistics 计时统计
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    debugAndCount(String metricName, String message, Object... args)
    Logs at DEBUG level and increments counter 以 DEBUG 级别记录日志并增加计数器
    static <T> T
    debugAndTime(String metricName, String message, Callable<T> task)
    Logs at DEBUG level with timing and records duration metric 以 DEBUG 级别带计时记录日志并记录耗时指标
    static void
    errorAndCount(String metricName, String message, Object... args)
    Logs at ERROR level and increments counter 以 ERROR 级别记录日志并增加计数器
    static void
    errorAndCount(String metricName, String message, Throwable throwable)
    Logs at ERROR level with exception and increments counter 以 ERROR 级别带异常记录日志并增加计数器
    static long
    getCount(String metricName)
    Gets the count for a specific metric 获取特定指标的计数
    Gets overall log statistics 获取整体日志统计
    getTimingStats(String metricName)
    Gets timing statistics for a specific metric 获取特定指标的计时统计
    static void
    infoAndCount(String metricName, String message, Object... args)
    Logs at INFO level and increments counter 以 INFO 级别记录日志并增加计数器
    static void
    infoAndTime(String metricName, String message, Runnable task)
    Logs at INFO level with timing (void return) 以 INFO 级别带计时记录日志(无返回值)
    static <T> T
    infoAndTime(String metricName, String message, Callable<T> task)
    Logs at INFO level with timing and records duration metric 以 INFO 级别带计时记录日志并记录耗时指标
    static void
    Resets all statistics (useful for testing) 重置所有统计(用于测试)
    static void
    warnAndCount(String metricName, String message, Object... args)
    Logs at WARN level and increments counter 以 WARN 级别记录日志并增加计数器

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • debugAndCount

      public static void debugAndCount(String metricName, String message, Object... args)
      Logs at DEBUG level and increments counter 以 DEBUG 级别记录日志并增加计数器
      Parameters:
      metricName - the metric name | 指标名
      message - the log message | 日志消息
      args - the arguments | 参数
    • infoAndCount

      public static void infoAndCount(String metricName, String message, Object... args)
      Logs at INFO level and increments counter 以 INFO 级别记录日志并增加计数器
      Parameters:
      metricName - the metric name | 指标名
      message - the log message | 日志消息
      args - the arguments | 参数
    • warnAndCount

      public static void warnAndCount(String metricName, String message, Object... args)
      Logs at WARN level and increments counter 以 WARN 级别记录日志并增加计数器
      Parameters:
      metricName - the metric name | 指标名
      message - the log message | 日志消息
      args - the arguments | 参数
    • errorAndCount

      public static void errorAndCount(String metricName, String message, Object... args)
      Logs at ERROR level and increments counter 以 ERROR 级别记录日志并增加计数器
      Parameters:
      metricName - the metric name | 指标名
      message - the log message | 日志消息
      args - the arguments | 参数
    • errorAndCount

      public static void errorAndCount(String metricName, String message, Throwable throwable)
      Logs at ERROR level with exception and increments counter 以 ERROR 级别带异常记录日志并增加计数器
      Parameters:
      metricName - the metric name | 指标名
      message - the log message | 日志消息
      throwable - the exception | 异常
    • 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

      public static void infoAndTime(String metricName, String message, Runnable task)
      Logs at INFO level with timing (void return) 以 INFO 级别带计时记录日志(无返回值)
      Parameters:
      metricName - the metric name | 指标名
      message - the log message | 日志消息
      task - the task to execute | 要执行的任务
    • getCount

      public static long getCount(String metricName)
      Gets the count for a specific metric 获取特定指标的计数
      Parameters:
      metricName - the metric name | 指标名
      Returns:
      the count | 计数
    • getTimingStats

      public static LogMetrics.TimingStats getTimingStats(String metricName)
      Gets timing statistics for a specific metric 获取特定指标的计时统计
      Parameters:
      metricName - the metric name | 指标名
      Returns:
      the timing stats or null | 计时统计或 null
    • getStats

      public static LogMetrics.LogStats getStats()
      Gets overall log statistics 获取整体日志统计
      Returns:
      log stats | 日志统计
    • reset

      public static void reset()
      Resets all statistics (useful for testing) 重置所有统计(用于测试)