Spring Boot Logging Basics-1

·

2 min read

Intro:-

Logging refers to the recording of activity by an application. Logs are independent of the code and can be customized at runtime.

Logging is typically of three parts: the Logger, the Formatter and the Appender (or Handler).

The Logger is responsible for capturing the message to be logged along with certain metadata and passing it to the logging framework.

After receiving the message, the framework calls the Formatter with the message which formats it for output.

The framework then pass the formatted message to the appropriate Appender/Handler for further processing. This might include output to a console display, writing to disk, appending to a database.

Task:-

you are making basic spring boot app with logger.

Software Requirements:-

Ubuntu-20.04 +jit [linux-x86_64]
Open Jdk 8
Spring Boot 2.2

Level:-

Beginner

Prerequisite:-

Knowledge of core java and text editor

Step 1:-

Using Spring Initializr(start.spring.io) download starter app by filling required parameters. Unzip the folder at your computer.

Step 2:-

Open main entry file, which in my case it is->Application.java, and write following code

static final Logger log = 
        LoggerFactory.getLogger(Application.class);


    public static void main(String[] args) throws Exception {
     log.info("Before Starting application");
        SpringApplication.run(Application.class, args);
        log.debug("Starting my application in debug with {} args", args.length);
     log.info("Starting my application with {} args.", args.length);
    }

Step 3:-

The Spring Boot starter depends upon facade (SLF4J) and frameworks (Logback).
SLF4J is a simple front-facing facade supported by several logging frameworks.

So import it in to main file.

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

Step 4:-

run the app by give following command at terminal-

mvn spring-boot:run

It will show the log in console

Screenshot from 2021-07-30 17-07-07.png

Application.java

appjava.png

Conclusion:-

After performing above steps you made a spring boot nano app,which uses slf4j logger and by making these app you get acquainted with spring boot basics and logger basics.