Elements of Logging Framework

  • Logger: It captures the messages.
  • Formatter: It formats the messages which are captured by loggers.
  • Handler: It prints messages on the console, stores them in a file or sends an email, etc.

Java provides several logging frameworks, some of which are:

  1. Logback Configuration logging
  2. Log4j2 Configuration logging
  3. Logging with Lombok
  4. @Slf4j and @CommonsLog

Initial Setup

To create a simple Spring Boot project using Spring Initializer, please refer to this article. Let’s define a simple Rest Controller that outputs various levels of log messages.

Java




// Rest Controller to print various log level messages
package com.log.controller;
  
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
  
@RestController
public class LogController {
  
    // creating a logger
    Logger logger
        = LoggerFactory.getLogger(LogController.class);
  
    @RequestMapping("/log") public String log()
    {
        // Logging various log level messages
        logger.trace("Log level: TRACE");
        logger.info("Log level: INFO");
        logger.debug("Log level: DEBUG");
        logger.error("Log level: ERROR");
        logger.warn("Log level: WARN");
  
        return "Hey! You can check the output in the logs";
    }
}


Now we simply need to run the application and hit http://localhost:8080/log to see the log messages.

Spring Boot – Logging

Logging in Spring Boot plays a vital role in Spring Boot applications for recording information, actions, and events within the app. It is also used for monitoring the performance of an application, understanding the behavior of the application, and recognizing the issues within the application. Spring Boot offers flexible logging capabilities by providing various logging frameworks and also provides ways to manage and configure the logs.

Why to use Spring Boot – Logging?

A good logging infrastructure is necessary for any software project as it not only helps in understanding what’s going on with the application but also to trace any unusual incident or error present in the project. This article covers several ways in which logging can be enabled in a spring boot project through easy and simple configurations. Let’s first do the initial setup to explore each option in more depth.

Similar Reads

Elements of Logging Framework

Logger: It captures the messages. Formatter: It formats the messages which are captured by loggers. Handler: It prints messages on the console, stores them in a file or sends an email, etc....

Log Levels

...

0. Default Logging With No Configurations

...

Logging to a file

Spring boot allows us to see the logs in the console even if we do not provide any specific configuration for it. This is because spring boot uses Logback for its default logging. Spring boot’s internal logging provider is Apache Commons which provides support for Java Util Logging ,Log4j2, and Logback. Hence Apache Commons had to be manually imported until spring boot 1.x. However, since spring boot 2.x, it is downloaded transitively. To be more specific, Spring boot starters such as spring-boot-starter-web imports spring-boot-starter-logging which automatically pulls in Logback. On running the application and visiting the http://localhost:8080/log page, we can see the following output in the console:...

1. Logging with Logback configuration

Spring boot only logs to the console by default. In order to log into a file, the following properties need to be added to the application.properties file:...

2. Logging with Log4j2 configuration

Spring boot enables logback to be configured in a specific manner to meet our project’s requirements. In order to do so, we need to define a configuration file where we can specify logging pattern, color, different properties for file logging & console logging, and an efficient rolling policy to prevent the creation of huge log files. Whenever Spring boot finds a file with any of the following names, It automatically overrides the default configuration....

3. Logging with Lombok

...

4. @Slf4j and @CommonsLog

For using log4j2, we need to exclude Logback from our starter dependency and add the dependency for log4j2 as follows:...