SLF4j Vs Log4j – Which One is Better?

Lokesh Gupta

Which logging framework we should use in our new application? I have been asked this question multiple times, so I thought to write down my answer in this post so fellow developers can refer to it from time to time when needed.

1. SLF4j acts like abstract Logging API

Simple Logging Facade for Java (SLF4J) is an API designed to give generic access to many logging frameworks like Logback, Apache commons logging, java.util.logging; Log4j being one of them.

When using SLF4j, which logging implementation will be used is decided at deployment time, not when we write the application code.

SLF4j acts like a facade or abstraction where other logging libraries can be seen as implementations. We can replace the logging framework without changing the source code.

The best practice is to use SLF4j for writing the log statements, and then choose the appropriate backend for it including Log4j among others. Please note that if no logging framework is found then SLF4J will default to a no-operation implementation.

2. Making SLF4j and Log4j act together

To choose which logging framework we will use in runtime, we have to include two kind of jar files:

  1. slf4j-api-${latest.stable.version}.jar (Many logging frameworks have implemented it in their core library)
  2. Desired logging framework jar files

For example, to use Log4j2 in the application, we will need to include the below-given jar files.

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.15.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.15.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.15.0</version>
</dependency>

Once you have placed both jar files in the application classpath, SLF4j will automatically detect it and start using Log4j2 for processing the log statements based on the configuration we provide in log4j2 configuration file.

status = error
name = PropertiesConfig

filters = threshold

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appenders = console

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

Let us now write the following logging statements in our application classes:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld
{
    public static void main(String[] args)
    {
        Logger logger = LoggerFactory.getLogger(HelloWorld.class);

        logger.info("An info message");
        logger.debug("A debug message");
    }
}

Run the application and check the console. We will see the log statement.

2021-12-13 19:55:49 INFO  Main:10 - Hello World !!

In the future, if we want to replace log4j with any other logging framework – All you have to do is replace the binding and logging jar files (along with the configuration file). It’s easy. No need to change the actual source code files.

3. Conclusion

So essentially, SLF4J does not replace Log4j, Both work together.

SLF4j removes the tight coupling between the application and logging frameworks. It makes it easy to replace with any other logging framework in the future with a more capable library.

Happy Learning !!

Comments

Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode