Use Lombok@Slf4j to complete the log in SpringBoot

Table of contents

1. Why use logs

2. Basic use of logs

3. Log level

4. Advantages of logs

5. About SLF4j and other logging frameworks


1. Why use logs

If you always use output statements like "System.out.println()" to output certain information, it may not matter during the development process, but after the project development is completed, this information should no longer be displayed. If you use delete The practice of source code and annotating source code requires a large workload , and it is not conducive to subsequent functional updates or maintenance that may be required .

Using logs, you can output according to the level, and set different display levels according to different operating environments (development environment, post-delivery production environment) to ensure that certain information is only output and visible during the development process, while This information will not be visible in the production environment after delivery.

2. Basic use of logs

In a Spring Boot project, when Lombok dependencies are added , @Slf4j annotations can be added to any class , and a variable named log can be used in the class to call a method to output logs (this variable is added by Lombok at compile time ).

2.1 When calling the method of outputting logs, the recommended method is:

public void info(String format, Object... arguments);

That is: the first parameter is a string , indicating the content to be output , but each variable value of this string uses {} as a placeholder , and then, the second parameter is a variable parameter , which is of type Object , then The values ​​corresponding to the placeholders in the string value of the first parameter are represented sequentially by the second parameter.

2.2 Example of use:

int m = 2022;
int n= 1225;
String a="This is a sample"
log.info("{}:m的值为{}, n的值为{}, m+n的值为{}", a , m, n, m+n);

3. Log level

3.1 Display level

In SLF4j, the displayable levels of logs, from the importance of information, from low to high are:

- trace: trace information

- debug: debug

- info: general information

- warn: warning

- error: error

The above 5 levels have corresponding methods when using the log variable to output logs. The method names are the names of these 5 levels, and the overloads of the methods of these 5 levels are the same!

Depending on the method called, the level of the output log is different. For example, when calling the info() series of methods (multiple overloaded methods), the output log is at the info level.

When the debug() series of methods are called, the output log is at the debug() level.

3.2 Set the display level of the log

In the Spring Boot project, in application.properties, use the following syntax to set the display level of the log:

logging.level. package name. class name = log display level

example

logging.level.com.example.demo.demo=debug

The above configuration can set the log in the specified class to the specified display level.

Can also be configured as:

logging.level.package name= log display level

example

logging.level.com.example.demo=debug

The above configuration can set the logs of all classes in the specified package and its descendants to the specified display level.

Once the display level is set, the logs of the set level and more important levels will be displayed. For example, when it is set to debug, it will display debug, info, warn, and error. If it is set to warn, only warn and error will be displayed. level of logging.

4. Advantages of logs

The main advantages of the log are:

- Can be configured to control the display level of the log

- The text of the log will be cached, and the output efficiency will be higher when executed multiple times

- In advanced use, you can also output logs to other specified locations, such as files, databases, etc.

5. About SLF4j and other logging frameworks

Logging frameworks such as log4j and logback are relatively mainstream logging frameworks, and `SLF4j` is a set of standards, and it does not have a specific log function. Therefore, SLF4j and log4j / logback are more like an interface and implementation class. Relationships (nature is not).

Guess you like

Origin blog.csdn.net/qq_43780761/article/details/126455108