SLF4j logging in quick hands

Tram Ho

I’m TUAN, currently a Full-stack Developer in Tokyo .
If you find this Blog interesting, please give me a like and subscribe to support me.

Today I’m going to quickly go through some of the simplest things about SLF4j.

Logging is a very important part of programming. It will help a programmer to know where the application is failing. This article will help you log with SLF4j.

Definition

Logging  Facade simple for Java (SLF4J) acts as a Facade simple or Abtraction for various logging frameworks, such as java.util.logging, logback and log4j. SLF4J allows framework users to log what they want at deployment.

(About Pattern like Facade hay Abtraction, Factory I also have a specific article maybe on this issue you can refer to know some more Basic Design Pattern that every dev should know)

How to use SLF4j.

Request

To use SLF4j, we need to add three dependencies in pom.xml.

  • slf4j-api.jar
  • logback-core.jar
  • logback-classic.jar

How to Logging?

We need to create a instance of Logger that is accessed by calling the static getLogger function from the class LoggerFactory.

Syntax:

Logger logger = LoggerFactory.getLogger("className");

ClassName is the name of the class to which logging is added. This ClassName becomes the name of the logging.

Now using this instance we can write log.

Example:

logger.debug ("Hello world.");

In the above example, the debug function is used to log the information. There are various functions that can be used to record information. Every function has meaning and can be displayed in different level located in the file logback.xml.

Other logging functions:

  • trace()
  • debug()
  • info()
  • warn()
  • error()

DONE

Logback Architecture

Logback is divided into three modules commonly known as logback-core, logback-classic and logback-access. Logback is built on top of three main classes i.e. Logger, Appender and Layout. The Logger class is part of the logback-classic module. Appender and Layout are part of logback-core.

Configuration in logback

As mentioned in the official documentation, when logback configures itself and it will follow the steps below.

  1. Logback tries to find a file named logback-test.xml in the classpath.
  2. If no such file is found, logback will try to find a file named logback.groovy in the classpath.
  3. If no such file is found, it will test the logback.xml file in the classpath.
  4. If no such file is found, service-provider (introduced in JDK 1.6) will be used to resolve the interface implementation com.qos.logback.classic .spi.Configurator by looking up the file META-INFservicesch.qos.logback.classic.spi.Configurator in the classpath. Its contents should specify the fully qualified class name of the implementation Configurator as desired.
  5. If none of the above succeeds, logback will automatically configure using [BasicConfigurator](https://logback.qos.ch/xref/ch/qos /logback/classic/BasicConfigurator.html) it will log and redirect to console (Default is console).

Appender Tag

Appender is a component that logs logging events. In the appender tag, we have a encoder tag, where we specify the format of the logging message in the template tag. There are two types of appenders  console and file. Appender has two property names and the class that defines it is file appender or console appender. We will use encoder tag while defining the subsections as provided below. It is basically used to convert event write log to  byte array and log on outputStream .

Console Appender

Just by looking at the name, it will write log on console.

The name attribute of appender is STDOUT representing appender is console appender. In the class attribute, we specify the class of the console appender. File Appender it will log to the file. If appender is file appender, it has an file tag with the path to where the logging.< /p>

Reference Documentation

official SL4J.

Roudup

As always, I hope you enjoyed this post and learned something new.

Thank you and see you in the next posts!

If you like my blog, click follow to support me. Thank you.

Ref

Share the news now

Source : Viblo