Updated April 4, 2023
Introduction to Spring Boot Logging
Spring boot logging is defined as a framework that enables developers to trace out errors that might occur in the running of the application. Logging in spring boot is basically an API that provides tracing out of information along with a recording of any critical failure that might occur in the application during its run. Spring boot uses a common logging framework to implement all internal logging and leaves the log implementation open. A thin adapter that allows configurable bridging to other logging systems is the USP of Commons logging. The default configurations provided by spring boot logging are Java Util logging, Log4J2, Logback. The loggers in default configurations are pre-configured for using console output.
Syntax of Spring Boot Logging
Spring boot logging, as mentioned, is a logging framework that provides the flexibility of log implementation. For example, if the developer uses “Starters”, logback is used for logging. Logback routing is included in the framework to ensure that all the dependent libraries that uses the Java Util logging, Log4J2, Commons logging etc., work in resonance. Here we will see about spring boot logging from the syntax perspective.
1. Instantiating the log level for the root.
logging.level.root = <log level>
<log level> can be replaced by any of the following log levels as per the requirement of the application.
The log levels which are available are: “TRACE”, “DEBUG”, “INFO”, “WARN”, “ERROR”, “FATAL”, “OFF”.
2. Specifying the log file path.
logging.path = /<path of the file>
3. Specifying the log file name.
logging.file = /<path of the file>/<log file name>.log
4. Importing the logger class.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
5. Defining the logging.xml pattern.
<pattern> define the pattern here </pattern>
6. Defining the logging.xml pattern.
logging.pattern.console = <logging pattern goes here>
How does Logging work in Spring Boot?
Working on it is important to understand the format in which a log is printed.
A default spring boot log file contains the following items:
- Date and Time: This provides the occurrence date and time of the log item.
- Log Level: This provides the information on what level of information the log is of. It will be one out of the 7 options we will see now, i.e. TRACE, DEBUG, INFO, WARN, ERROR, FATAL or OFF.
- Process ID: This provides the information of the process ID on which the spring boot application is running on.
- Separator: — This is a separator which signifies the next part of the log.
- Thread Name: This is enclosed within a square ( [ ] ) brackets and mostly contains the thread within which the logging thread or element is present.
- Logger Name: This is the penultimate element that contains the source class name.
- Log Message: Finally, this element contains the log message, which explains the methods followed in the application and helps us tracing back to the root cause if an error pops up in the application.
The 7 option falls in the following order of severity (low to high).
- TRACE
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
- OFF
The log level, as mentioned in the application, determines the detailing of the log messages. Configuring the log level with optimum aspect saves a lot of time. For example, when the level shows as FATAL, the application should actually have encountered some serious failure. Once the architecture of the application is developed, the logs should be carefully set up so that tracing back to the root point is easier. We don’t want to look at logs that mention some information about the process every time. It might be a possibility in cases this information is classified as FATAL due to improper architecture.
With each of the classified options available, when a code in the application executes, the corresponding options are validated against the level declared for the application; if the severity of the level declared for application is lower than the point of code execution, it is put in the log message. One can declare the level by using the code logging.level.com.<application name>=<logging level>. Now let us assume that during code execution, the code breaks and fails due to some corner cases we have not considered. The location of the code is put into the log message with the appropriate level. But if the code execution yields a less severe level than the declared level of application, the logging is skipped.
Once it is adjudged of what to put, the application looks for the pattern declared in either the xml or in the java code and corresponding elements are extracted and noted. A developer may also choose to output the log into the, and as per the path and file mentioned, the logs gets output to the file. Another element is that one can choose to have colour-coded log output in the console in case the terminal supports ANSI.
Example of Spring Boot Logging
Given below is the example of Spring Boot Logging:
Logging in Spring Boot with different cases.
Syntax:
Logresource.java:
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RestController
public class Logresource {
Logger logger = LoggerFactory.getLogger(Logresource.class);
@RequestMapping("/")
public String home() {
logger.debug("This is a DEBUG statement");
logger.warn("This is a WARN statement");
logger.error("This is a ERROR statement");
return "Hello Readers";
}
}
DemologgingApplication.java:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemologgingApplication {
public static void main(String[] args) {
SpringApplication.run(DemologgingApplication.class, args);
}
}
Output:
logging.level.com.example.demo=WARN is an application.properties file:
logging.level.com.example.demo=ERROR is an application.properties file:
logging.level.com.example.demo=TRACE is an application.properties file:
In this example, we saw that as if the logging severity is higher than the one initialized in the application.properties only that message is logged and rest others are ignored.
Conclusion
In conclusion, in this article, we have seen working on spring boot logging, and the flexibility spring boot provides to the developers. Following it is a small piece of code that tries to replicate the concept of the logging.
Recommended Articles
This is a guide to Spring Boot Logging. Here we discuss the introduction, how logging works in spring boot? And example, respectively. You may also have a look at the following articles to learn more –