javaspringslf4jmicrometer

Catch all ERROR level logs to count them with Micrometer


I am integrating my Spring Boot application with Micrometer (and Prometheus and Grafana).

I would like to have a Micrometer counter that collects and shows me info (when the exception appeared) about the thrown exceptions.

My environment works great and Micrometer shows the expected info. Currently my code looks like this:

try {
   // original code
} catch ... {
   meterBuilder.getCounter(ERROR_COUNTER_NAME, MeterBuilder.Status.ERROR).increment();
  // original code    
  LOG.error(....);
  ...
}

It is really inconvenient to go through the whole application and copy/paste the same Micrometer code everywhere. I would like to improve this verbose code and hide this error-counter from the developers. They always forget to add the extra monitoring line.

My idea is to extend SLF4J somehow and have a central point where all ERROR level log events go through. Then I want to place my Micrometer code there. That could be great if this custom SLF4J hook class can be a Spring managed bean in order to I can inject configs and Micrometer beans into it.

Is that possible to implement it? Unfortunately I could not found any useful on the internet about it.


Solution

  • Try to use standard micrometer instruments like LogbackMetrics (if you are using Logback).

    LogbackMetrics counts the log events with a log level equal to or higher than the corresponding logger's effective log level.

    Example from the documentation:

    // 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);