What is the difference between SLF4J and LOG4J?

Last modified date

Comments: 0

SLF4J and LOG4J are not comparable as they are two separate things.

LOG4J is a logging framework, and SLF4J is a logging abstraction that uses logging frameworks like LOG4J under the hood.

There are two major benefits of using the SLF4J abstraction rather than using loggers like LOG4J directly.

SLF4J is a logging facade

SLF4J doesn’t do logging, it instead uses an underlying logging framework of your choice.

This is useful since the SLF4J logging framework is generic and means your code isn’t coupled to a particular logging framework and the underlying framework can be easily replaced if needed.

String concatenation

String concatenation is an expensive process and something would have to be performed in your code if you were using pure LOG4J.

However SLF4J uses placeholders instead of string concatenation:

String userName = JakTech;
log.info("Performing request for user {}", userName);

The {} placeholder is more performant as the placeholder is replaced by the string at runtime, rather than a new object being created.

Reference

https://medium.com/@coviamtech/slf4j-vs-log4j-91b578ce0bc3

JakTech

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.