Sub topic 10
Different logging for dev and prod profiles
This chapter assumes you can write a basic Spring Boot application, edit application.properties, and activate a Spring profile. It also assumes you have seen the default Logback setup and log levels from earlier chapters.
Why the environments need different logging
Development and production need different things from logs.
- Development: You want verbose output, such as
DEBUGfor your own packages, readable single-line text, and no log files to clean up. - Production: You want
INFOorWARNas the baseline, a file or structured output that a log collector can read, and rolling so disks do not fill up. Verbose levels cost I/O and can expose sensitive data.
Spring profiles let you keep one build and switch behavior at startup. Spring Boot gives you two ways to vary logging by profile:
- Profile-specific property files, such as
application-dev.properties. <springProfile>blocks insidelogback-spring.xml.
Use the first for simple differences, such as levels, file names, and patterns. Use the second when the structure of the configuration differs, such as different appenders.
Profile-specific property files
Spring Boot loads application.properties first. It then loads application-{profile}.properties for each active profile, and values from the profile file override the base file. Logging properties follow the same rule.
Put shared settings in the base file:
logging.level.root=info
logging.level.com.example.orders=infoThen override only what differs:
# application-dev.properties
logging.level.com.example.orders=debug
logging.level.org.springframework.web=debug
logging.pattern.console=%d{HH:mm:ss.SSS} %-5level %logger{20} - %msg%n# application-prod.properties
logging.level.root=warn
logging.level.com.example.orders=info
logging.file.name=/var/log/orders/app.log
logging.logback.rollingpolicy.max-file-size=50MB
logging.logback.rollingpolicy.max-history=14You activate a profile in one of these ways:
java -jar app.jar --spring.profiles.active=prod
SPRING_PROFILES_ACTIVE=prod java -jar app.jarThe environment variable form suits containers and deployment tools, because it needs no change to the command line.
Using springProfile in logback-spring.xml
Property files cannot choose a different appender or add a second one. For that you need a Logback configuration file. Spring Boot's <springProfile> element wraps any part of the configuration and includes it only when the profile matches.
<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 include lines pull in Spring Boot's own defaults: the conversion rules, the default patterns, and a CONSOLE appender. Your file only has to decide which appenders attach to the root logger for each profile.
Why the file must be named logback-spring.xml
Logback finds logback.xml very early, before Spring has built its Environment. At that point the profile information is not available, so Spring-specific elements such as <springProfile> and <springProperty> cannot work. Spring Boot looks for logback-spring.xml later in startup, after the Environment exists. If you put <springProfile> in a file named logback.xml, Logback will reject the element.
Profile expressions
The name attribute accepts a single profile, a comma-separated list, or an expression.
| Value | Matches when | Column |
|---|---|---|
| `dev` | `dev` is active | |
| `dev, test` | `dev` or `test` is active | |
| `!prod` | `prod` is not active | |
| `dev \ | test` | `dev` or `test` is active |
| `cloud & !local` | `cloud` is active and `local` is not | |
| `(dev \ | test) & !local` | one of `dev` or `test` is active, and `local` is not |
If you mix & and | in one expression, you must use parentheses. Spring does not guess the precedence for you.
Adding a file appender only in production
A common production layout logs to both the console and a rolling file. This version attaches the file appender only for prod:
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<springProfile name="!prod">
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<springProfile name="prod">
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</springProfile>
</configuration>Two details matter here.
- The
FILEappender writes to the path inlogging.file.nameorlogging.file.path. Set one of them inapplication-prod.properties, as shown earlier. Without it, the appender has no file to write to. - The
includefor the file appender sits outside anyspringProfile, so the appender is defined in every profile. It is only attached to a logger inprod. Defining an appender and not attaching it does no harm.
The rolling settings from the earlier chapter, max-file-size and max-history, still apply, because the included file appender reads them.
Using other properties with springProperty
You can also read a value from the Spring Environment into the Logback configuration:
<springProperty scope="context" name="appName"
source="spring.application.name" defaultValue="app"/>After this line, you can write ${appName} in patterns or file names. The defaultValue is used when the property is not set.
How the pieces combine
The XML file and the properties both take part in startup, and they do not always agree. The order is:
The practical rule is that logging.level.* properties are applied after the XML file. If logback-spring.xml sets the root level to INFO for prod, but application-prod.properties sets logging.level.root=warn, the property wins. Pick one place for levels per concern. Keeping levels in properties and structure in XML avoids confusion.
Common mistakes
- Using
logback.xmlwith<springProfile>. The element is rejected because the file is read before Spring is ready. Rename it tologback-spring.xml. - Forgetting the no-profile case. If no profile is active, neither
devnorprodmatches, and your configuration may attach no appender to the root logger. A!prodblock, as above, covers it. Alternatively, set a default profile inapplication.properties. - Setting
spring.profiles.activein a profile file. Spring Boot does not allow activating profiles from inside a profile-specific file. Set it in the base file, on the command line, or in an environment variable. - Wrong file path in production. A relative
logging.file.name=logs/app.logresolves against the working directory of the process. In a container or service manager that may be read-only or not what you expect. Use an absolute path. - Leaving
DEBUGon in production. A copieddevblock can reach production if theprodblock is missing a root level. Check the effective level after deployment. - Mixing
&and|without parentheses. The expression is invalid and the application fails to start.
Choosing between the two approaches
| Need | Use |
|---|---|
| Different levels per environment | Profile property files |
| Different log file location or rolling size | Profile property files |
| Different console pattern | Profile property files |
| Different set of appenders per environment | `springProfile` in `logback-spring.xml` |
| Custom appender or encoder in one environment only | `springProfile` in `logback-spring.xml` |
Start with property files. Move to XML only when a property cannot express what you need. Fewer moving parts means fewer surprises when a profile is activated by a deployment tool.
Exercise
Your logback-spring.xml has a <springProfile name="prod"> block that sets the root level to INFO. In application-prod.properties someone also set logging.level.root=warn. Production is started with SPRING_PROFILES_ACTIVE=prod. Which root level is in effect, and what would you change so the application also behaves sensibly when no profile is active?
Show answer
The root level is WARN, because logging.level.* properties are applied after the XML file and override it. To handle the no-profile case, add a second block such as <springProfile name="!prod"> with a root logger and the CONSOLE appender, or set a default profile in application.properties. To avoid the conflict, keep the root level in only one place.