Spring Boot Logging

Spring Boot Logging

In this article, I am going to discuss Spring Boot Logging. Please read our previous article where we discussed Spring Boot Application Properties.

Spring Boot Logging

As we have learned by now, Spring Boot uses Apache commons-logging for all internal logging. However, other logging techniques, such as Java Util Logging, Log4j2, and Logback can also be used.

Default Logging Format

By default, the logging format is as follows:

Default Logging Format

As can be seen in the above screenshot, the following fields are present in the default log output:

  • Date and time of the execution
  • Type of log
  • Process ID
  • Thread name (which is in the square brackets)
  • Logger name which shows the name of the class
  • The log text

By default, only INFO, WARN, and ERROR logs shall be displayed in the console. This behavior can be modified to display other information such as DEBUG, FATAL, etc. In this article, we will learn about default logging.

Method to display Debug log:

Debug logging is a more accurate style of logging wherein almost all information is printed. This is helpful in finding the exact point where an error has occurred. To do this, follow these steps:

Step 1: Modify the application.properties file. As done in the previous chapter, navigate to the application.properties file in the src/main/resources folder. This should be empty by default. Add the following line to the file:

Spring Boot Logging

Save the application.properties file and run the application. A much more comprehensive output should be visible. This helps identify the point where a bug was encountered by the program. Here is a sample screenshot:

Spring Boot Logging

As seen here, several new logs have been printed with the DEBUG tag. This shows the extra output produced by the logger.

Logging to a file

More times than not, we would like to save logs to files for easier analysis. This can be done using two methods. Both of these methods involve modifying the application.properties file.

Method 1: Add the following line to the application.properties file. The log file shall be saved in the specified folder. The log file shall be stored in a file called spring.log (default file name).

Logging to a file

The content of the spring.log file is as follows:

Logging to a file

Method 2: Add the following lines to the application.properties file.

Logging to a file

When this line is present, the application shall write to the specified name, rather than the default name. Furthermore, the log file shall be present in the application folder. A sample of the springlogsample.log file is as follows:

Logging to a file

Typically, log files have an extension of .log. Extensions such as .txt can also be used. However, most developers use .log because it helps with the markup of the log. Such markup can be seen in the above sample. Some text is highlighted to show important details, such as file locations, class names, etc.

In the next article, I am going to discuss Spring Boot Logging using Logback. Here, in this article, I try to explain Spring Boot Logging. I hope you enjoy this Spring Boot Logging article.

Leave a Reply

Your email address will not be published. Required fields are marked *