Sub topic 10
Different logging for dev and prod profiles
This chapter assumes you can write a Spring Boot application, know how application.properties works, and understand what a Spring profile is. It also assumes you have met the log levels and the idea of a Logback configuration file.
Why logging differs between environments
On a developer machine you want detail. DEBUG output helps you follow what the code is doing, and the volume does not matter. In production the same output costs disk space and money, and it can bury the lines that matter. Production usually runs at INFO or WARN.
Spring Boot gives you two ways to vary logging by profile:
- Profile-specific property files, such as
application-dev.properties. - The
<springProfile>element insidelogback-spring.xml.
Option 1: profile-specific properties
When the dev profile is active, Spring Boot loads application-dev.properties after application.properties. Values in the profile file override the base file. Put shared settings in the base file and only the differences in the profile files.
The next two lines are settings you might put in application-dev.properties and application-prod.properties. They are shown together here for comparison:
# application-dev.properties
logging.level.root=info
logging.level.com.example.orders=debug
# application-prod.properties
logging.level.root=warn
logging.file.name=logs/app.logThis approach is enough when the only differences are levels, file names, or patterns.
Option 2: springProfile in logback-spring.xml
Use XML when the structure of the configuration changes between profiles, for example when different appenders are attached. The file must be named logback-spring.xml. Spring Boot processes that name, so <springProfile> works. A plain logback.xml is read by Logback before Spring starts, so <springProfile> fails there.
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<springProfile name="dev">
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<springProfile name="prod">
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
</configuration>The two include lines reuse Spring Boot's default pattern and console appender, which is named CONSOLE. Each <springProfile> block only takes effect when its profile is active.
The name attribute accepts a profile expression. You can write name="dev | test" for either profile, or name="!prod" for every profile except prod.
Activating a profile
Set the active profile when you start the application:
java -jar app.jar --spring.profiles.active=prodYou can also set the SPRING_PROFILES_ACTIVE environment variable. This is common in containers.
Common errors
- No profile active. If neither block matches, the file defines no root logger. Nothing is logged at the level you expect. Add a default block such as
<springProfile name="!dev & !prod">, or put a root logger outside any profile block. - Using
logback.xml. The<springProfile>element causes an error or is ignored. Rename the file tologback-spring.xml. - Debug logging left on in production. Check the effective level at startup, and never rely on a profile that you did not actually activate.
- Expecting both mechanisms to merge cleanly. When
logback-spring.xmldefines the root level, it wins overlogging.level.rootin properties for that logger. Pick one place for each setting.
Exercise
Your logback.xml contains a <springProfile name="prod"> block, but Spring Boot reports an error at startup. What is the cause and the fix?
Show answer
Logback reads logback.xml before Spring Boot is ready, so it does not understand <springProfile>. Rename the file to logback-spring.xml so Spring Boot processes it.
Exercise
Write a <springProfile> name expression that matches every profile except prod.
Show answer
name="!prod"