Logging Metrics Instrumentation

Micrometer can add metrics to different loggers:

Log4j Instrumentation

// Setting up instrumentation
new Log4j2Metrics().bindTo(registry);

// Usage example
Logger logger = LogManager.getLogger(Log4j2MetricsTest.class);
Configurator.setLevel(Log4j2MetricsTest.class.getName(), Level.INFO);
logger.info("info");
logger.warn("warn");
logger.fatal("fatal");
logger.error("error");
logger.debug("debug"); // shouldn't record a metric as per log level config
logger.trace("trace"); // shouldn't record a metric as per log level config

assertThat(registry.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(1.0);
assertThat(registry.get("log4j2.events").tags("level", "warn").counter().count()).isEqualTo(1.0);
assertThat(registry.get("log4j2.events").tags("level", "fatal").counter().count()).isEqualTo(1.0);
assertThat(registry.get("log4j2.events").tags("level", "error").counter().count()).isEqualTo(1.0);
assertThat(registry.get("log4j2.events").tags("level", "debug").counter().count()).isEqualTo(0.0);
assertThat(registry.get("log4j2.events").tags("level", "trace").counter().count()).isEqualTo(0.0);

Logback Instrumentation

// Setting up instrumentation
logbackMetrics = new LogbackMetrics();
logbackMetrics.bindTo(registry);

// Usage example
logger.setLevel(Level.INFO);

logger.warn("warn");
logger.error("error");
logger.debug("debug"); // shouldn't record a metric

assertThat(registry.get("logback.events").tags("level", "warn").counter().count()).isEqualTo(1.0);
assertThat(registry.get("logback.events").tags("level", "debug").counter().count()).isEqualTo(0.0);