Logging

Logging in Spring Boot is an essential diagnostic and monitoring mechanism that records application behavior, system events, errors, and performance metrics during runtime. By default, Spring Boot provides built-in, pre-configured logging using the SLF4J (Simple Logging Facade for Java) API backed by the Logback implementation, allowing developers to track execution flows and troubleshoot production issues without complex manual setup.

1. Logging Fundamentals

Logging fundamentals establish the architecture, abstractions, and default behavior used by Spring Boot to record runtime messages.

  • Facade Architecture: Spring Boot uses SLF4J as an abstraction facade, allowing developers to write log statements against a single interface (org.slf4j.Logger) while keeping the underlying logging framework (such as Logback or Log4j2) interchangeable.
  • Default Setup: Any spring-boot-starter brings in spring-boot-starter-logging automatically, which configures Logback to route informational messages to the system console (stdout) with formatted timestamps, thread names, log levels, logger names, and messages.
  • Logger Creation: Loggers are typically declared using LoggerFactory.getLogger(MyClass.class) or through Lombok’s @Slf4j annotation for cleaner code.

2. Log Levels

Log levels categorize the severity, importance, and verbosity of log events, allowing granular control over which messages are output.

  • Hierarchy: The standard severity levels in order from least to most severe are:

    • TRACE — Extremely detailed diagnostic events (finer than DEBUG).
    • DEBUG — Diagnostic information useful during local development and debugging.
    • INFO — Informational messages highlighting application progress at a high level (the default root log level in Spring Boot).
    • WARN — Potentially harmful situations or unexpected conditions that do not halt operation.
    • ERROR — Error events that impair normal functioning or indicate request failures.
  • Granular Scoping: Developers can adjust log levels globally or per-package in application.properties (e.g., logging.level.org.springframework.web=DEBUG or logging.level.com.example=TRACE).

3. File Logging

File logging redirects log output from standard console streams to physical files on the host system or storage volume for persistent auditing and archiving.

  • Configuration Keys: Enabled in application properties using logging.file.name (specifies an exact log file name, e.g., app.log) or logging.file.path (specifies a directory where default spring.log files are created).
  • Rolling & Archiving: Spring Boot automatically manages file rotation using Logback’s rolling file appender, rolling over files when they reach a specific size limit (e.g., logging.logback.rollingpolicy.max-file-size=10MB) and retaining historical archives for a set period (e.g., max-history=7d).

4. Log Configuration

Log configuration provides deep customization of log formats, appenders, and environment-specific logging rules.

  • Pattern Customization: Allows custom console or file output formats using properties like logging.pattern.console and logging.pattern.file to include custom variables, colors, or MDC (Mapped Diagnostic Context) trace IDs.
  • External Config Files: For advanced features—such as custom appenders, asynchronous logging, or sending logs directly to external aggregators (like ELK/Splunk)—developers can supply a dedicated logback-spring.xml (or log4j2-spring.xml) file in src/main/resources.
  • Profile-Aware Logging: Within logback-spring.xml, the <springProfile> tag allows defining different logging behaviors and destinations depending on the active Spring profile (e.g., detailed console logs in dev, JSON-formatted file output in prod).
Scroll to Top