Sub topic 9
Taking control with logback-spring.xml
This chapter assumes you know how to set log levels and file output in application.properties, and that you have a Spring Boot application using the default Logback setup.
When properties are not enough
Properties cover levels, file names, rolling limits, and patterns. Some needs go beyond them:
- Several appenders with different patterns or destinations.
- Filters that send only certain loggers to a certain file.
- Settings that differ by Spring profile.
For these you write a Logback configuration file. Spring Boot looks for logback-spring.xml on the classpath, usually in src/main/resources.
Why the name has `-spring`
Logback also reads a file named logback.xml. Logback loads that file very early, before Spring Boot can take part. Spring Boot cannot process its own extensions in it, such as <springProfile>, and it cannot apply its logging.* properties.
Name the file logback-spring.xml. Spring Boot then initializes Logback itself, which lets you use <springProfile> and <springProperty>.
A minimal file
Rather than writing every appender from scratch, include the defaults that ship with Spring Boot. The defaults.xml file defines the conversion rules and the default patterns. The console-appender.xml file defines an appender named CONSOLE.
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>This file does the same as the default setup. The <root> element sets the root level. The <appender-ref> element connects the root logger to the CONSOLE appender. An appender that no logger references produces no output.
How the pieces connect
A logger decides whether a message is accepted, based on its level. An appender decides where an accepted message goes. A logger can reference several appenders.
Reading the file in order
Logback reads the file from top to bottom, so order matters:
<include>elements load the Spring Boot defaults, which defineCONSOLEand the pattern properties.- Your own appenders are defined next, if you have any.
<logger>and<root>elements come last, because they refer to appenders by name.
If you reference an appender before it exists, Logback prints an error at startup and the logger gets no output from it.
What still comes from properties
Once logback-spring.xml exists, logging.level.* properties still work, because Spring Boot applies them after the file is read. Pattern and file properties are different. Settings such as logging.pattern.console work only through the included defaults files, which read them. If you write your own appender with a hard-coded pattern, the property is ignored for that appender.
Common mistakes
- Wrong file name.
logback.xmlorlogback-spring.XMLis not picked up as intended. Use exactlylogback-spring.xml. - Missing appender reference. An appender defined but not referenced by any logger never writes anything.
- Wrong location. Put the file in
src/main/resources, so it ends up at the root of the classpath. - Ignored errors. Logback prints configuration problems to standard output at startup. Read the first lines of output after any change.
Exercise
You add a <springProfile name="prod"> block to logback.xml and it has no effect. What is the likely cause, and what is the fix?
Show answer
Logback loads logback.xml before Spring Boot can process Spring-specific tags, so <springProfile> is not handled. Rename the file to logback-spring.xml.
Exercise
Your logback-spring.xml defines a FILE appender, but no log file appears. What do you check first?
Show answer
Check that a <logger> or <root> element contains an <appender-ref ref="FILE"/>. An appender with no reference never receives messages.