Logging with SLF4J

Logging is important in software development. There are many famous frameworks to be used for logging: Log4j, Logback, java.util.logging… If we wanna change the logging framework without affecting Java code, SLF4J is the good choice.

This tutorial writes about SLF4J with example of how to apply SLF4J in some logging frameworks.

I. What is SLF4J

SLF4J, the Simple Logging Facade for Java, serves as a simple facade or abstraction for many logging frameworks above. The layer we apply logging is independent from logging implementation. We only have to choose the configuration of logging system.


To work with SLF4J, we add its dependency to our Maven Project pom file:


SLF4J API is easy to use, this is how we get a logger:

static Logger logger = LoggerFactory.getLogger(MainApp.class);

And this is a very simple example of using that Logger:

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

public class MainApp {

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

		logger.info("Hello World");

		System.err.println("Java Sample Approach");

Run the project, the console window shows:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Java Sample Approach

This warning is printed because no slf4j binding could be found on the class path. It lacks of logging implementation.


We can apply the native implementation of slf4j-api: slf4j-simple or slf4j-nop

For slf4j-nop, add this dependency after slf4j-api dependency:


Run the project, the console window shows only:

Java Sample Approach

For slf4j-simple, add this dependency after slf4j-api dependency:


Run the project, the console window shows an additional simple logging:

[main] INFO MainApp - Hello World
Java Sample Approach

Notice that, If we use more than one loggin implementation, SLF4J will emit a warning with listing the location of those bindings:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/TienTN/.m2/repository/org/slf4j/slf4j-nop/1.7.21/slf4j-nop-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/TienTN/.m2/repository/org/slf4j/slf4j-simple/1.7.21/slf4j-simple-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.helpers.NOPLoggerFactory]

It is because SLF4J API is designed to bind with ONE and only ONE underlying logging framework at a time.

II. Supported Frameworks
1. Logback

Add this dependency after slf4j-api dependency:


The Dependency Hierarchy after that is:
Run the project, the console window shows:

00:01:23.011 [main] INFO  MainApp - Hello World
Java Sample Approach
2. java.util.logging

Add this dependency after slf4j-api dependency:


The Dependency Hierarchy after that is:
Run the project, the console window shows:

Nov 21, 2016 12:12:29 AM MainApp main
INFO: Hello World
Java Sample Approach
3. Log4j

Add this dependency after slf4j-api dependency:


The Dependency Hierarchy after that is:
Run the project, the console window shows:

log4j:WARN No appenders could be found for logger (MainApp).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Java Sample Approach

