Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Spring Boot in Java helps in using Java-based frameworks to build web applications and microservices. It is an open-source tool. It uses Java, one of the most popular programming languages for application development. It makes it simple to make Spring-based applications that can be run quickly.
In this article, we will study Logging with Spring Boot.
What is Logging with Spring Boot?
Logging means noting down the points of development so that anyone can understand what happened in what phase of the development cycle. If you use Spring Boot for your application, you can enable logging quickly to get your points noted easily.
Log Format
The following are the parts of the logs in Spring Boot:-
Date and Time of the log.
Log Levels: WARN, INFO, DEBUG, ERROR, etc.
Process ID
Thread Name in square brackets.
Logger Name shows the source class name.
Log Message
Now, let us see some implementations of Logging with Spring Boot.
Example with slf4j logger
First, we initialize a Spring-based application using the Spring Initializr in Maven Project, Java language, and Spring Web dependency.
Then, we create a class named HomeController.java inside our project’s folder in our Java folder. It will serve the purpose of RestController for our project. The code of our HomeController.java is as follows:
We can see that the file returns Hey there as the main output of the webpage. Furthermore, we see four logs each time the webpage loads: trace, error, debug, and info. We import the necessary files and use the LoggerFactory.getlogger() method to use the custom logging methods.
Also, the following program is to be written in the application.properties file to allow the logging methods to function. Without it, trace and debug may not work. Thus, we are specifying our log levels for our project.
logging.level.com.example=TRACE
OUTPUT
Upon running the application, the following is the output of the webpage:
Below is the output for the logs printed in the console:
We can see that all the logs have been duly noted. It serves our purpose of logging with Spring Boot.
Also, by default, logs are shown in the console. If we want To print them in a file, the following commands can be written in the application.properties file:
Apart from usually coding our logging methods in the RestController, we can create a log-back file to serve our purpose for Logging with Spring Boot. It can be named: logback.xml or logback-spring.xml.
Following is the program written in our logback.xml file in our resources folder inside our main folder in the src folder:
We specify a printing pattern for our logs with colors as yellow for date and time and white for our log messages. Also, we set the format of our date and time formats. And we give the name to our logs file as app_log_back.log. We also print in on the console.
OUTPUT Upon writing this file, we see the following output on the console:
We see that the app_log_back.log file is created in the Logs folder in our project with the following output:
Frequently Asked Questions
What is Spring Boot?
Spring Boot in Java helps in using Java-based frameworks to build web applications and microservices. It is an open-source tool. It uses Java, one of the most popular programming languages for application development. It makes it simple to make Spring-based applications that can be run quickly.
What do you mean by Logging with Spring Boot?
Logging means noting down the points of development so that anyone can understand what happened in what phase of the development cycle. If you use Spring Boot for your application, you can enable logging quickly to get your points noted easily.
What are the different components of a log in Spring Boot?
The following are the parts of the logs in Spring Boot: Date and Time, Log Level(WARN, ERROR, etc.), Process ID, Thread Name, Logger Name, and the Log Message. The — is the separator between them.
Conclusion
Spring Boot is an essential tool that helps develop various applications using Java programming language. Also, having a clear and concise logging infrastructure for your project is vital to keep track of the events happening. In this article, we studied Logging with Spring Boot. We started with its definition and components, then two ways to achieve Logging with Spring Boot.
We recommend you read the following articles too:-