SLF4J Simple Example

SLF4J binding with simple (slf4j-simple)

Here is an example illustrating the simplest way to output "Hello world" using SLF4J-Simple. It begins by getting a logger with the name "HelloWorld". This logger is in turn used to log the message "Hello World".

package com.javavillage.slf4j;

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

public class SLF4JSample {

	private static Logger LOGGER = LoggerFactory.getLogger(SLF4JSample.class);
    public static void main(String[] args) {
        LOGGER.info("Hi, Welcome to SLF4J exmaple");
    }

}
for now I am adding below dependency for maven pom.xml
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.7.13</version>
</dependency>
Compiling and running HelloWorld will result in the following output being printed on the console.
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.
This warning is printed because no slf4j binding could be found on your class path.

The warning will disappear as soon as you add a binding to your class path. Assuming you add slf4j-simple-1.7.13.jar so that your class path contains:
now I am adding slf4j-simple dependency to to my pom.
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-simple</artifactId>
	<version>1.7.13</version>
</dependency>

Now you see logging output on STDOUT with INFO level. This simple logger will default show any INFO level message or higher.

[main] INFO com.javavillage.slf4j.SLF4JSample - Hi, Welcome to SLF4J exmaple

In order to see DEBUG messages, you would need to pass in this System Property
-Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG

at your Java startup.