REW

How Do I Manage Logs In Spring Boot?

Published Aug 29, 2025 3 min read
On this page

In Spring Boot, you manage logs through a facade, SLF4J, which abstracts the specific logging framework, allowing you to switch implementations like Logback (the default) or Log4j2 with minimal code changes.

This article provides a comprehensive guide to logging, from basic setup and configuration to advanced features and best practices for production environments.

Basic logging setup

The logger instance

You obtain a Logger instance in any class using the LoggerFactory. A common practice is to create a static, final logger for each class.

Code Example:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
@Service
public class MyService {
    private static final Logger logger = LoggerFactory.getLogger(MyService.class);
    public void performAction() {
        logger.info("An informational message for an action.");
        // ... more logic
    }
}

Use code with caution.

Logging levels

Spring Boot supports several logging levels in increasing order of severity: TRACE, DEBUG, INFO, WARN, and ERROR. Choosing the right level is important for managing log verbosity.

Configuration using application.properties

Simple logging configuration can be done in application.properties or application.yml. You can set log levels for specific packages or the root logger using logging.level. To output logs to a file in addition to the console, set the logging.file.name property.

Advanced configuration with Logback

For more advanced control, create a logback-spring.xml file. This allows for features like log rotation, file appenders, custom formatting, and profile-specific configurations. You can find an example logback-spring.xml file with console and file appenders and profile configurations in the referenced document.

Advanced logging techniques

Structured logging

Structured logging, often in JSON format, makes logs easier to analyze with tools. Starting with Spring Boot 3.4, you can enable structured console and file output using logging.structured.format.console=logstash and logging.structured.format.file=logstash.

Dynamic log levels

Spring Boot Actuator allows changing log levels at runtime. Add the Actuator dependency and enable the /loggers endpoint. You can then use a POST request to modify log levels.

Centralized logging

Centralizing logs is crucial in microservice environments. Configure logging appenders to send logs to a central system and use tools like ELK Stack, Splunk, or Grafana Loki for aggregation and analysis.

Mapped Diagnostic Context (MDC)

MDC adds contextual information, such as a request ID, to log messages to trace requests. This involves adding a filter to generate an ID, placing it in the MDC, and updating the logging pattern in logback-spring.xml to include the MDC value.

Logging best practices

  • Avoid logging sensitive data.
  • Use placeholders ({}) for better performance.
  • Choose appropriate log levels for different environments.
  • Log exceptions correctly by passing the exception object.
  • Avoid over-logging to keep logs clear and manageable.
Enjoyed this article? Share it with a friend.