Clean • Professional
Spring Boot comes with a powerful and automatic logging system.
It is built on:
Spring Boot sets up logging very early during startup, even before the ApplicationContext is created.
This means you can see logs from the very first steps of your application.
Spring Boot Logging Architecture is the internal system that:
It uses SLF4J as the front layer (the API you write code with)
and Logback as the back layer (the engine that prints logs).
Spring Boot also gives you:
application.propertiesIn short definition: Logging Architecture is the system that collects, formats, and outputs log messages in a Spring Boot application using SLF4J as the logging API and Logback (or other frameworks) as the backend.
Your Application Code (Logger)
↓
SLF4J (Logging Facade)
↓
Spring Boot LoggingSystem (Abstraction Layer)
↓
Logback / Log4j2 / JUL (Actual Logging Engine)
↓
Appenders (Console, File, Rolling, JSON, Remote)
↓
Final Output
This flow ensures all logs—your code, Spring logs, third-party library logs—are funneled consistently to the chosen backend.
The core components of Spring Boot’s logging architecture define how log messages are captured, processed, formatted, and finally sent to the console or log files.
Spring Boot logging is built on three major layers:
org.slf4j.Logger interface.Example:
private static final Logger log = LoggerFactory.getLogger(MyService.class);
log.info("Service started");
Included through:
spring-boot-starter-logging
Handles:
You can replace Logback with Log4j2 or JUL by excluding the default dependency.
These are the core building blocks that handle logging in your application: Loggers receive messages, Appenders determine where they go, and Layouts/Formatters control how they appear.
TRACE, DEBUG, INFO, WARN, ERROR, OFFHierarchy :
Loggers are hierarchical, meaning child loggers inherit settings from their parent.
Example:
com.example → parent
com.example.service → child
Usage: Each class usually has its own logger for fine-grained control.
Define where log messages are sent (the output destinations).
Common Types:
Control the appearance and structure of log messages.
Common Elements:
Configuration: Can be customized via properties files, YAML, or XML (Logback / Log4j2 configuration).
Spring Boot automatically sets up logging through the auto-configuration layer, which ensures logs are initialized early and properly configured.
Runs very early during application startup.
Responsibilities:
Handles the actual logging backend selection.
| Logging Framework | Backend Class |
|---|---|
| Logback | LogbackLoggingSystem |
| Log4j2 | Log4J2LoggingSystem |
| JUL (Java Util Logging) | JavaLoggingSystem |
Developers rarely need to interact directly with this layer, as Spring Boot manages it automatically.
Spring Boot initializes logging before the ApplicationContext starts.
Components:
BackgroundPreinitializer
Default Logback Setup
logback.xml or properties are presentThis explains why logs appear from the earliest startup phases.
Spring Boot merges logging configurations from multiple layers to provide flexible and environment-specific logging.
Simple, property-based configuration.
Examples:
logging.level.root=INFO
logging.level.com.myapp=DEBUG
logging.file.name=app.log
logging.pattern.console=%d{HH:mm:ss} - %msg%n
Spring Boot auto-loads:
logback-spring.xml (recommended)logback.xmlSupports profile-based logging:
<springProfile name="prod">
<logger name="com.app" level="WARN"/>
</springProfile>
Allows dynamic configuration outside the application:
Example:
java -jar app.jar --logging.level.root=TRACE
Spring Boot lets you group multiple packages under a single logical name for easier log configuration.
Example:
logging.group.web=org.springframework.web, org.hibernate
logging.level.web=DEBUG
Useful for enabling/disabling logging for entire subsystems with one property.
Spring Boot supports logging to the console (enabled by default) and to files (optional). Console logs show timestamp, level, thread, logger, and message, while file logging can use rolling policies for rotation and retention.
Enabled by default in Spring Boot.
Example output:
2025-01-01 10:22:33.122 INFO 12345 --- [main] c.e.demo.Service : User created
Includes:
Enable file logging using:
logging.file.name=app.log
# or
logging.file.path=logs/
spring.log)If no file is explicitly configured, Spring Boot may create a default log file named:
spring.log
This file appears in the application's working directory and contains all log output.
Spring Boot supports the following log levels:
TRACE – Fine-grained debuggingDEBUG – General debugging informationINFO – Routine information messagesWARN – Potential issues or warningsERROR – Error eventsOFF – Disable loggingPer-package example:
logging.level.org.hibernate.SQL=DEBUG
Spring Boot allows different log levels for different profiles.
Using application.yml:
logging.level.root: INFO
---
spring.profiles: dev
logging.level.root: DEBUG
Using Logback XML:
<springProfile name="dev">
<logger name="com.app" level="DEBUG"/>
</springProfile>
Spring Boot supports features for enhanced logging beyond basic levels:
Example:
MDC.put("requestId", "123");
log.info("Processing started");
MDC.clear();
Outputs logs in JSON format, suitable for structured logging and centralized logging systems.
Example configuration:
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
Commonly used with:
Improves performance by logging in a separate thread.
Example (Log4j2):
<AsyncLogger name="com.app" level="INFO"/>Protects sensitive information from being logged.
Commonly masked data:
Spring Boot Actuator allows changing log levels at runtime without restarting the application.
management.endpoints.web.exposure.include=loggers
/actuator/loggers
SLF4J API (Your Code)
↓
Commons Logging Bridge (Spring Internals)
↓
Spring Boot LoggingSystem (Backend Selector)
↓
Logback / Log4j2 / JUL (Actual Logging Engine)
↓
Appenders (Console, File, JSON, ELK, Splunk)
↓
Formatted Output
Ensures consistent, powerful, and extensible logging throughout your application.