Logging in Spring Boot: A Comprehensive Guide

Logging is a crucial aspect of any application. It helps in debugging, monitoring, and maintaining the application. Spring Boot provides extensive support for logging, making it easy to configure and use. This guide will walk you through setting up and using logging in a Spring Boot 3.2 application.


  • JDK 17 or later
  • Maven or Gradle
  • IDE (IntelliJ IDEA, Eclipse, etc.)

Setting Up the Project

Step 1: Create a New Spring Boot Project

Use Spring Initializr to create a new project with the following dependencies:

  • Spring Web

Download and unzip the project, then open it in your IDE.

Step 2: Basic Logging Configuration

Spring Boot uses Logback as the default logging framework. It also supports other logging frameworks such as Log4j2 and Java Util Logging.

2.1 Using application.properties for Logging Configuration

In the src/main/resources directory, create or update the application.properties file to configure logging.

# src/main/resources/application.properties

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


  • logging.level.root=INFO: Sets the root logging level to INFO.
  • logging.level.org.springframework.web=DEBUG: Sets the logging level for Spring Web to DEBUG.
  • logging.file.name=logs/spring-boot-application.log: Specifies the file name for logging.
  • logging.pattern.console: Sets the pattern for console logging.
  • logging.pattern.file: Sets the pattern for file logging.

2.2 Using application.yml for Logging Configuration

Alternatively, you can use the application.yml file for logging configuration.

# src/main/resources/application.yml

    root: INFO
    org.springframework.web: DEBUG
    name: logs/spring-boot-application.log
    console: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
    file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"


  • The YAML configuration mirrors the properties file configuration but uses YAML syntax.

Step 3: Using Logging in Your Application

3.1 Create a Sample Controller

Create a controller class named HelloController in the com.example.demo.controller package.

package com.example.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

public class HelloController {

    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    public String hello(@RequestParam(required = false, defaultValue = "World") String name) {
        logger.info("Received request to /hello with name: {}", name);
        return "Hello, " + name + "!";


  • Logger and LoggerFactory: Classes from SLF4J (Simple Logging Facade for Java) to log messages.
  • logger.info: Logs an informational message.
  • logger.debug, logger.error, etc.: Other logging levels.

Step 4: Advanced Logging Configuration

4.1 Configuring Logback with logback-spring.xml

For more advanced logging configurations, you can use a logback-spring.xml file. Create this file in the src/main/resources directory.

    <property name="LOGS" value="logs"/>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
            <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern>

    <logger name="org.springframework.web" level="DEBUG"/>
    <root level="INFO">
        <appender-ref ref="FILE"/>
        <appender-ref ref="CONSOLE"/>


  • RollingFileAppender: Logs messages to a file with rolling based on time.
  • ConsoleAppender: Logs messages to the console.
  • logger and root: Configures logging levels and appenders.

4.2 Using Log4j2

If you prefer Log4j2, include the necessary dependencies in your pom.xml file and configure log4j2-spring.xml.

Adding Log4j2 Dependencies


Creating log4j2-spring.xml

Create log4j2-spring.xml in the src/main/resources directory.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"/>
        <RollingFile name="File" fileName="logs/spring-boot-application.log" filePattern="logs/spring-boot-application-%d{yyyy-MM-dd}.log">
                <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
            <DefaultRolloverStrategy max="30"/>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        <Logger name="org.springframework.web" level="debug" additivity="false">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>


  • Configuration: Defines the configuration for Log4j2.
  • Appenders: Defines where the log messages will be sent (console and file).
  • Loggers: Defines logging levels and the appenders for different packages.

Step 5: Running and Testing the Application

5.1 Run the Application

Run the Spring Boot application using your IDE or the command line:

./mvnw spring-boot:run

5.2 Test Logging

Use a web browser or a tool like Postman to send a GET request to http://localhost:8080/hello?name=SpringBoot. Check the console and log files to see the logging messages.


In this comprehensive guide, you have learned how to set up and use logging in a Spring Boot 3.2 application. We covered:

  • Basic logging configuration using application.properties and application.yml.
  • Using SLF4J for logging in your application.
  • Advanced logging configuration using logback-spring.xml.
  • Configuring and using Log4j2.

By following these steps, you can effectively configure and manage logging in your Spring Boot applications to help with debugging, monitoring, and maintaining your application.