Micrometer Logging Registry
LoggingMeterRegistry is a MeterRegistry that publishes metrics to a logger (or to another Consumer<String>). It is useful for inspecting metrics locally without a metrics backend (in production, use a real metrics backend instead). LoggingMeterRegistry is included in micrometer-core, so no additional dependencies are needed.
1. Setup
By default, the registry logs through the io.micrometer.core.instrument.logging.LoggingMeterRegistry logger once per minute on INFO level.
You can use it to create meters and record measurements as you would with any other registry:
MeterRegistry registry = new LoggingMeterRegistry();
registry.counter("my.counter").increment();
Keep the registry open while the application runs so that it can publish measurements in the background. Call its close method when the application shuts down and you don’t need the registry anymore.
|
2. Configuration
Use LoggingRegistryConfig to configure the publish interval and/or whether meters that have no activity in an interval should still be logged.
LoggingRegistryConfig config = new LoggingRegistryConfig() {
@Override
public String get(String key) {
return null;
}
@Override
public Duration step() {
return Duration.ofSeconds(30);
}
@Override
public boolean logInactive() {
return true;
}
};
MeterRegistry registry = new LoggingMeterRegistry(config, Clock.SYSTEM);
LoggingRegistryConfig is an interface with a set of default methods. If, in the implementation of get(String k), rather than returning null, you instead bind it to a property source (for example, a simple Map can work), you can override the default configuration through properties:
Map<String, String> properties = Map.of("logging.step", "30s", "logging.logInactive", "true");
LoggingRegistryConfig config = properties::get;
The supported properties are logging.step, logging.logInactive, and logging.enabled (set it to false to stop publishing).
| By default, meters that had no activity in the publishing interval are omitted (a time gauge counts as inactive when it reports 0). Gauges and custom meters are always published. |
2.1. Custom Output
You can replace the default logger with a Consumer<String> that receives each formatted measurement:
MeterRegistry registry = LoggingMeterRegistry.builder(LoggingRegistryConfig.DEFAULT)
.loggingSink(System.out::println)
.build();
This publishes measurements directly to standard output instead of using a logger.
To change how meter IDs are formatted, use the meterIdPrinter method of the builder. The builder also lets you set a custom Clock and ThreadFactory.
3. Sample Output
Registering the following meters on a LoggingMeterRegistry with the default configuration:
Gauge.builder("demo.gauge", Math::random).tag("app", "demo").register(registry);
Counter.builder("demo.counter").tag("app", "demo").register(registry).increment();
Timer.builder("demo.timer").tag("app", "demo").register(registry).record(Duration.ofMillis(42));
Should produce an output similar to this:
demo.counter{app=demo} delta_count=1 throughput=0.016667/s
demo.gauge{app=demo} value=0.385101
demo.timer{app=demo} delta_count=1 throughput=0.016667/s mean=0.042s max=0.042s