It is because we haven’t set configuration for Log4j.
To do this, we can look back to the post How to configure Apache Log4j2 with XML Configuration.
Or create a configuration file (reference from http://logging.apache.org/log4j/1.2/manual.html)

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.

# A1 uses PatternLayout.
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Run the project again, the console window shows:

1    [main] INFO  MainApp  - Hello World
Java Sample Approach
III. Source code
1. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE (It’s OK if you use Eclipse)

2. Download Link


3. Check results

– Config maven build:
clean install
– Open pom.xml file and modify
– Run project with mode Java Application

59 thoughts on “Logging with SLF4J”

  1. This design is steller! You definitely know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Wonderful job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!

  2. We’re a bunch of volunteers and opening a new scheme in our community. Your website offered us with helpful information to work on. You have performed an impressive process and our entire neighborhood will be thankful to you.

  3. 855648 632993Its always good to learn tips like you share for weblog posting. As I just started posting comments for weblog and facing issue of lots of rejections. I feel your suggestion would be beneficial for me. I will let you know if its function for me too. 122054

  4. Hey. Very cool website!! Man .. Excellent .. Superb .. I’ll bookmark this website and take the feeds additionally…I’m glad to find a lot of helpful info here within the article. Thank you for sharing..

  5. Apple at this time includes Rhapsody as an app, which is a superb start, but it is at present hampered as a result of the incapacity in direction of store regionally on your iPod, and includes a dismal 64kbps bit selling price. If this changes, then it will considerably negate this usefulness for the Zune, still the Ten music for every thirty day period will however be a large moreover within just Zune Pass’ like.

  6. I keep listening to the rumor speak about getting free online grant applications so I have been looking around for the finest site to get one. Could you tell me please, where could i find some?

  7. Good article and right to the point. I am not sure if this is actually the best place to ask but do you guys have any ideea where to employ some professional writers? Thank you 🙂

  8. When I initially commented I clicked the -Notify me when new comments are added- checkbox and now every time a remark is added I get 4 emails with the identical comment. Is there any method you’ll be able to remove me from that service? Thanks!

  9. Nice post. I used to be checking constantly this weblog and I am impressed! Extremely helpful information specifically the last part 🙂 I care for such info much. I was seeking this certain information for a very long time. Thank you and good luck.

  10. I have been exploring for a little bit for any high-quality articles or weblog posts in this kind of space . Exploring in Yahoo I ultimately stumbled upon this site. Reading this information So i am glad to exhibit that I have an incredibly excellent uncanny feeling I found out just what I needed. I most no doubt will make certain to don¦t omit this website and provides it a look on a continuing basis.

  11. That is the appropriate weblog for anyone who wants to seek out out about this topic. You notice a lot its almost exhausting to argue with you (not that I actually would need…HaHa). You positively put a new spin on a subject thats been written about for years. Nice stuff, simply great!

  12. Thanks for every other fantastic post. The place else may just anybody get that kind of information in such a perfect manner of writing? I’ve a presentation next week, and I’m on the search for such info.

  13. I’m curious to find out what blog system you have been working with? I’m having some small security issues with my latest website and I’d like to find something more safeguarded. Do you have any suggestions?

  14. of course like your website however you have to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I to find it very troublesome to tell the truth on the other hand I’ll certainly come back again.

  15. My brother suggested I might like this web site. He was entirely right. This publish actually made my day. You cann’t believe just how much time I had spent for this info! Thank you!

  16. Definitely believe that which you said. Your favorite reason seemed to be on the web the easiest thing to be aware of. I say to you, I certainly get irked while people consider worries that they plainly don’t know about. You managed to hit the nail upon the top and also defined out the whole thing without having side-effects , people could take a signal. Will probably be back to get more. Thanks

  17. I am really loving the theme/design of your blog. Do you ever run into any internet browser compatibility problems? A number of my blog visitors have complained about my website not operating correctly in Explorer but looks great in Chrome. Do you have any solutions to help fix this issue?

  18. Pretty great post. I just stumbled upon your weblog and wanted to say that I’ve really enjoyed browsing your weblog posts. After all I’ll be subscribing to your feed and I’m hoping you write once more soon!

  19. Hi! This is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!

  20. Hi there, I discovered your web site by way of Google even as looking for a similar subject, your website came up, it seems great. I’ve bookmarked it in my google bookmarks.
    Hello there, simply become alert to your weblog via Google, and found that it’s really informative. I’m gonna be careful for brussels. I’ll be grateful when you proceed this in future. Numerous people shall be benefited out of your writing. Cheers!

  21. An interesting discussion is worth comment. I think that you should publish more about this issue, it may not be a taboo subject but usually folks don’t discuss such topics. To the next! Many thanks!!

  22. Heya i am for the first time here. I came across this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you helped me.

  23. Heya this is somewhat of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

  24. I would like to thnkx for the efforts you have put in writing this website. I’m hoping the same high-grade web site post from you in the upcoming as well. Actually your creative writing abilities has encouraged me to get my own website now. Actually the blogging is spreading its wings rapidly. Your write up is a great example of it.

  25. I don’t even understand how I finished up right here, however I believed this publish was great. I don’t understand who you’re however definitely you’re going to a well-known blogger in case you are not already 😉 Cheers!

  26. Having perused this I thought it was incredibly edifying. I like you contributing a few time and energy to gather this short article. I once again end up contributing a huge load of energy both examining and posting comments.But so what, it was still beneficial.

Leave a Reply

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