Intercept Log Messages slf4j
Asked Answered
C

1

17

I have an application which uses slf4j as the logging facade. Now I would like to intercept all the error messages before the transfer is handed out to the underlying logging system. Is it possible to do that for slf4j? I looked through the documentation and see that we can change the Appenders in the implementation (like log4j) to achieve this but can we do this at the facade level itself? The intercept does basic stuff like incrementing a global counter for the number of error messages etc.

Cantal answered 19/12, 2012 at 15:59 Comment(2)
It does not sound like a good design decision to me. In future, you may want to turn this off using configuration; perhaps you have a specific requirement / constraints that you want to share with us.Floret
The requirement is as simple as the one I mentioned. Look through error logs and send out a notification if a threshold is met? I was searching a bit and I found specific Appenders which kinda do this for the implementations linkCantal
R
9

There is nothing for doing this in the SLF4J API.

But if your logger is Logback you can do it using a Filter like TurboFilter.

If you're not using Logback you'll probably need something like an AspectJ interceptor.

Runnels answered 19/12, 2012 at 16:56 Comment(1)
And if the particular use case is to get log metrics, there are appenders available which achieve this from Codahale Metrics linkCantal

© 2022 - 2024 — McMap. All rights reserved.