Sub topic 7
Writing logs to the console and to files
This chapter assumes you can start a Spring Boot application, edit application.properties, and set per-package log levels. It covers where log lines go and how to keep log files from growing without limit.
Console output is the default
A new Spring Boot application writes every log line to the console (standard output) and nowhere else. You don't configure anything for this. Spring Boot attaches a Logback console appender to the root logger when the application starts.
An appender is the Logback component that sends a log event to a destination. The console appender sends it to System.out. The file appender, covered below, sends it to a file.
Console output works well in three situations:
- Local development, where you read the terminal.
- Containers, where Docker or Kubernetes captures stdout and forwards it to a log collector.
- Platform-managed hosting, where the platform collects stdout for you.
In the last two cases, writing your own log files often adds work without adding value. The platform already handles rotation and shipping.
Turning on file output
Spring Boot writes to a file only when you set one of two properties:
logging.file.nameis the file to write to. It can be a relative or an absolute path.logging.file.pathis a directory. Spring Boot createsspring.loginside it.
logging.file.name=logs/app.logWith this setting, each log event goes to both the console and logs/app.log. The console appender stays active. A relative path resolves against the working directory of the process, not against your project or the JAR location. Starting the same JAR from a different directory therefore creates the log file somewhere else.
If you set both properties, logging.file.name wins and logging.file.path is ignored. Pick one.
# Writes /var/log/orders/spring.log
logging.file.path=/var/log/ordersSpring Boot creates missing parent directories. It cannot fix permissions, though. If the process user cannot write to the target, the file appender fails and you get no file output. Check this first when a log file doesn't appear.
How a log event reaches each destination
The level check happens first, and it applies to both destinations. You can't send DEBUG to the file and INFO to the console with properties alone. That split needs a threshold filter in logback-spring.xml, which a later chapter covers.
Rolling: keeping files from growing forever
A single log file that grows forever will eventually fill the disk. Rolling solves this. When the active file reaches a limit, Logback renames it into an archive and starts a fresh file. Spring Boot configures a rolling policy for you as soon as you enable file output. These properties tune it:
| Property | Meaning | Default |
|---|---|---|
| `logging.logback.rollingpolicy.max-file-size` | Size at which the active file rolls | `10MB` |
| `logging.logback.rollingpolicy.max-history` | Number of archive days to keep | `7` |
| `logging.logback.rollingpolicy.total-size-cap` | Maximum combined size of all archives | `0B` (no cap) |
| `logging.logback.rollingpolicy.clean-history-on-start` | Delete old archives when the app starts | `false` |
| `logging.logback.rollingpolicy.file-name-pattern` | Name of archived files | `${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz` |
A realistic configuration looks like this:
logging.file.name=logs/app.log
logging.logback.rollingpolicy.max-file-size=25MB
logging.logback.rollingpolicy.max-history=30
logging.logback.rollingpolicy.total-size-cap=2GB
logging.logback.rollingpolicy.clean-history-on-start=true
logging.logback.rollingpolicy.file-name-pattern=logs/archive/app.%d{yyyy-MM-dd}.%i.gzThe name pattern controls both the archive location and the rolling schedule:
%d{yyyy-MM-dd}is the date. The file also rolls at midnight.%iis an index. It distinguishes several archives created on the same day because the size limit was hit.- The
.gzsuffix makes Logback compress each archive. Use.loginstead to keep them uncompressed.
Together, these properties give you a policy: the file rolls at 25 MB or at midnight, archives are compressed, anything older than 30 days is deleted, and the archive folder never exceeds 2 GB.
Details that surprise people
max-historycounts periods, not files. With a daily pattern it means days. If your pattern contained%d{yyyy-MM}, the same value would mean months.total-size-caphas no effect withoutmax-history. Logback applies the cap while it cleans old archives, so set both.- Rolling is a Logback feature. The
logging.logback.rollingpolicy.*properties do nothing if you switch to Log4j2. Older Spring Boot versions (before 2.4) used the nameslogging.file.max-sizeandlogging.file.max-history. Check which version you run before copying properties from an old article. - Cleanup runs only when a roll happens. An idle application with old archives won't delete them until the next rollover or restart.
clean-history-on-startcovers the restart case. - Don't let several instances share one file. Two JVMs writing and rolling the same file will corrupt each other's archives. Give each instance its own file name, or log to the console and let a collector do the work.
Different patterns for each destination
The console and the file have separate pattern properties, so each can have its own format:
logging.pattern.console=%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%nA common choice is a short pattern on the console for developers and a full one in the file, with the date and thread name. Pattern syntax gets its own chapter.
Colors and character encoding
Spring Boot colors console output when the terminal supports ANSI escape codes. Color is useful in a terminal and harmful in a captured file, because the escape codes show up as stray characters. Control it with spring.output.ansi.enabled:
spring.output.ansi.enabled=neverThe accepted values are always, detect (the default), and never. The file appender never writes color codes.
Encoding is set per destination too:
logging.charset.console=UTF-8
logging.charset.file=UTF-8The file charset defaults to UTF-8. Set the console charset explicitly if non-ASCII characters appear garbled on a Windows terminal.
Overriding at launch
Because these are ordinary Spring properties, you can override them without rebuilding. Command-line arguments take precedence over application.properties:
java -jar orders.jar \
--logging.file.name=/var/log/orders/orders.log \
--logging.logback.rollingpolicy.max-history=14Environment variables work as well. LOGGING_FILE_NAME maps to logging.file.name. This lets the same artifact run in development with no file and in production with a rolled file.
Troubleshooting checklist
If no file appears, work through these checks in order:
- Confirm that
logging.file.nameorlogging.file.pathis actually set. The startup banner and log lines don't tell you this. Check the effective properties. - Check that the process user can create files in the target directory.
- Look at the working directory if you used a relative path. The file may be somewhere unexpected.
- Check whether a
logback-spring.xmlfile is present. A custom Logback file replaces Spring Boot's defaults in the places where it defines its own appenders, so the properties above may no longer apply. - Search the first lines of console output for Logback status messages. A failed appender prints a warning there.
Exercise
An application is configured with logging.file.name=logs/app.log and logging.logback.rollingpolicy.total-size-cap=1GB, but no max-history value is set. The logs folder keeps growing past 1 GB of archives over several weeks. Which settings would you check or change, and why?
Show answer
Two things explain this. First, the cap is applied while Logback cleans old archives, and that cleanup runs only when a roll happens, so an idle period delays enforcement. Second, the Spring Boot default max-history is 7 days, so you can't rely on the cap alone to explain growth. Check that the archive files really match the file name pattern. Files outside file-name-pattern, such as manually copied logs or archives from an earlier pattern, are never cleaned. Then set max-history explicitly, for example 30, so both limits apply together. Optionally set clean-history-on-start=true so a restart also removes excess archives.
Exercise
You want the console to show a short line with time, level, and message, and the log file to show the full date, thread, and logger name. Which two properties do you set, and what happens to ANSI color codes in the file?
Show answer
Set logging.pattern.console for the short format and logging.pattern.file for the detailed format. The file appender does not write ANSI color codes. Color is applied only to console output, and you can disable it there with spring.output.ansi.enabled=never.