How slf4j works? No log getting created

Mike picture Mike · Jan 5, 2012 · Viewed 23.2k times · Source

I've below code in Java 1.6:

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

private static Logger log = LoggerFactory.getLogger(myfile.class);

Now, I put the slf4j-api-1.6.4.jar & slf4j-simple-1.6.4.jar in classpath & code compiles fine but where is it logging all the information????

I've log.info("test"); but its not creating any log file. I tried creating log4j.properties with below content:

log4j.appender.stdout=org.apache.log4j.RollingFileAppender
log4j.appender.stdout.File=/var/abc.log
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd-MMM-yyyy HH:mm:ss}]%6p %c{1}:%L - %m%n
log4j.appender.stdout.MaxFileSize=50000KB
log4j.appender.stdout.MaxBackupIndex=200

log4j.rootLogger=info, stdout

But its not working, I know above file is required for log4j but how does slf4j works?? Do I need to create any properties file similar to log4j?? If so, where do I need to put it?

Thanks!

Answer

Ken Chan picture Ken Chan · Jan 5, 2012

SLF4J is just a façade which allows you to switch different logging frameworks easily .All the logging calls using SLF4J API will be delegated to the underlying logging framework .

You don't have to create any properties file for SLF4J .All you need to do is use the correct "SLF4J bindings" jar that matches your logging frameworks in order to delegate all the logging calls of the SLF4J API to the underlying logging framework . The following picture shows the relationship between SLF4J , "SLF4J bindings" , and the underlying logging frameworks

enter image description here

slf4j-simple-1.6.4.jar delegates all events to System.err , but not delegates to log4j API.So , you should use slf4j-log4j12-1.6.4.jar instead.

To summarizes, you should use the following jars:

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

Reference