SLF4J vs LOG4J Which one to prefer?

SLF4J is basically an abstraction layer. It is not a logging implementation. It means that if you're writing a library and you use SLF4J, you can give that library to someone else to use and they can choose which logging implementation to use with SLF4J e.g. log4j or the Java logging API. It helps prevent projects from being dependent on lots of logging APIs just because they use libraries that are dependent on them.

So, to summarise: SLF4J does not replace log4j, they work together. It removes the dependency on log4j from your library/app.


Best practice today is to use slf4j to for your own log statements and then choose the appropriate backend for it. Here log4j.
The primary reason for migrating is that you get {} in your message strings allowing you to skip the ifDebug() { log("...") } construct.



Comments