Signup/Sign In
LAST UPDATED: APRIL 25, 2020

Spring Boot Logging

In this tutorial, we will be discussing about the logging options available in Spring boot.

Spring Boot File Logging:

It is essential to keep the track of ongoing activity in any application when its running and performing some action and the most efficient way of doing so is via logging. Logs help us understand in better what is happening in the application. Logs can also help us to track the performance of our application and helps us to understand where is the scope of optimization. They can also be sent to some other dedicated server for further processsing and analysis. It can be enabled by adding simple properties in application.properties.

logging.path=D:

The above property enables a default file logging in D drive under the name of spring which is created automatically.

Spring Boot Console Logging:

This enables logging on the console and notice the code below. We need to follow the below procedure using slf4j which is configured by spring boot.

@SpringBootApplication
public class Application {

    public static void main(String a[]){
        // initialise the logger
        Logger logger = LoggerFactory.getLogger(Application.class);
        // use the logger
        logger.info("Application about to start");
        SpringApplication.run(Application.class, a);
    } 
}

using console logger for spring boot application

The above figure displays the console logging before the application launch. It displays the time, thread name and the message. The spring boot auto configures the org.slf4j.Logger so, it can be used as shown in the above code.

We can also use properties in application.properties as follows. For example, if you want to see the logs for internal springframework functioning, you can do so by adding the following property:

logging.level.org.springframework=debug

The above property allows to run the spring framework in DEBUG mode which means we will be able to see the autowiring of instances done by spring and the auto-configuration report generated for us.

The above figure displays the autowiring description of the instances during application launch.

We can also change the pattern of logs by specifying few properties in application.properties. In order to change the logging pattern of console, use logging.pattern.console property and to change the logging pattern of file, use logging.pattern.file property.

# Logging pattern for the console
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
 
# Logging pattern for file
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n

The logging level can be one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The most commonly used logging levels are:

INFO: used for basic information logging

DEBUG: used for logging additional information helpful in application debugging

ERROR: used for logging error conditions.

Conclusion: In this tutorial, we have seen the logging of the spring boot application which is of the crucial importance especially in the production environment.



About the author:
I'm a writer at studytonight.com.