Clean • Professional
Spring Boot provides a flexible, layered logging system that abstracts the underlying logging framework while providing production-ready defaults. Understanding its internals helps in diagnostics, performance tuning, and customization.
Spring Boot uses SLF4J (Simple Logging Facade for Java) as the abstraction layer.
Logger (SLF4J API)Application Code
↓
SLF4JLogger(Facade)
↓
Spring Boot LoggingSystem
↓
Underlying LoggingFramework(Logback, Log4j2, JUL)
↓
Appender → Layout/Encoder → Destination (Console/File/Remote)
Example Usage:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
publicclassMyService {
privatestaticfinalLoggerlogger= LoggerFactory.getLogger(MyService.class);
publicvoidprocess() {
logger.info("Processing started");
}
}
Spring Boot provides automatic logging configuration out-of-the-box:
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%nOverride via application.properties:
logging.level.root=DEBUG
logging.level.com.example=TRACE
logging.file.name=app.log
logging.pattern.console=%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
Spring Boot initializes logging before ApplicationContext creation using LoggingApplicationListener:
logback-spring.xml / logback.xmllog4j2-spring.xml / log4j2.xmllogging.propertiesLOG_FILE, LOG_PATH.Internal Classes:
| Class | Role |
|---|---|
LoggingApplicationListener | Main listener configuring log system early |
DefaultLoggingSystem | Provides defaults for Console + File logging |
LogbackLoggingSystem | Detects logback.xml or spring defaults, sets log level |
LoggerConfiguration | Holds logger name and effective level |
LoggingSystemProperties | Reads application.properties overrides |
TRACE < DEBUG < INFO < WARN < ERRORExample:
com.example →INFO
com.example.service →DEBUG
com.example.service.PaymentService →inheritsDEBUG
Log events pass through:
logger.debug("message"))Appender Types:
Log Formatting:
Spring Boot supports patterns for timestamp, thread, level, logger, message, exceptions.
Example console pattern:
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]%-5level%logger{36} - %msg%n
AsyncAppender in Logback.Example (Logback XML):
<appendername="ASYNC"class="ch.qos.logback.classic.AsyncAppender">
<appender-refref="FILE"/>
</appender>
FILE is the actual appender defined elsewhere, e.g., a RollingFileAppenderAsyncAppender| Property | Description |
|---|---|
logging.level.root | Sets the root logger level (global default) |
logging.level.com.example | Sets package- or class-specific logging level |
logging.pattern.console | Defines the console log message format / pattern |
logging.file.name | Specifies the log file output location |
logging.file.max-size | Sets the maximum size for rolling log files |
logging.file.max-history | Defines the number of archived log files to keep |
Spring Boot handles logging even before ApplicationContext exists:
LoggingApplicationListener triggers at ApplicationStartingEventlogging.level.* auto-applies to loggersapplication-dev.yml can define different logging levels/actuator/loggers endpoint allows runtime log level inspection & changeTRACE only for certain packages at runtime{} placeholders.Logger → Filters → Appenders → Encoder → Destination
Supported Features:
Benefits of Proper Setup:
| Backend | Best Use Case |
|---|---|
| Logback | Default choice for most Spring Boot applications; simple, performant, Spring Boot optimized |
| Log4j2 | High-throughput, structured logging, cloud-native or enterprise apps |
| JUL (java.util.logging) | Legacy applications only; limited features, not recommended for new projects |
Spring Boot’s logging engine is powerful, flexible, and production-ready. Proper understanding of its internals allows developers to: