How to use LoggerFactory.getLogger to output logs in Spring Boot (2)

Introduction

LoggerFactory.getLogger()Is the factory method used to get the logger in Spring Boot. It is part of the SLF4J (Simple Logging Facade for Java) framework integrated within Spring Boot. SLF4J is a general-purpose logging framework that provides an easy way to handle logging output from Java applications.

Detailed explanation

In Spring Boot applications, we usually use LoggerFactory.getLogger()methods to get logger instances. It accepts a parameter to specify the name of the class to be logged, usually we pass in the Class object of the current class to ensure that the logger is associated with the current class.

The SLF4J framework provides a variety of log levels, such as: TRACE, DEBUG, INFO, WARN and ERROR. By calling the corresponding level method on the logger, we can record different levels of log information. These log levels are used to provide appropriate information in different scenarios.

Complete example and code

Let's demonstrate a simple Spring Boot application and LoggerFactory.getLogger()log in it using:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LoggerDemoApplication {

    private static 

Guess you like

Origin blog.csdn.net/qq_29901385/article/details/131976651