Search code examples
springlog4jlogbackslf4j

Difference log4j, slf4j and logback


What is the difference between log4j, logback and slf4j. When I need to use every of them? Can you give some examples?


Solution

  • Log4j and logback are logging APIs and SLF4J is a logging facade. i.e. SLF4J is an abstraction for various and logging frameworks such as log4j, log4j2, logback, jcl and jul.

    So you can't use SLF4J alone in your application. It needs one of the above listed logging API as its logging provider. SLF4J also provides various bridging APIs to route logging from other logging APIs to SLF4J.

    Although logback is a logging api, you can't use it without using slf4j.

    So if you want to use every one of them then you can use SLF4J and logback as your logging implementation and use log4j-over-slf4j.jar to redirect your log4j logging to SLF4J.

    See following example created by Baeldung on how to use log4j bridge with slf4j.

    https://www.baeldung.com/slf4j-with-log4j2-logback#Setup