- SLF4J - Home
- SLF4J - Overview
- SLF4J - Logging Frameworks
- SLF4J Vs Log4j
- SLF4J - Environment Setup
- SLF4J - Referenced API
- SLF4J - Hello world
- SLF4J - Error Messages
- SLF4J - Parameterized logging
- SLF4J - Migrator
- SLF4J - Profiling
SLF4J Useful Resources
SLF4J - Hello World Application
In this chapter, we will see a simple basic logger program using SLF4J. Follow the steps described below to write a simple logger.
Step 1 - Create an object of the slf4j.Logger interface
Since the slf4j.Logger is the entry point of the SLF4J API, first, you need to get/create its object
The getLogger() method of the LoggerFactory class accepts a string value representing a name and returns a Logger object with the specified name.
Logger logger = LoggerFactory.getLogger("SampleLogger");
Step 2 - Log the required message
The info() method of the slf4j.Logger interface accepts a string value representing the required message and logs it at the info level.
logger.info("Hi This is my first SLF4J program");
Example - Printing a LOG as INFO message
Following is the program that demonstrates how to write a sample logger in Java using SLF4J.
SLF4JExample.java
package com.tutorialspoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SLF4JExample {
public static void main(String[] args) {
//Creating the Logger object
Logger logger = LoggerFactory.getLogger("SampleLogger");
//Logging the information
logger.info("Hi This is my first SLF4J program");
}
}
Output
On running the following program initially, you will get the following output instead of the desired message.
SLF4J(W): No SLF4J providers were found. SLF4J(W): Defaulting to no-operation (NOP) logger implementation SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
Since we have not set the classpath to any binding representing a logging framework, as mentioned earlier in this tutorial, SLF4J defaulted to a no-operation implementation. So, to see the message you need to add the desired binding in the project classpath. Since we are using eclipse, set build path for respective JAR file or, add its dependency in the pom.xml file.
For example, if we need to use JUL (Java.util.logging framework), we need to set build path for the jar file slf4j-jdk14-x.x.jar. And if we want to use log4J logging framework, we need to set build path or, add dependencies for the jar files slf4j-log4j12-x.x.jar and log4j.jar.
After adding the binding representing any of the logging frameworks except slf4j-nopx.x.jar to the project (classpath), you can get the required output.
Adding slf4j-simple as dependency
Update pom.xml to include sl4fj-simple library.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tutorialspoint</groupId>
<artifactId>sl4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SL4JDemo</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>24</source>
<target>24</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.1.0-alpha1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.1.0-alpha1</version>
</dependency>
</dependencies>
</project>
Output
On running the SLF4JExample.java program, you will get the following desired message.
SLF4J(I): Connected with provider of type [org.slf4j.simple.SimpleServiceProvider] [main] INFO SampleLogger - Hi This is my first SLF4J program