Search code examples
javalogbackspring-logback

Logback: perform custom action on ERROR level log


With logback, I am looking for a way to perform a custom action every time a certain number of ERROR logs occurs. In the past, with log4j, I was able to do this by either subclassing some logger base class or implementing some logger interface (I can't remember exactly). I haven't been able to find the same functionality with my searches for logback.

Does anyone know a way (I have a spring boot application) to do this? Basically I want to capture the string message of all ERROR logs, search for specific text, and then count them and when a threshold is reached, publish a custom Prometheus metric.

Just looking for the thing to subclass/implement here, not a full solution.

Thank you


Solution

  • Probably many options in Logback, but extending an appender works nice. Something like ch.qos.logback.classic.AsyncAppender that implements ch.qos.logback.core.spi.AppenderAttachable.

    I did this, not gonna post the whole solution since you don't want that, but you can look at the AsyncAppender, I basically did the same thing:

    class MyOwnAppender extends UnsynchronizedAppenderBase<ILoggingEvent> implements AppenderAttachable<ILoggingEvent>