Sunday, September 25, 2022

How to set the logging level in Spring Boot application.properties - Example Tutorial

Hello guys, if you are wondering how to set the logging level on spring boot then you have come to the right place. In the past, I have shared the best Spring Boot courses and free courses to learn Spring MVC and in this article, I will share how to set logging levels like DEBUG and INFO in Spring Boot.  How do we configure the logging level of our Spring boot application is one of the questions that arise when developing a large application. Because we need to trace errors, warnings, informational data when running our application and to this, Spring has introduced Spring boot logging configurations. 

So in this tutorial, we are going to discuss how we can implement spring boot logging configuration via application.properties or application.yml, both are valid Spring boot configuration files. 


How to configure the logging level on the Spring Boot application?

By configuration the application.properties file according to below, we can locate it under the resource folder of our application. So given below is the application.properties files which we used in this tutorial.

logging.level.org.springframework=DEBUG
logging.level.com.howtodoinjava=DEBUG

#this is used to store the loggers in the tempdir folder
logging.file=${java.io.tmpdir}/application.log

# The way how you create the logging files
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%

# The way how you write the logging patterns within the file
logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} - %msg%

So in below, we are going to discuss about different kind of loggings which is using in Spring application. So first have a look at default loggings in Spring boot application.


Default logging in Spring boot.

So in the default method of logging, there is no need to add any configuration on application.properties file. So below is an example code of it.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

@RestController
public class AppController {

@RequestMapping("/example")
public String example(Map<String, Object> model) {

LOGGER.debug("This is a debug message");
LOGGER.info("This is an info message");
LOGGER.warn("This is a warn message");
LOGGER.error("This is an error message");

model.put("example", "Example !");
return "index";
}
}
Start your application and call the get method /example, then you will get the following type of response in your application console.
2021-10-07 14:54:05 - GET "/ping", parameters={}
2021-10-07 14:54:05 - Mapped to com.programming.itjobspro.controller.AppController#healthCheck()
2021-10-07 14:54:05 - Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2021-10-07 14:54:05 - This is an info message
2021-10-07 14:54:05 - This is a warn message
2021-10-07 14:54:05 - This is an error message 


Note: The default logging level is INFO as the debug messages are not present. We can also configure the logging pattern by writing it in the application.properties file by configuring in it. 
# The way how you create the logging files
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%

So in the above pattern, the output logging message will be printed as below types.
  • Date and Time - Millisecond precision.
  • Thread name - Enclosed in squre brackets.
  • Logger name - Class name
  • message - Log message


Log Levels

When you use logs in your applications, there are certain levels and there are various kind
loggers in Spring application. Spring boot loggers, application loggers, Hibernate loggers,
Thymeleaf loggers and many more.

Logging levels available - TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF

And also you can configure the root level using logging.level.root. In your
application.properties file, add the following lines of code.


#logging.level.root=WARN
logging.level.org.springframework.web=ERROR
logging.level.com.howtodoinjava=DEBUG

After upgrading the log of the application to DEBUG mode, then the output logs will be
follows.
2021-10-07 15:26:34 DEBUG 4092 --- [nio-8080-exec-1]
 com.programming.itjobspro.controller: debug log statement printed
2021-10-07 15:26:34 INFO 4092 --- [nio-8080-exec-1] com.programming.itjobspro.controller: info log statement printed
2021-10-07 15:26:34 WARN 4092 --- [nio-8080-exec-1] com.programming.itjobspro.controller: warn log statement printed
2021-10-07 15:26:34 ERROR 4092 --- [nio-8080-exec-1] com.programming.itjobspro.controller: error log statement printed

Logging Patterns

In order to change the logging pattern of the applicaiton, you need to edit the 
application.properties file and add the following lines of code in there.

# 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

After running the application, we can see the following output.

2021-10-07 15:26:34 - This is a debug message
2021-10-07 15:26:34 - This is an info message
2021-10-07 15:26:34 - This is a warn message
2021-10-07 15:26:34 - This is an error message

