Use of Spring Boot logs

1. What is Spring Boot log

Spring Boot log refers to the mechanism and tool for recording log information and tracking application execution process in Spring Boot application. Spring Boot integrates common log frameworks, such as Log4j, logback, Java Util Logging, and provides an easy-to-use The configuration method enables developers to conveniently monitor and interview the running status of the application. Let’s take a look at

the simplest log, which is also the most common log that everyone has seen. When the project starts , the log has actually been printed, but it is not persisted
image.png

2. The role of the log

From the startup of the Spring Boot project above, we can see that when it started, it printed a log. From the log, we can see some information, BUG level, port number, etc. Is there only these logs? Of course not, the simplicity here The startup log only records some basic things, but the log can do more things

  • Record the login log, which is beneficial to analyze whether the user logs in normally, and prevent malicious attacks from cracking the user

If we record the login log, then we can analyze the user's risky login from different places, multiple brute force attacks, etc.

  • Record the operation log of the system to facilitate data recovery and location operations

For example, a school’s educational administration system records every system operation log, such as student transfer information, account information, student status management, etc. When the teacher makes a mistake, such as transferring Zhang San from class 1 to class 2, this can According to the log, the rollback operation is performed to restore the data, and at the same time, it is possible to locate which teacher's wrong operation has a problem, and to pursue responsibility, etc.

  • Record the execution time of the system, which is convenient for optimizing the program as data support

Record the start time of a program, and record the time at the end, so that the execution time of the program can be obtained. If the execution time is too long, then you need to consider how to optimize it. Of course, in addition to some functions listed above, the log has other
functions Important uses, you can explore by yourself

3. How to customize the log

When I set up a routing method in TestController, after I went to visit, I printed the log on the console: XXX, is this the log information? I believe that I will definitely think that

image.png
it is right to output it on the console. If this method does not The output is wrong. From this level alone, it seems that the basic positioning can indeed be based on the method, but it is not so when viewed from the following angles.

  1. no location information

I don’t know where this method is executed. When we call this method more often, it is difficult to locate where the method called is wrong by simply souting through the console.

  1. no time information

This is a black box mode, there is no time, and I don’t know whether he made a mistake before execution or executed and made a mistake.
Then, some people will definitely think that I can output different error messages under each method, and add Time sout can also solve this problem, except that it is a little more troublesome. However, in addition to these two problems, sout also has two other problems

  1. no level setting

In production, when an error occurs, it is more to see the error message more intuitively than to debug the error message. There is no way to set the log level to filter the information.

  1. cannot be persisted

This is the most important problem. When the project is in production, the log information is very important information. It can be used as the basis for problem analysis and needs to be persisted. However, there is no way to solve it with sout. After restarting, the log information cannot be observed up

3. How to use the log

For Spring Boot, it has a built-in SLF4J framework, which can directly call SLF4J in the program to print the log, and the log is a facade mode. What is the facade mode? For example, it is like looking for a house. SLF4J is

here It acts as a housing intermediary, and its different houses are its realization. For example, a large flat (Log4j), a small bungalow (logback), and a sea view villa (Java Util Logging). One day you need to rent a house to install your You can go to a real estate agent, and the real estate agent will introduce you to the above types of houses, and you can choose by yourself. For example, if you choose a large flat, you can change to a sea view villa through an intermediary if you are tired of living in it. You can save the time of looking for a house. If you want to replace it, you only need to replace it through an intermediary. Bring it

into our log, the intermediary is the facade of the log, and the house controlled by the intermediary is the realization of the log . What are the benefits of this? We no longer need to pay attention to the appearance, and entrust others to do it for us. We only need to see the house we like (use any log method to output it) and get tired of living one day (there is a problem with this log method) and we can quickly pass The intermediary is changing a house (changing a log method), avoiding that there is no place to go (this method is used to control all your logs, without an intermediary, it will take a long time to modify if you want to change the house) understand the

log After getting the facade mode, let's see how to customize the implementation and also give the log

  1. get log object
    • attention level

From the system log started above, we can see that they are all one-class, so the log object obtained must be a class-level log object, not a method-level object that is only implemented in a specific method
image.png

  • area of ​​attention

