Logging can be used to debug programs, print run logs and error messages for later maintenance of the system. Before the rise of SpringBoot, log4j was the best way to log. For now, many projects use Log4J for logging. So how does our SpringBoot log?

By default, SpringBoot uses LogBack to record log information. By default, SpringBoot uses the base. XML configuration to output log information to the console and file.

Objective in this chapter

Learn how to use LogBack to record logs to the console and files in SpringBoot projects, and output different forms of log information according to different levels.

Build the project

Since SpringBoot is integrated with LogBack, we don’t need to add any dependencies, we just need to create a new empty project. As shown in Figure 1 below:




Figure 1

We created the project with the WAR type of our choice, so we automatically added WEB and Tomcat. This is irrelevant because SpringBoot has a default configuration so we don’t need to add any operations. Now the log can be printed on the console. To verify this, we will create an IndexController and then add an access method /index, Add the info level print of the log to this method (the default configuration is for info and above to output) as shown in Figure 2 below:




Figure 2

Let’s start the project by accessing 127.0.0.1:8080/index to see the console output, as shown in Figure 3 below:




Figure 3

The default logback configuration is base.xml, so how can we change the default configuration?

Example Modify LogBack configuration

(2) If the file does not exist, try to find logback. XML. (3) If neither file exists, try to find logback.xml. LogBack automatically minimizes its configuration with BasicConfiguration, which allows it to output logs to the console without adding any configuration.

Next we create a file called logback.xml in the Resources directory and add the log configuration output to the file and store it in different files by day. The specific configuration is shown in Figure 4 below:




Figure 4.

In the logback.xml configuration file, we added console output, file daily output, maximum log file cap, minimum log level, and so on. Let’s run the project to see if our configuration works.

Test LogBack

Modify the index method in our IndexController and add output at multiple log levels, as shown in Figure 5 below, to make it easier to check if our configuration takes effect:




Figure 5

Our logback. XML configuration file sets the root output level to INFO, so if logback. XML takes effect, there will be no Debug level log output in our console and file. Let’s restart the project. Access the previous address /index to view the console and /logs/ Runtime.xxxx. log configuration file contents, as shown in Figures 6 and 7 below:




Figure 6.




Figure 7.

As you can see, Debug level logs are not output, only INFO and ERROR level logs are printed and output to the file as expected. We configured./logs as the output root of the logs, so LogBack automatically creates a folder named logs in our project root, and a file named after us is automatically created when the project is first logged.

Masking Logging

If we need to mask one or more packages in a project that do not output logs and do not record logs to files, then we need to modify application.yml to add the corresponding configuration, as shown in Figure 8 below:




Figure 8.

Can see we blocked com. Yuqiyu. Chapter12) controller log print under the package, For testing purposes we create a controller named TestController under the com.yuqiyu.chapter12 package and copy the contents of IndexController as shown in Figure 9 below:




Figure 9.

Let’s restart the project again, access 127.0.0.1:8080/index, and then check the console and log file to see if there are logs, as shown in Figure 10 below:




Figure 10.

The console does not print any logs, nor does the log file! Let’s revisit 127.0.0.1:8080/test to see the console and log file contents, as shown in Figures 11 and 12 below:




Figure 11.




Figure 12

You can see that the log was successfully printed, proving that our configuration has taken effect. Of course, you can change the log output level of a package by changing ‘OFF’ to the corresponding level.

Pay attention to

In the application.yml configuration file, off must be quoted otherwise it will not take effect.

conclusion

This chapter describes how SpringBoot uses the built-in log component to output logs, save logs to files, and control log output.

This chapter has been uploaded to the code cloud:

SpringBoot source code address: gitee.com/hengboy/spr…

SpringCloud source code address: gitee.com/hengboy/spr…

For SpringBoot related articles, visit: Directory: SpringBoot learning directory

For a series of QueryDSL articles, visit the QueryDSL Generic Query Framework learning directory

For articles in the SpringDataJPA series, visit: Directory: SpringDataJPA Learning directory

SpringBoot related articles please visit: directory: SpringBoot learning directory, thank you for reading!

Welcome to join QQ technical exchange group, common progress.




QQ technical exchange group