Send the logging output to logging file by using the logging.path.

2021-10-07 15:26:34 - This is a debug message
2021-10-07 15:26:34 - This is an info message
2021-10-07 15:26:34 - This is a warn message
2021-10-07 15:26:34 - This is an error message]

Logging output file.

To print the logs in the file, use the logging.file or logging.path property.

logging.file=C:/Users/Programmers/application-debug.log
So see the output in the following diagram.
2021-10-07 15:26:34 DEBUG 4092 --- [http-nio-8080-exec-1]
 com.programming.itjobspro.controller       : This is a debug message
2021-10-07 15:26:34 INFO 4092 --- [http-nio-8080-exec-1] com.programming.itjobspro.controller : This is an info message
2021-10-07 15:26:34 WARN 4092 --- [http-nio-8080-exec-1] com.programming.itjobspro.controller : This is a warn message
2021-10-07 15:26:34 ERROR 4092 --- [http-nio-8080-exec-1] com.programming.itjobspro.controller : This is an error message

Color-coded log output

To enhance the readability of the logging output of your application, we can use the color-coded outputs. Only you have to do is,
enable the ANSI which is shown in the below. Note This may be vary acccording to your IDE and here used the intellij IDE.

How to set the logging level with application.properties in Spring Boot - Example Tutorial


color coding is configured using %clr conversion word.
  • Fatal Error - Red
  • Warn - Yellow
  • Info, Debug and trace - Green


Specific Logging Configuration.

Within one application, we can have multiple configurations. So you can achieve this by creating multiple application-{profile}.properties files in the same location of applicaiton.properties file.

Lets think that your application has multiple environments with having dev and prod. Then you can create two profile specific properties.

Application-prod.properties.
logging.level.com.howtodoinjava=ERROR
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

application-dev.properties.

logging.level.com.howtodoinjava=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n

In order to pass the profile information to application, spring-profile.active is passed to
runtime.


$ java -jar -Dspring.profiles.active=prod spring-boot-demo.jar

That's all about how to set the logging level with application.properties in Spring Boot
application. This is very important topic as you need to maintain high readability in your
code and also if there is an error happen, you need to know what exactly going with
your application.

Hope you understand the tutorial well and see you in the next tutorial.

Until then bye!

Other Java and Spring Tutorial you may like
  • How to use PropertySource in Spring Framework (Example)
  • How Spring MVC works internally? (answer)
  • How to upload a file in the Spring MVC application? (example)
  • Spring Data JPA Repository (JpaReposistory example)
  • 5 Courses to learn Spring Cloud for Microservices (courses)
  • Difference between @Autowired and @Inject in Spring? (answer)
  • Difference between @Component@Service, and @Controller in Spring (answer)
  • 10 Advanced Spring Boot Courses for Java developers (courses)
  • Difference between @RequestParam and @PathVariable in Spring (answer)
  • Top 7  Courses to learn Microservices in Java (courses)
  • @SpringBootApplication vs @EnableAutoConfiguration? (answer)
  • 10 Spring MVC annotations Java developer should learn (annotations)
  • Spring Data JPA @Query Example (query example)
  • 5 Courses to Learn Spring Security for Java programmers (courses)
  • Top 5 Frameworks Java Developer Should Know (frameworks)
  • Top 5 Spring Boot Annotations Java Developers should know (read)
  • 20+ Spring MVC Interview Questions for Programmers (answer)
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • Top 5 Courses to Learn and Master Spring Cloud (courses)
  • Top 5 Spring Cloud annotations Java programmer should learn (cloud)

Thanks for reading this article so far. If you find this Spring Boot Logging Example then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are a Java beginner and want to learn the Core Spring Framework from scratch, and looking for some best online resources then you can also check out these best Spring Core and MVC courses for beginners. This list contains free Udemy and Pluralsight courses to learn Spring MVC from scratch.     

2 comments:

Feel free to comment, ask questions if you have any doubt.