Sub topic 7
Writing logs to the console and to files
This chapter assumes you can run a Spring Boot application, edit application.properties, and know what a log level is. By default Spring Boot writes logs to the console only. You will add a log file and control how it rolls over.
Console output
Logback's console appender is on by default. It needs no configuration. You can change its layout with logging.pattern.console. Pattern syntax is covered in a later chapter.
Console output is the right choice for containers and cloud platforms. The platform captures standard output and handles storage and rotation for you.
Turning on a log file
Spring Boot writes to a file only when you set one of two properties:
logging.file.nameis the file to write. It can be a relative or absolute path, such aslogs/app.log.logging.file.pathis a directory. Boot creates a file namedspring.loginside it.
Set only one of them. If both are present, logging.file.name wins. Boot creates missing directories. Relative paths resolve against the directory where the JVM starts, not the project folder.
Once a file is configured, Boot writes to both the console and the file. The file uses the same pattern as the console unless you set logging.pattern.file.
Rolling files
An unbounded log file will eventually fill the disk. A rolling policy closes the current file at a boundary and starts a new one. With Logback, the rolling policy is on by default as soon as a file is configured. You tune it with these properties:
logging.file.name=logs/app.log
logging.logback.rollingpolicy.max-file-size=10MB
logging.logback.rollingpolicy.max-history=14
logging.logback.rollingpolicy.total-size-cap=1GB
logging.logback.rollingpolicy.clean-history-on-start=false| Property | Meaning | Default |
|---|---|---|
| `max-file-size` | Size at which the current file rolls over | 10MB |
| `max-history` | Number of archived files to keep | 7 |
| `total-size-cap` | Maximum combined size of all archives | not set |
| `clean-history-on-start` | Delete old archives at startup | false |
Archived files are named with a date and an index, by default ${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz. They are compressed. You can change the name with logging.logback.rollingpolicy.file-name-pattern.
The max-history value counts the periods in the file name pattern. With a daily pattern it means days. Because the pattern includes the date, a file also rolls at midnight, not only when it reaches the size limit.
The logging.logback.rollingpolicy.* properties apply only to Logback. If you switch to Log4j2, they have no effect.
How the flow works
Checking the result
Start the application and log a few messages. Then look for the file:
ls -l logs/
tail -f logs/app.logAfter the file passes 10MB or the date changes, an archive such as app.log.2026-12-01.0.gz appears next to app.log.
Common mistakes
- Setting both
logging.file.nameandlogging.file.pathand expecting both to apply. - Expecting a file to appear without setting either property.
- Using
max-historywithout knowing it counts periods from the file name pattern. - Forgetting that the rollingpolicy properties are ignored with Log4j2.
Exercise
Your application writes logs/app.log. You want to keep at most 30 archives and never use more than 500MB for all archives together. Which properties do you set?
Show answer
Set logging.logback.rollingpolicy.max-history=30 and logging.logback.rollingpolicy.total-size-cap=500MB. Keep logging.file.name=logs/app.log.
Exercise
You set logging.file.path=/var/log/shop and no other file property. What file does Boot write to?
Show answer
/var/log/shop/spring.log. Boot uses the fixed name spring.log inside the directory given by logging.file.path.