You can see from the started system log that the log is printed with location information, and it is clear which package and class you are in, so the log object can only be private, that is, it can only be stored in my class itself Use, it is not easy to use under other classes, otherwise the log will be messed up
image.png

  • Pay attention to the choice of frame

Since we use SLF4J as the facade mode, we need to call the specific implementation method through the facade to create the log object, so when creating the log object, we should choose the Logger under SLF4J to sum up, and finally create a log object through the Logger of SLF4J, and
image.png

pass Provided log project to call getLogger (because it is at the class level, so the getLogger method of the class is selected here ) to successfully create a log object

image.png
image.png

  1. use the log object

image.png
After accessing the route, you can see the following information on the console:

PS: The log information printed here is different from your default one. I set the default level in the configuration file to debug, and the default level of the log system to info. This problem will be discussed below , here we only focus on the content of the log object output log, that is,

image.png
time information, log level information, log location information, and log content information . It can be said that it is a very complete log

4. Log level

  1. trace : trace level, the lowest level
  2. debug : used when debugging is required, debug level
  3. info : normal print information (log default level)
  4. warn : warning, does not affect the use, but needs attention
  5. error : error information, higher level error log information
  6. fatal : Fatal, the event that the program execution exits due to a code exception

The above is the log level, but we can only operate the first five log levels, and the last fatal log level cannot be manipulated. When the program exits with an error, the system will automatically print the fatal level log information for us. From the
above Below, the level is getting higher and higher, and the lower the message is, the less the message is received. It follows a principle that the low-level ones cannot view the high-level ones, and can

image.png
image.png
only view the error messages of the info, warn and error levels. Trace and debug cannot be viewed, why?
When we log out the info and look at it

image.png

It can be seen that only info is missing. After commenting out warn and error to see that
image.png
there is no log information at this time, we can draw a conclusion. The default level of the log is info. What should we do when we want to view the log information of trace and debug? After all, debugging logs are very important for writing programs, and can locate the problem . Let’s introduce the level settings of logs in projects in Spring Boot

Five. Log level setting

Configure the log level in the properties configuration file below
image.png

Or set the log level in yml
image.png


image.png
After setting, we can see the error log information of the debug level because we have set the default level to debug in the configuration file before executing the observation. The trace log is lower than the debug level . The information is still not viewable, if you want to see it, you can set the log level in the configuration information to trace

In addition, in addition to the root (root logger) setting as the log level in the configuration file , there are more detailed log level settings to set the log level for specific packages . For example, in the following configuration file, set the class under the com.example package The log level is info, and the log level under the com.example.demo package is debug
image.png

6. Persistence of logs

Of course, the log is so important not only because it can locate the problem, but more importantly, it can be used as data analysis. If you want to analyze the data, you must not print the log through the console sout. The next startup will be gone, so you must set The log is persisted, and the following is to realize the persistence operation of the log and

set it in the yml configuration file. Set the file path, the name of the file can not be written (write the generated name is the name you set), and a file named spring will be automatically generated .log file
image.png
and then run the log method just now. At this time, such a spring.log file is generated under the save path I just saved.
image.png
When I repeatedly access this method, it will print the log repeatedly. Let’s see what will happen It will not overwrite
image.png

In fact, it will not overwrite, if it is really overwritten, it will be a big problem!

PS 1: In the save path setting of the file, if no path is set, it will generate a you under the project set the name of the file

image.png

PS 2: You can see the instructions on the official website. When the log file exceeds 10MB in size, it will save your subsequent log file in a new file (and it will name and split the log for you according to its own naming rules) , but the size of this log file can also be set

Seven. lombok implementation log

It can be seen that when implementing log printing in different classes, it is necessary to create log objects, and it will be a bit troublesome to modify the class object of the getLogger method, so lombok provides us with an easier way to realize log printing

  1. Add lombok dependency
    This step will be added manually

  2. Add @slf4j annotation
    image.png
    lombok In addition to @slf4j annotation, there are many other annotations, you can go to the official website to check.

Guess you like

Origin blog.csdn.net/qq_68288689/article/details/130676491