Intercept Log Messages slf4j

2020-08-09 17:46发布

问题:

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.

回答1:

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.