Pages

July 06, 2017

What is the difference between SLF4J, Logback and LOG4J?

Logically these are entirely two different things.

SLF4J (Simple Logging Facade For Java) is not a logging component and it does not do the actual logging. It is only an abstraction layer to an underlying logging component. Where as Log4j is a logging component and it does the logging instructed to do.

log4j is a reliable, fast and flexible logging framework written in Java, which is distributed under the Apache Software License. It is highly configurable through external configuration files at runtime

Why is SLF4J better than Log4J? Why should you use SLF4J?
SLF4J is not a replacement for Log4j but it works along with it. It provides abstraction, which is always better because we can change the implementation as you wish and need not be tied to the dependency.

Let's say you are using SLF4J along with Log4j, in future if required Log4j can be swapped with another logging framework like Logback without compiling the source code. FYI, Logback is the reference implementation for SLF4J.

SLF4J provides placeholder based logging, which improves the readability of code by removing checks like isInforEnabled(), isDebugEnabled(), etc. e.g:

This is how we would do in Log4j:

if (logger.isDebugEnabled()) {
    logger.debug("Placing the order for customer with id: " + id + ", total amount need to be paid: " + amount);
}

On the other hand, if you use SLF4J:

logger.debug("Placing the order for customer with id: {} , total amount need to be paid: {} ", id, amount);

By using the logging method of SLF4J, we defer the cost of constructing logging messages (string), until you need it, which is both CPU and memory efficient. In SLF4J we write a log messages in a template format with a placeholder and supply actual value as parameters.

Since SLF4J uses less number of temporary strings means less work for the garbage collector, which means better throughput and performance for your application.

What are the differences Log4j and Logback? 
Log4j is a Java-based logging utility. It is an open source logging framework. With this tool – logging behavior can be controlled by editing a configuration file only without touching the application binary and can be used to store the Selenium Automation flow logs;

Logback is a logging framework for Java applications. It is intended as a successor to the popular log4j project. It is divided into three modules, logback-core, logback-classic and logback-access. The logback-core module lays the groundwork for the other two modules, logback-classic natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging frameworks and logback-access module integrates with Servlet containers, such as Tomcat and Jetty, to provide HTTP-access log functionality.

Log4j can be classified as a tool in the 'Logging Tools' category, while Logback is grouped under 'Log Management'.

-K Himaanshu Shuklaa..

No comments:

Post a Comment