Spring Boot Logger

Spring boot logging framework uses commons Logging for all internal logging and application specific logs. Log frameworks like Java Util Logging, Log4J2, and Logback are supported by the Spring Boot Logger. Spring boot log setup is preconfigured in the spring boot application. It is possible to configure other springboot loggers using straightforward configurations. Spring boot log settings can be configured using application properties files. The spring boot log is already set up to use console output in default installations. The logging in spring boot helps in understanding, tracking, monitoring and troubleshooting application issues.

The Spring Boot logging framework allows developers to track down errors that may occur during application execution. The spring boot log helps the developer in debugging bugs and comprehending how the application works. If a critical failure occurs in the spring boot application while it is running, the spring boot logger records the configured information. The level of logging, as well as where and how to log, can be set in the configuration files.

Spring Boot Logging Example

The following example shows how to log information in the spring boot application. The apis in the Logger class of the slf4j framework are used to log the message. By default, the logs will be printed in the spring boot console window. The application properties file can be used to redirect logs to a file. The code below will print the logs to the console window.

Package com.yawintutor;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

public class TestController {

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

    public String home() {
        LOGGER.trace("This is a trace message");
        LOGGER.debug("This is a debug message");
        LOGGER.info("This is an info message");
        LOGGER.warn("This is a warning message");
        LOGGER.error("This is an error message");

        return "Welcome to Yawin Tutor";

Initial Logging Setup

The spring boot application is set up with the Apache Commons logging framework by default. You do not need to configure the logging setup to enable it. Logback is the default logging method when Spring Boot starters are used. The Logback implementation by default logs the output to the console at the info level. Spring Boot’s required dependency for logging is Apache Commons Logging. Spring Boot automates the majority of an application’s configuration settings, allowing developers to concentrate on the code.

Spring Boot Logging Level Configuration

The available spring boot log levels are “TRACE”, “DEBUG”, “INFO”, “WARN”, “ERROR”, “FATAL”, and “OFF”. The application properties files can be used to configure the Springboot logging levels. Logging levels can be set at the root level or in a specific package structure. The following is the syntax for configuring the logging level in the application properties file.



Redirect Logs to File

The Spring boot logger allows you to redirect logs to a file. Using logging properties, the file name should be configured in the application properties files. The configuration below will write the logs to a specified file in the spring boot application. This command will create a log in file but will not stop writing in the console window. The logs will be written to both a file and a console window.


Disable Console Log

If you redirect the log to a file, the spring boot application will write the log in both the console window and the configured file. The following commands should be used to stop sending the log to the console window. The following command will stop printing the banner and display the spring boot default logs in the console window.


Spring Boot Logging Pattern

Spring boot allows you to change the message logging pattern. A prefix percentage can be used to define the logging pattern. The following is an example of a logging pattern that can be configured in the application properties file.

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

Configure LogBack in Spring Boot

Spring boot log can be configured using Logback. The logback xml file will be used to configure the logger using the xml code below. The output of the console output appender will be logged in the console window. The output will be logged in the specified file location by the logfile appender. The log level is set at the root level. The logs will be logging in spring boot using the logback xml configuration mentioned below.


<?xml version = "1.0" encoding = "UTF-8"?>
   <appender name = "consoleoutput" class = "ch.qos.logback.core.ConsoleAppender"></appender>
   <appender name = "logfile" class = "ch.qos.logback.core.FileAppender">
   <root level = "INFO">
      <appender-ref ref = "logfile"/>
      <appender-ref ref = "consoleoutput"/> 

Logging with Log4j2

The pom xml file should be modified to load Log4j2 library instead of default logging framework. The following dependency inclusion and exclusion should be added in the pom xml file. The dependency for log4j2 will be added in the spring boot logging framework.


The log4j2 xml file is used by the log4j2 logging framework. The sample log4j2 xml file will be as shown below.


<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
        <Property name="pattern">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
        <Console name="consoleoutput" target="SYSTEM_OUT">
            <PatternLayout pattern="${pattern}" />

        <Root level="info">
            <AppenderRef ref="consoleoutput" />

Custom Log Configuration

By including the required libraries to the classpath, the different logging systems can be activated. They can be further customised by adding the appropriate configuration file to the classpath’s root or to a location specified by the logging.config.


logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy


log4j2-spring.xml, log4j2.xml

JDK – Java util logging