Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback native VS Logback via SLF4J

I have gone through the following article regarding the logging frameworks available for Java: http://michaelandrews.typepad.com/the_technical_times/2011/04/java-logging-reconsidered.html

The author has mentioned using SLF4J with Logback. How is that different from using Logback directly. Wouldn't it be better if one uses Logback directly rather than going for SLF4J, since Logback is built on top of SLF4J.

like image 941
TheLameProgrammer Avatar asked Apr 29 '11 04:04

TheLameProgrammer


People also ask

What is the difference between Logback and SLF4J?

Logback and SLF4J can be primarily classified as "Log Management" tools. According to the StackShare community, Logback has a broader approval, being mentioned in 4 company stacks & 9 developers stacks; compared to SLF4J, which is listed in 5 company stacks and 7 developer stacks.

Does Logback use SLF4J?

Logback natively implements the SLF4J API.

Which is better log4j or SLF4J?

Comparison SLF4J and Log4j Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both. However, it is always difficult to prefer one between the two.

What is the advantage of SLF4J?

This is the main purpose of SLF4J (Simple Logging Facade for Java) – a logging abstraction which helps to decouple your application from the underlying logger by allowing it to be plugged in – at runtime. Of course, the flexibility that such an abstraction provides is the main reason to use SLF4J.


1 Answers

SLF4J is adding zero overhead to Logback since it is simply the interface that is implemented by Logback without any additional layer.

You should use SLF4J simply because...

  1. It enables you to switch away from Logback if you ever need to
  2. It does not cost you anything, even the imports are smaller ;)
  3. Other people will love you for using SLF4J and hate you for using a specific logging framework directly if you ever release your code into the wild.

The only place where you'd access Logback directly would be while (re)configuring your logging manually in an application. The need for this arises occasionally but even in that case, working with Logback would be restricted to a single class or even method.

As a rule of thumb: libraries should always use a logging abstraction while applications define the logging they are using, optionally accessing it directly.

like image 134
Huxi Avatar answered Sep 24 '22 13:09

Huxi