Logging your application is an important aspect. It will help a programmer to see where an application is failing. This article will help you with how to log an application using SLF4j.
Definition
The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks, such as java.util.logging, logback, and log4j. SLF4J allows the end-user to plug in the desired logging framework at deployment time.
How to use SLF4j.
Requirements
In order to use SLF4j, we require three dependencies to be added in the pom.xml file.
- slf4j-api.jar
- logback-core.jar
- logback-classic.jar
How to add Logging?
We need to make the instance of the Logger retrieved by invoking the static getLogger method from the LoggerFactory class.
The syntax is given below:
Logger logger = LoggerFactory.getLogger("className");
The className is the name of the class in which logging is added. This className become the name of the logger.
Now using this instance, we can write the logging statement.
For example:
logger.debug("Hello world.");
In the above example debug method is used to log the information. There are various methods that can be used to log the information. Every method has it’s meaning and can be displayed according to the log level set in the logback.xml file.
Various methods of logging are :
- trace()
- debug()
- info()
- warn()
- error()
Logback’s architecture
Logback is divided into three modules commonly known as logback-core, logback-classic, and logback-access. Logback is built upon three main classes i.e. Logger, Appender, and Layout. The Logger class is part of the logback-classic module. Appender and Layout interfaces are part of logback-core.
Configuration in logback
As mentioned in the official documentation, when logback configures itself and it follows the below steps.
- Logback tries to find a file called logback-test.xml in the classpath.
- If no such file is found, logback tries to find a file called logback.groovy in the classpath.
- If no such file is found, it checks for the file logback.xml in the classpath.
- If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of
com.qos.logback.classic.spi.Configurator
interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the classpath. Its contents should specify the fully qualified class name of the desiredConfigurator
implementation. - If none of the above succeeds, logback configures itself automatically using the
BasicConfigurator
which will cause logging output to be directed to the console.
Appender Tag
Appender is a component that has the task to write the logging events. In appender tag, we have the encoder tag in which we specify the format of the logging message in pattern tag. There are two types of appender, console and file appender. Appender has two attribute name and class which specify whether it is a file appender or console appender. We will be using the encoder tag while defining the appenders as given below. It is basically, used to convert the log event into the byte array and writes the log on the outputStream.
Console Appender
As the name suggests it will append the log on the console.
The name attribute of appender is STDOUT that represents the appender is console appender. In the class attribute, we specify the class of the console appender.
File Appender
It will append the log in the file. If appender is file appender, then it has the file tag that has the path of the destination where logging file is stored.
Reference
Official documentation of SL4J.