Quick revision
The short answer
To switch to Log4j2, we exclude spring-boot-starter-logging from the starters and add spring-boot-starter-log4j2; because Boot and our code log through SLF4J, no application code changes. logback-spring.xml is a Logback configuration that Spring Boot loads itself, after its Environment is ready, so it can use Boot's extensions <springProfile> and <springProperty>. A plain logback.xml is picked up by Logback on its own as soon as the first logger is created, which is too early for those extensions to work.
What to remember
In this answer, we saw that Spring Boot logs through SLF4J with Logback as the default backend, so switching to Log4j2 is a build change: exclude spring-boot-starter-logging, add spring-boot-starter-log4j2, and check the dependency tree. We also saw that logback-spring.xml is loaded by Boot after the Environment is ready, which is why it supports <springProfile> and <springProperty>, while logback.xml is read by Logback on its own too early for those extensions.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
To switch to Log4j2, we exclude spring-boot-starter-logging from the starters and add spring-boot-starter-log4j2; because Boot and our code log through SLF4J, no application code changes. logback-spring.xml is a Logback configuration that Spring Boot loads itself, after its Environment is ready, so it can use Boot's extensions <springProfile> and <springProperty>. A plain logback.xml is picked up by Logback on its own as soon as the first logger is created, which is too early for those extensions to work.
Why this question is asked
Logging configuration looks like a detail until something goes wrong: logs disappear after a dependency upgrade, a profile-specific appender never activates, or two logging backends fight over the classpath. Interviewers ask this question because it touches several ideas at once: how Boot chooses a logging backend, what SLF4J does, and when in the startup sequence configuration is read. In this answer, we will recap Boot's default logging stack, switch it to Log4j2 in Maven and Gradle, see why application code does not change, and then compare logback-spring.xml with logback.xml with an example of each extension.
1A Quick Recap of Boot's Logging Stack
Spring Boot separates the logging API from the logging backend. Our code and most libraries log through SLF4J, a facade, which means an interface layer that forwards calls to whatever real logging library is on the classpath. The real library is called the backend, and it decides formats, files and levels.
Every Boot starter depends on spring-boot-starter, which depends on spring-boot-starter-logging. That starter brings Logback as the backend plus bridges, small adapters that redirect other logging APIs such as java.util.logging and Log4j 2's API into SLF4J. So by default, everything ends up in Logback. Spring Boot's LoggingSystem class then detects which backend is present and configures it.
- Application code and librariescall the SLF4J API:
log.info(...) - Bridges
java.util.loggingand Log4j API redirected to SLF4J - SLF4Jthe facade, chooses the backend found on the classpath
- BackendLogback by default, or Log4j2 with spring-boot-starter-log4j2
- Appendersconsole, file, JSON
5 layers, from the top down. Step through them one at a time.
2Switching to Log4j2
Switching means removing the Logback starter and adding the Log4j2 one. In Maven, the exclusion goes on a starter that brings spring-boot-starter-logging in. Excluding it from spring-boot-starter-web is enough when that is our only path to it, but other starters can bring it back, so in larger projects a global exclusion is safer, which Gradle makes easy.
The dependency tree check at the end is worth doing every time. If Logback is still present alongside Log4j2, SLF4J finds two providers, prints a warning that multiple providers were found and picks one of them, and Boot may fail with an error that it cannot work out which logging system to use. The fix is always to find the starter that still brings spring-boot-starter-logging and exclude it there.
Configuration then moves to log4j2-spring.xml, the Log4j2 counterpart of logback-spring.xml, and the common properties such as logging.level.* and logging.file.name keep working because Boot applies them to whichever backend it finds.
3Why Application Code Does Not Change
Our classes depend only on the SLF4J API. They never import a Logback or Log4j2 class.
Notice that the output looks the same, because Boot provides matching default patterns for both backends. The switch is purely a build change. The exception is code that uses backend-specific classes directly, such as a custom Logback appender or a turbo filter; those must be rewritten for Log4j2, which is a good reason to keep such code to a minimum.
4Logback-spring.xml and Boot's Extensions
When we stay on Logback, the name of the configuration file matters more than people expect. Boot's LoggingSystem looks for logback-spring.xml and loads it during Boot's own logging initialisation, after the Environment, with its profiles and properties, is ready. That timing is what makes two Boot extensions possible: <springProfile> includes a part of the configuration only when a profile is active, and <springProperty> reads a value from the Spring Environment, such as application.properties.
The JSON pattern here is only a simple illustration; for real structured logging, Boot 3.4 and later have built-in formats. What matters is that appName came from spring.application.name and the prod block applied only with the prod profile.
5Why logback.xml Is Too Early
A file named logback.xml is also found by Logback itself. Logback initialises the moment the first LoggerFactory.getLogger() call happens, which is very early, before SpringApplication has even built the Environment. At that point there are no profiles and no application properties, so Logback has nothing to evaluate <springProfile> or <springProperty> against. Those tags are not part of Logback; Boot registers them only when it loads the configuration itself. In a plain logback.xml they are not understood, and Logback reports errors for the unknown elements or ignores them, so the profile-specific setup never takes effect.
So the rule is simple: in a Spring Boot application, name the file logback-spring.xml, or point logging.config at a file, and then Boot controls when it is loaded. The same applies to Log4j2 with log4j2-spring.xml.
6 messages in order. Play them, or step through them one at a time.
6Follow-up questions
Can I keep Logback but use Log4j2's configuration features?
No, the configuration format belongs to the backend. If we need features such as Log4j2's asynchronous loggers, we switch the backend; otherwise we use Logback's own equivalents, such as AsyncAppender.
What happens with Log4j2's own API calls in libraries?
With Logback as the backend, spring-boot-starter-logging includes log4j-to-slf4j, which sends Log4j 2 API calls to SLF4J. With Log4j2 as the backend, spring-boot-starter-log4j2 includes the SLF4J binding for Log4j2, so SLF4J calls go to Log4j2 instead. Either way, every log call ends up in one backend.
Does logging.config change any of this?
Yes. If we set logging.config=classpath:my-logging.xml, Boot loads that file itself at the right time, so the Spring extensions work in it whatever its name. It is useful when different deployments need different files.
7Conclusion
In this answer, we saw that Spring Boot logs through SLF4J with Logback as the default backend, so switching to Log4j2 is a build change: exclude spring-boot-starter-logging, add spring-boot-starter-log4j2, and check the dependency tree. We also saw that logback-spring.xml is loaded by Boot after the Environment is ready, which is why it supports <springProfile> and <springProperty>, while logback.xml is read by Logback on its own too early for those extensions.
Open with
Spring Boot logs through SLF4J, a facade, and by default spring-boot-starter-logging brings Logback as the backend. To switch to Log4j2, I exclude spring-boot-starter-logging from the starters that pull it in, in Gradle usually with a global exclusion, and add spring-boot-starter-log4j2.
Then, if they want more
Then I check the dependency tree, because if Logback is still there SLF4J finds two providers and Boot may refuse to choose. My application code does not change at all, because it only uses the SLF4J Logger and LoggerFactory, and Boot applies logging.level properties and similar settings to whichever backend it finds. Configuration moves to log4j2-spring.xml.
About logback-spring.xml versus logback.xml: the difference is who loads the file and when. logback.xml is found by Logback itself on the very first getLogger call, before Spring has built the Environment, so there are no profiles or properties yet. logback-spring.xml is loaded by Boot's LoggingSystem after the Environment is ready, so Boot can offer the springProfile tag, to include parts only for a profile, and springProperty, to read values like spring.application.name.
That is why in Boot applications I always use the -spring variant or logging.config.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do you switch a Spring Boot application from Logback to Log4j2, and what is the...": https://scrivio-interview-library.netlify.app/spring-boot/boot-logging/b662ddc6b5/