Search code examples
javalogginglogbackslf4j

Make a custom SLF4J Logger


I have an application that uses slf4j for logging. Now I want to add a functionality that whenever I log an error a specific url get called. Currently I added a wrapper method in application level that sends the http request then calls the logger method. But this way I miss some of the outputs and some exceptions just get logged without having the corresponding request sent. I am trying to make my custom logger class. My problem is which classes should I extend (or interface to implement). I can't extend the Logger class because its constructor is not public. Implementing the Logger interface seems daunting because all the methods are abstract. The NOPLogger doesn't have these problems but its info (or error...) methods are final and cannot be extended and they do nothing, guess hence the name. I have already implemented the ILoggerFactory interface to return an instance of my own Logger.


Solution

  • If your logger library is Logback, you could misuse filters for this. Just let everything pass your filter and if it is an error, additionally send your HTTP request.