Understanding logging and its levels in Spring Boot
Introduction to Log4j2 , log levels and formatting logs in Spring Boot
As a software developer, do you spend most of your time debugging your code? Going through the entire codebase and still not able to figure out where your code went awry? Here’s where logging comes to the rescue!
Logging is the process of tracking all the events that happen after a piece of code is run. It is a very important aspect of software development as it helps to track where exactly the code crashes and thus eases debugging .
Let’s understand how logging is done in spring boot!
To make logging easier for programmers, Java provides a variety of logging frameworks like : log4J, java.util.logging (JUL), tiny log, log back, etc.
A logging framework can be used to perform all the tasks like setting log file destinations, customizing log messages, etc.
LOGGER
To generate loggers, we use the LoggerFactory class of the org.slf4j and the Logger interface is the entry point to slf4j API. The getLogger is a method of the Logger Factory class that takes a string value as a name and provides the logger object of the specified name.
/*Logger logger = LoggerFactory.getLogger(Controller.class); */
Log Levels
The messages logged can be of various security levels. Spring Boot supports five log levels which are
ERROR — runtime errors
DEBUG — Information about the flow of the system
TRACE — more detailed information about the flow of the system
WARNING — warning for the errors caused due to the usage of deprecated APIs.
INFO — events occurring at the run time
By default, the log level is set to INFO in Spring Boot. We can set log levels according to our requirements in the applications.properties file
The format to set the log-level configuration is logging.level.[classpath] = [level]
.
We can also set different log levels for different classes.
logging.level.root=INFO
logging.level.com.logging =DEBUG
In the end, when running the spring boot application, you will see the different log levels in the console.