Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How slf4j works? No log getting created

Tags:

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!

like image 568
Mike Avatar asked Jan 05 '12 03:01

Mike


People also ask

Can SLF4J work without log4j?

Conclusion. So essentially, SLF4J does not replace Log4j, Both work together. SLF4j removes the tight coupling between the application and logging frameworks. It makes it easy to replace with any other logging framework in the future with a more capable library.

How does SLF4J work?

SLF4J standardized the logging levels, which are different for the particular implementations. It drops the FATAL logging level (introduced in Log4j) based on the premise that in a logging framework we should not decide when to terminate an application. The logging levels used are ERROR, WARN, INFO, DEBUG and TRACE.

Is SLF4J internally using log4j?

So essentially, SLF4J does not replace log4j; they both work together. It removes the dependency on log4j from your application and makes it easy to replace it in the future with the more capable library.


2 Answers

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

  • SLF4J user manual
like image 101
Ken Chan Avatar answered Sep 27 '22 18:09

Ken Chan


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).

like image 36
Paul Avatar answered Sep 27 '22 19:09

Paul