SLF4J vs Log4j

Which one to use in my project either SLF4J or Log4j or both? Its been long since SLF4J came into picture and adopted heavily by projects all around. But certain things never get over. Like the difference between abstract class and interface in Java. Difference between SLF4j and Log4j is still a popular questions among ‘loggers’, should I say newbie loggers? In SO, still there are questions raised on this topic. So now its time we deal with it in Java Papers.

However we elaborate this question deserves only a one line answer. The question itself is wrong. SLF4J and Log4J focus on different areas and they are not similar components. SLF4 is a logging facade. The name says it all. SLF4J is Simple Logging Facade for Java. It is not a logging component and it does not do the actual logging. It is only an abstraction layer to an underlying logging component.

In the case of Log4j, it is a logging component and it does the logging instructed to do. Logically these are entirely two different things. So what would be the right question. Do I need an abstraction logging layer in my project. That is, should I use SLF4J. If you want to compare SLF4J, then the right comparison would be to compare with Apache Commons Logging.

what

Should I use SLF4J (Logging Abstraction)?

This Java tutorial was added on 01/02/2016.

Comments on "SLF4J vs Log4j" Tutorial:

  1. deepak says:

    short and crispm

  2. rk verma says:

    Not clear?

  3. harish Kumar says:

    Nice Explanation. Thank You

  4. Rakesh says:

    Thanks for sharing. But could you please explain the slf4j part with an example using logback or any other logging component?

  5. suyash says:

    About performance, I have read on internet that SLF4j helps in performance as it uses string immutability concept very well. For logging it provides place holders which will be replaced during runtime. So the no of string objects being created is less.
    I am not sure how much impact this will add to improve performance, but I can think on this point when it comes to performance optimization with SLF4j.

  6. Mohan Kumar says:

    Thank you Joe.. I like your blog a lot than a text book. Very nicely articulated. SLF4J is new for me.. It would be better if a short article on it is published..

    We use log4J only but i came to know about other frameworks.. Thank you.

  7. Anonymous says:

    thankss

  8. vikranth says:

    Not so clear, need more clarity to differentiate it

  9. sandhya says:

    nice joe… thank you so much.. we are using log4j currently i can suggest my team to do new development in slf4j it will be better option i guess

Comments are closed for this "SLF4J vs Log4j" tutorial.