21
votes

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!

3
SLF4J is just a logging interface. You need a logging implementation on the classpath. Do you have one? - Mike Yockey
you import slf4j-simple-1.6.4.jar, but you're using log4j as your logger, that's the problem. - Rangi Lin

3 Answers

43
votes

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

1
votes

SLF4J is just a logging abstraction - it doesn't write any logs itself. Use Logback - it implements SLF4J natively and is written by the same guy who wrote SLF4J and log4j. SLF4J and Logback are a great framework - we love how SLF4J bridges other frameworks (JUL, Commons Logging, log4j) to our framework of choice (which is Logback).

0
votes

I used slf4j-api (jar). I replaced the dependency(jar) logback-core with logback-classic then it started logging.