Use Logging in Spring Boot

  1. The objective of the lesson
  2. Create a Spring Boot project
  3. Logging Level
  4. Logging File
  5. Logging Pattern

1. The objective of the lesson

"Logging" is simply understood to be "recording" the problems during operation of applications. The problems herein are errors, warnings and other information,.... This information can be displayed on Console screen or recorded in files.
When you run the Spring Boot application directly onEclipse, you can see information on Console window. This information show you the state of the application, errors occurring during running application. That is Logging!.
There are many different Logging libraries such as Logback, Log4j2, JUL,.. Spring Boot can work with these libraries. By default, Spring Boot has automatically configured and used the Logback library for its logging, and you don't need any more configurations, except you want to customize some arguments. The customizations herein can be specifying file name to write information, pattern, ...
In this lesson,we are going to discuss the operation principle of Logback in Spring Boot application, and learn about customizing way for Logback.
The properties can be customized by you:

2. Create a Spring Boot project

On Eclipse, create a Spring Boot project.
To use Logging, you need to use one of the following "Starters" :
  • spring-boot-starter-logging
  • spring-boot-starter-web
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"


    <description>Spring Boot +Default Logging</description>

        <relativePath/> <!-- lookup parent from repository -->





package org.o7planning.sblogging.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

public class MainController {

    private static final Logger LOGGER = LoggerFactory.getLogger(MainController.class);

    @RequestMapping(path = "/")
    public String home() {

        LOGGER.trace("This is TRACE");
        LOGGER.debug("This is DEBUG");
        LOGGER.info("This is INFO");
        LOGGER.warn("This is WARN");
        LOGGER.error("This is ERROR");

        return "Hi, show loggings in the console or file!";
Run your application directly on Spring Boot, and then visit the following link:
On the Console window of Eclipse, you can see the Logs information as follows:
2018-01-14 13:21:57.593  INFO 7980 --- [nio-8080-exec-6] o.o.sblogging.controller.MainController  : This is INFO
2018-01-14 13:21:57.593  WARN 7980 --- [nio-8080-exec-6] o.o.sblogging.controller.MainController  : This is WARN
2018-01-14 13:21:57.593 ERROR 7980 --- [nio-8080-exec-6] o.o.sblogging.controller.MainController  : This is ERROR

3. Logging Level

Based on the severity of problem, Logback divides the information to be recorded into 5 Levels, the least severity is TRACE, and the most severity is ERROR. Note: Some Logging libraries divide the information to be recorded into 7 different levels.
  • INFO
  • WARN
  • OFF
By default, Spring Boot record only information with the severity of INFO or higher
# Default:

Change the Logging Level in application.properties:
* application.properties *

# ..
Rerun your application, and see the information written on the Console window:
2018-01-14 17:45:50.341  WARN 8500 --- [nio-8080-exec-1] o.o.sblogging.controller.MainController  : This is WARN
2018-01-14 17:45:50.342 ERROR 8500 --- [nio-8080-exec-1] o.o.sblogging.controller.MainController  : This is ERROR

4. Logging File

Default logging information is written on Console screen, however, you can configure so that they are written in files.
* application.properties *
Rerun your application directly on Eclipse, then access the following link:
Then refresh project again, you can see the logging file created.

5. Logging Pattern

Log records are written in a pattern. Below is a default pattern:
And you can change "Logging pattern" by customizing the following properties:
  • logging.pattern.console
  • logging.pattern.file
# Pattern:

logging.pattern.console= %d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n

# Output:

2018-Jan-17 01:58:49.958 WARN  [http-nio-8080-exec-1] o.o.s.c.MainController - This is WARN
2018-Jan-17 01:58:49.960 ERROR [http-nio-8080-exec-1] o.o.s.c.MainController - This is ERROR
# Pattern:

logging.pattern.console= %d{dd/MM/yyyy HH:mm:ss.SSS} %-5level [%thread] %logger{115} - %msg%n

# Output:

17/01/2018 02:15:15.052 WARN  [http-nio-8080-exec-1] org.o7planning.sblogging.controller.MainController - This is WARN
17/01/2018 02:15:15.054 ERROR [http-nio-8080-exec-1] org.o7planning.sblogging.controller.MainController - This is ERROR
# Pattern:

logging.pattern.console=%d{yy-MMMM-dd HH:mm:ss:SSS} %5p %t %c{2}:%L - %m%n

# Output:

18-January-17 02:21:20:317  WARN http-nio-8080-exec-1 o.o.s.c.MainController:22 - This is WARN
18-January-17 02:21:20:320 ERROR http-nio-8080-exec-1 o.o.s.c.MainController:23 - This is ERROR

Spring Boot Tutorials

Show More