Updated April 17, 2023
Introduction to Spring boot logging level
In spring boot, it is very easy to log at a different level; also, spring boot provides us default logging. While using it, we do not require an external dependency because it is already included. Logging is used to keep track of all the activity of our application; by the use of this, we can identify the errors in our application at runtime if they occur. Also, we can check and track if the application working fine. These are very easy to use and handle because we do not require lots of configuration for this as the spring boot framework manages it. In the coming section of the tutorial, we will see the different logging levels in the spring boot application and how we can set them in our application for better error tracking purpose in detail.
Syntax
As we have seen we can set different logging level in spring boot let’s take a look at the syntax and required things which are needed to make this work in our application for better understanding see below;
logging.level.root= your logging level
As you can see in the above line of syntax, we are trying to configure the log level. For this, we have to make the above changes to our applications.properties file, and we can set the log level whichever we want. Let’s take a look at the practice syntax for better understanding see below;
Example:
logging.level.root=DEBUG
As you can have mentioned, this type of configuration enables the logging level at DEBUG level. In the coming section, we will see all the logging levels in spring boot with their respective working in detail and how to print the statement using those logs level in detail for beginners to understand it better.
How to set logging level in Spring boot?
In spring boot, we can set different logging levels, also with minimum configuration needed. This log level is important when we deploy our application in the development and production environment to keep track of any error or say all the activity. We have different types of log levels available in spring boot to make sure not any unnecessary logs can’t be printed on the production environment, which can lose any kind of information to anyone who is not authorized for that. In this section, we will first see the different type of logging level in detail, how they work and how we can use them in spring boot; see below, which are as follows;
1. INFO
This kind of information or logs should not contain the error or details of something. They are just used to inform the developers that everything is working fine. We can add logs as service has started, service has stopped, any db operations; we can trace all these kinds of logs that come under this INFO level general-purpose logs that tell the application’s activity.
Example:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
public class MyClass {
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
private void test(){
logger.info("I am info level log !!");
}
}
application.properties
logging.level.root=INFO
As you can see in the above code, we have implemented the info level logs; for this, we have to first initiate the LoggerFactory inside that we pass the name of our class, now we can use this. by calling the info() method on it.
2. DEBUG
In this mode, we want to monitor the information related to an application, like if the user got created, trying to print their id and all. This will come into the picture when we want to capture the production activity closely. Below see the syntax to use this;
Example:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
public class MyClass {
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
private void test(){
logger.debug("I am info debug log !!");
}
}
application.properties
logging.level.root=DEBUG
As you can see in the above code, we have implemented the debug level logs; for this, we have to first initiate the LoggerFactory inside that we pass the name of our class, now we can use this debug() method on it.
3. ERROR
This type of logging level is used when we want to capture the error in our application, causing the application to be down and breaking the functionality to work properly. We can track database connections, any file missing, or something serious which is causing the application to stop. Below see the syntax to use this in the spring boot application see below;
Example:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
public class MyClass {
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
private void test(){
logger.error("I am error level log !!");
}
}
application.properties
logging.level.root=ERROR
As you can see in the above code, we have implemented the error level logs; for this, we have to first initiate the LoggerFactory inside that we pass the name of our class; now, we can use this. by calling the error() method on it.
4. WARN
In this type of logs, we track the warning, which can lead the application to stop; suppose we are trying to establish a connection, but somehow it is falling and not able to connect, so in this kind of siltation, we can use warn level, which can predict any future cause to our application. Below see the syntax to use this in the spring boot application see below;
Example:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
public class MyClass {
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);
private void test(){
logger.warn("I am warn level log !!");
}
}
application.properties
logging.level.root=WARN
As you can see in the above code, we have implemented the warn level logs; for this, we have to first initiate the LoggerFactory inside that we pass the name of our class, now we can use this. by calling the warn() method on it.
Conclusion
By using the logging level, we can easily divide our information logs not to be printed in different environments. We have to make normal changes to the application properties of the respective environment.
Recommended Articles
This is a guide to the Spring boot logging level. Here we discuss How to set the logging level in the Spring boot along with the examples. You may also have a look at the following articles to learn more –