Spring Boot achieves logging through built-in support for common logging frameworks, defaulting to Logback with sensible pre-configured settings, which can be customized via a logback.xml file.
Key Points: • Logback is the default; Log4j2 and Java Util Logging are supported as swappable alternatives. • SLF4J is the facade used in application code, keeping logging calls independent of the underlying implementation. • Default console logging is enabled out of the box with reasonable formatting and log levels. • Custom log levels, patterns, and file appenders are configured by adding logback.xml (or logback-spring.xml for Spring-profile-aware configuration) to src/main/resources. • logging.level.* properties in application.yml offer a quick way to change log levels per package without touching XML.
Example: Setting logging.level.com.example.service=DEBUG in application.yml turns on verbose logging just for that package, useful for diagnosing an issue in production without redeploying a custom logback.xml.
Code Example:
<!-- logback-spring.xml -->
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>app.log</file>
<encoder>
<pattern>%d{ISO8601} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE"/>
</root>
</configuration>Interview Tip: A concise interview answer is:
"Spring Boot defaults to Logback via the SLF4J facade, so my code just calls SLF4J's Logger API. For anything beyond the defaults, I add a logback-spring.xml to control log levels, patterns, and file appenders, and use logging.level.* properties for quick, no-redeploy adjustments."