Practice: configuration, profiles and logging
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
Configuration bugs rarely throw a clear exception: the app starts, quietly uses the wrong value, and fails later somewhere else. Four mistakes cause most of them.
Mistake 1: guessing which value wins
A key can be set in six places, so editing the file you know may change nothing. Work from the strongest source down: command line, -D, environment variable, outside file, profile file, base file.
Example 1 · Terminal: the same key from the file, a variable and an argument
Runs in: the config-lab practice project (Spring Boot 4.1)
# config-lab with shoplite.max-page-size=50 in application.properties# and max-page-size: 30 in application.yml (both inside the jar)java -jar target/config-lab-0.0.1-SNAPSHOT.jarSHOPLITE_MAXPAGESIZE=40 java -jar target/config-lab-0.0.1-SNAPSHOT.jarSHOPLITE_MAXPAGESIZE=40 java -jar target/config-lab-0.0.1-SNAPSHOT.jar --shoplite.max-page-size=25$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar shoplite.max-page-size = 50 $ SHOPLITE_MAXPAGESIZE=40 java -jar target/config-lab-0.0.1-SNAPSHOT.jar shoplite.max-page-size = 40 $ SHOPLITE_MAXPAGESIZE=40 java -jar target/config-lab-0.0.1-SNAPSHOT.jar --shoplite.max-page-size=25 shoplite.max-page-size = 25
What this code does
- Inside the jar,
.properties(50) beats.yml(30). The environment variable (40) beats both, and the argument (25) beats the variable. - The runner is
PageSizeReportfrom the properties vs YAML lesson; it only prints the resolved value.
People usually edit the file they know, and nothing changes, because a stronger source still sets the key. Inside one folder .properties beats .yml, and between two active profiles the one listed last wins.
When in doubt, print the value with a small runner, or add --debug to see the start-up report. Never guess on a server.
Mistake 2: broken YAML and wrong profile names
A key indented one level wrong ends up under a different parent, and a misspelled profile name is not an error either. Example 2 shows prd starting with the base values.
Example 2 · A misspelled profile starts without any warning
Runs in: the config-lab practice project (Spring Boot 4.1)
java -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=prd$ java -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=prd 2026-10-16T11:47:45.539+05:30 INFO 11756 --- [ main] com.shoplite.lab.ConfigLabApplication : The following 1 profile is active: "prd" Active profiles: [prd] Database URL: jdbc:mysql://localhost:3306/shoplite Error details: false Support email: help@shoplite.example
What this code does
- The log says profile
prdis active, and the database URL is the local one from the base file: the prod file was never read. - Reading this one log line after every deployment catches the mistake in seconds.
Unquoted values such as 1.10, 0123, off and NO silently change type in YAML. Profile mistakes are silent too: --spring.profiles.active=prd, an environment variable named SPRING_PROFILE_ACTIVE (no S), or a file named application.dev.properties (a dot instead of a dash) all start the app with the base values and no warning.
The fix is a habit: after every start, read the line The following 1 profile is active: "prod" or No active profile set in the log, and check one value you expect from that profile.
Mistake 3: missing secrets found too late
A ${DB_PASSWORD} placeholder with no variable behind it does not stop start-up: Boot keeps the literal text, and the first database call fails later. Example 3 compares this with failing fast.
Example 3 · A missing secret: late failure vs fail fast
Runs in: the config-lab practice project (Spring Boot 4.1)
# DB_PASSWORD is not setDB_USER=shoplite_app java -jar target/config-lab-0.0.1-SNAPSHOT.jar# Same, for a properties record with @NotBlank @Email String moderationEmail# bound from shoplite.reviews.moderation-email=${REVIEWS_EMAIL}java -jar target/config-lab-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod$ DB_USER=shoplite_app java -jar target/config-lab-0.0.1-SNAPSHOT.jar
... ERROR ... o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: java.sql.SQLException: Access denied for user 'shoplite_app'@'localhost' (using password: YES)
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target com.shoplite.lab.ReviewProperties failed:
Property: shoplite.reviews.moderationEmail
Value: "${REVIEWS_EMAIL}"
Origin: class path resource [application.properties] - 5:35
Reason: must be a well-formed email addressWhat this code does
- The datasource keeps the text
${DB_PASSWORD}as the password, so the failure appears only at the first connection. - The validated record rejects the same kind of unresolved text at start-up, and the report shows the literal
${REVIEWS_EMAIL}value, which tells you the variable was never set.
The fixes from this module: a prod-only RequiredSettingsCheck that calls getRequiredProperty, validation (@NotBlank, @Email) on your own properties records, which rejects the unresolved text, and no defaults for secrets.
When a secret is missing, read the last Caused by line: it names the key or the variable. Then check the environment of the process that runs the app, not your own terminal.
Mistake 4: log statements that hide the problem
A useful log line has the right level, {} placeholders, ids and the exception last. Example 4 compares a poor error line with a good one for the same failure.
Example 4 · A poor and a good error log for the same failure
Runs in: the config-lab practice project (Spring Boot 4.1)
// Poor: no stack trace, string joininglog.error("Payment failed for order " + orderId + ": " + e.getMessage());
// Good: placeholders, ids and amount, exception lastlog.error("Payment failed for order {} (Rs {})", orderId, amount, e);2026-10-16T12:15:39.421+05:30 ERROR 23580 --- [config-lab] [ main] c.shoplite.lab.payment.PaymentService : Payment failed for order 17: UPI gateway timed out after 10 s 2026-10-16T12:15:39.438+05:30 ERROR 23580 --- [config-lab] [ main] c.shoplite.lab.payment.PaymentService : Payment failed for order 17 (Rs 2796.00) java.lang.IllegalStateException: UPI gateway timed out after 10 s at com.shoplite.lab.payment.PaymentService.callUpiGateway(PaymentService.java:29) ~[classes/:na] ...
What this code does
- The poor line gives only the message. The good line adds the amount and the full stack trace, starting with the exact line that threw.
- These are the two calls from
PaymentServicein the logging lesson; real code keeps only the second one.
The most common logging bugs: e.getMessage() instead of the exception (no stack trace), string joining instead of {}, the wrong level (error for a normal event, info for a failure), messages without ids ("Something went wrong"), and secrets or personal data in the text. A good line answers what happened, to which order or product, and why: log.error("Refund for order {} (Rs {}) failed", orderId, amount, e).
Normal business events are INFO, handled surprises WARN, failures that need a person ERROR, and details for developers DEBUG. Also check that each exception is logged once, where it is handled, and that System.out.println never appears in a service.
Finish up
Mark complete & continue
Next: Mini project: ShopLite configuration and profiles