metrics-logger is a crate for logging metrics. It aids development and testing by allowing developers to view metrics without setting up a network endpoint.
This is achieved by implementing the Recorder trait from the metrics crate.
- This crate exports the
metricscrate to avoid version mismatches. The version ofmetrics-loggermatches that of themetricscrate. MetricsLoggerrequires callbacks to avoid issues with different versions of thelogortracingcrates in the user's project.
use metrics_logger::{metrics, MetricsLogger, LogMode};
// MetricsLogger implements the Recorder trait
let recorder = MetricsLogger::new(
LogMode::Periodic(10), // Logging interval in seconds
|logs| println!("Metrics: {}", logs), // Logging callback
|err| eprintln!("Error: {}", err), // Error callback
);
// This tells the metrics crate to use your Recorder implementation.
metrics::set_global_recorder(recorder).unwrap();cmd: Handles commands for updating metrics.handles: Implements metric handles (e.g., counters, gauges, histograms).state: Manages metric state and generates logs.
This library re-exports the metrics crate to ensure compatibility with the same version used internally.