How to get SLF4J "Hello World" working with log4j?

ripper234 picture ripper234 · Nov 30, 2010 · Viewed 107.1k times · Source

The "Hello World" example from SLF4J is not working for me. I guess this is because I added slf4j-log4 to my classpath. Should I configure log4j directly for the hello world to work?

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

Update: I added log4j initialization, and it still doesn't work:

public static void main(String[] params) {
        org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender());
        Logger logger = org.slf4j.LoggerFactory.getLogger(TestBase.class);
        logger.info("Hello World");
    }

And I'm getting:

log4j:ERROR No output stream or file set for the appender named [null].

Answer

darioo picture darioo · Nov 30, 2010

If you want to use slf4j simple, you need these jar files on your classpath:

  • slf4j-api-1.6.1.jar
  • slf4j-simple-1.6.1.jar

If you want to use slf4j and log4j, you need these jar files on your classpath:

  • slf4j-api-1.6.1.jar
  • slf4j-log4j12-1.6.1.jar
  • log4j-1.2.16.jar

No more, no less. Using slf4j simple, you'll get basic logging to your console at INFO level or higher. Using log4j, you must configure it accordingly.