skip to content

How do you configure CONSOLE and FILE appenders in Spring Boot, both via properties and via logback-spring.xml (including reusing Boot's defaults)?

level: middleimportance: should knowfreq 40%

answer

  1. logging.file.name / logging.file.path -> file appender
  2. rollingpolicy.* props tune rotation
  3. ConsoleAppender + RollingFileAppender in XML
  4. include defaults.xml / console-appender.xml / file-appender.xml / base.xml
  5. custom XML replaces defaults — keep a console ref

basics

~20 s

For simple needs, set logging.file.name (or logging.file.path) to enable a rolling file appender; console is on by default. For control, define ConsoleAppender and RollingFileAppender in logback-spring.xml, or <include> Spring Boot's console-appender.xml/file-appender.xml to reuse its defaults.

solid answer

~30 s

Spring Boot's default config always writes to the console. To also write to a file without XML, set logging.file.name=app.log (a specific file) or logging.file.path=/var/log (a directory where it writes spring.log) — this activates a rolling file appender with sensible size/history defaults you can tune via logging.logback.rollingpolicy.* properties. For full control, write logback-spring.xml with a <appender class="ch.qos.logback.core.ConsoleAppender"> and a <appender class="ch.qos.logback.core.rolling.RollingFileAppender"> with a SizeAndTimeBasedRollingPolicy, then reference both from <root>. To avoid re-deriving Boot's pattern/color settings, <include> the provided resources: defaults.xml, console-appender.xml, and file-appender.xml under org/springframework/boot/logging/logback/. base.xml includes all three plus a root logger.

code

java · 21 lines
java
// logback-spring.xml: reuse Boot's console, add a custom rolling file
// <configuration>
//     <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
//     <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
//
//     <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
//         <file>logs/app.log</file>
//         <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
//             <fileNamePattern>logs/app-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
//             <maxFileSize>10MB</maxFileSize>
//             <maxHistory>7</maxHistory>
//             <totalSizeCap>1GB</totalSizeCap>
//         </rollingPolicy>
//         <encoder><pattern>${FILE_LOG_PATTERN}</pattern></encoder>
//     </appender>
//
//     <root level="INFO">
//         <appender-ref ref="CONSOLE"/>
//         <appender-ref ref="FILE"/>
//     </root>
// </configuration>

go deeper

for a junior

Know console is default and logging.file.name turns on a file.

for a middle

Configure both appenders via properties and via XML, and reuse Boot's include fragments.

for a senior

Choose rotation policy, understand that custom XML replaces defaults, and separate console vs file patterns.

for a principal

Decide logging output strategy across environments (e.g., console-only JSON to stdout for containers vs rolling files on VMs) and standardize via shared config.

## Appenders, briefly An **appender** is a Logback/Log4j2 output destination. The two most common: - **ConsoleAppender** → writes to `System.out`/`System.err` (the terminal). - **FileAppender / RollingFileAppender** → writes to a file; the *rolling* variant rotates files by size and/or time so they don't grow unbounded. Each appender has an **encoder** (usually `PatternLayoutEncoder`) defining the line format via a `<pattern>`. ## Path 1 — properties only (no XML) Console logging is on by default. Enable file logging by setting exactly one of: - `logging.file.name=logs/app.log` → logs to that specific file. - `logging.file.path=/var/log/myapp` → logs to `spring.log` inside that directory. This turns on Spring Boot's built-in rolling file appender. Tune rotation with: ```properties logging.logback.rollingpolicy.max-file-size=10MB logging.logback.rollingpolicy.max-history=7 logging.logback.rollingpolicy.total-size-cap=1GB logging.logback.rollingpolicy.file-name-pattern=logs/app-%d{yyyy-MM-dd}.%i.log ``` And customize patterns/levels with `logging.pattern.console`, `logging.pattern.file`, `logging.level.*`. This path is enough for most services and requires no XML. ## Path 2 — full logback-spring.xml When you need custom appenders, filters, or MDC handling: ```xml <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder><pattern>${CONSOLE_LOG_PATTERN}</pattern></encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>logs/app.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>logs/app-%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxFileSize>10MB</maxFileSize> <maxHistory>7</maxHistory> <totalSizeCap>1GB</totalSizeCap> </rollingPolicy> <encoder><pattern>${FILE_LOG_PATTERN}</pattern></encoder> </appender> <root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> </root> </configuration> ``` ## Reusing Spring Boot's provided fragments Spring Boot ships ready-made Logback fragments so you don't hand-write its colored pattern and defaults. Include them by resource path: - `org/springframework/boot/logging/logback/defaults.xml` — conversion rules, default patterns, and sensible levels; also defines `${CONSOLE_LOG_PATTERN}` and `${FILE_LOG_PATTERN}`. - `org/springframework/boot/logging/logback/console-appender.xml` — a preconfigured `CONSOLE` appender. - `org/springframework/boot/logging/logback/file-appender.xml` — a preconfigured rolling `FILE` appender (honors `logging.file.name`). - `org/springframework/boot/logging/logback/base.xml` — includes defaults + both appenders + a root logger; a full working baseline you can start from and extend. Example minimal custom file that keeps Boot's console but adds your own file: ```xml <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <!-- add custom FILE appender here --> <root level="INFO"><appender-ref ref="CONSOLE"/></root> </configuration> ``` ## Gotchas - If you provide your own `logback-spring.xml` you **replace** the default config — console output is only present if you keep a ConsoleAppender (directly or via include). - Don't set both `logging.file.name` and `logging.file.path`; `name` takes precedence and mixing them is confusing. - The `logging.logback.rollingpolicy.*` properties only affect Boot's built-in file appender (properties path); if you define your own RollingFileAppender in XML, you configure rotation in the XML directly. - `<file>` in RollingFileAppender is the active file; rolled files follow `fileNamePattern`. Omitting `<file>` makes the active file follow the pattern too. - Console color codes may look wrong when output is redirected to a file; use the file pattern (no ANSI) for files.

  • What's the difference between logging.file.name and logging.file.path?
    logging.file.name sets a specific log file (e.g., logs/app.log). logging.file.path sets a directory in which Boot writes a file named spring.log. If both are set, name wins. Prefer just one.
  • If you add your own logback-spring.xml, do you still get console output automatically?
    No. A custom config replaces the defaults. You only get console output if you declare a ConsoleAppender yourself or <include> console-appender.xml/base.xml and reference it from the root logger.

saying these in an interview costs you the question

  • Thinking logging.logback.rollingpolicy.* properties affect a hand-written RollingFileAppender in XML
  • Assuming console output persists after supplying a custom config without a console appender
  • Setting both logging.file.name and logging.file.path expecting them to combine
  • Using plain FileAppender in production (no rotation) causing unbounded files

context