How to Configure Logging in Spring Boot Levels and Files
Configure Spring Boot logging: set levels per package, write rotating files, use profiles, change levels at runtime and add MDC fields.

Logging is the first debugging tool and the last one in production, and a service whose logs cannot be read or searched is hard to operate. Spring Boot gives sensible defaults, and a small set of properties covers most needs: levels per package, a file with rotation, a pattern, runtime changes through Actuator and context in every line. This guide walks through each with Spring Boot 4 and Logback, adds a custom logback-spring.xml for profile-specific behaviour, shows how to change levels in a running instance and ends with habits that keep logs useful and safe.
Levels
logging.level.root=INFO
logging.level.com.example.orders=DEBUG
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.springframework.web=WARNLevels run from least to most severe: TRACE, DEBUG, INFO, WARN, ERROR. A setting for a package covers its sub-packages, and the most specific match wins. Set the root level to INFO and raise single packages to DEBUG while investigating, instead of raising the root, because DEBUG from every library produces a flood that hides the lines you want. For groups, Spring Boot defines names such as web and sql:
logging.level.web=DEBUG
logging.level.sql=DEBUGA group saves you from remembering which packages are involved in web handling or in SQL. The detailed use of SQL logging is covered in logging SQL in Hibernate.
Files and rotation
By default logs go to the console only. Add a file:
logging.file.name=logs/app.log
logging.logback.rollingpolicy.max-file-size=50MB
logging.logback.rollingpolicy.max-history=14
logging.logback.rollingpolicy.total-size-cap=2GB
logging.logback.rollingpolicy.file-name-pattern=logs/app-%d{yyyy-MM-dd}.%i.gzFiles roll when they exceed the size or at midnight, old ones are compressed with gzip, and they are removed after 14 days or when the total size cap is reached. The cap is the setting that protects the disk, so always set it. In containers, log to the console and let the platform collect the output; that is the twelve-factor approach, and it avoids a writable log directory and the rotation question altogether.
Pattern
logging.pattern.console=%d{HH:mm:ss.SSS} %-5level [%thread] %logger{36} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%X{requestId}] %logger{36} - %msg%nThe console pattern is short for reading while developing, and the file pattern carries a full date and a request id for searching later. For machine parsing, structured output is better than a text pattern, since a log platform can index fields without regular expressions; see structured logging. Whatever format you pick, put the time zone in or log in UTC, because a log with local times is a puzzle when servers sit in different regions.
logback-spring.xml
For appenders and profile-specific configuration, add src/main/resources/logback-spring.xml. The -spring suffix lets Boot process the file with its profile support:
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<springProfile name="prod">
<root level="INFO"><appender-ref ref="CONSOLE"/></root>
</springProfile>
<springProfile name="dev">
<logger name="com.example" level="DEBUG"/>
<root level="INFO"><appender-ref ref="CONSOLE"/></root>
</springProfile>
</configuration>Without the -spring suffix, Logback reads the file too early and the springProfile element fails with a parse error. Include Boot's defaults so you keep its colours, pattern and converters, and add only what differs. Use the XML only when properties cannot express what you need, such as a second appender or different settings per profile, because properties are easier to override with environment variables.
Change levels at runtime
With Actuator, expose the loggers endpoint:
management.endpoints.web.exposure.include=health,loggerscurl -X POST http://localhost:8080/actuator/loggers/com.example.orders \
-H 'Content-Type: application/json' -d '{"configuredLevel":"DEBUG"}'This turns on debug for one package in a running instance, which is how you diagnose a production problem without a restart and without losing the state that caused it. Reset it afterwards by posting null as the level, or the instance keeps logging at debug until it restarts. Protect the endpoint, because it changes behaviour and a flood of debug output can slow the service; see the Java security checklist. In a cluster, the change applies to one instance, so repeat it per instance or use a configuration service.
MDC for context
@Component
class RequestIdFilter extends OncePerRequestFilter {
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
throws ServletException, IOException {
MDC.put("requestId", UUID.randomUUID().toString());
try {
chain.doFilter(req, res);
} finally {
MDC.clear();
}
}
}Include %X{requestId} in the pattern, and every line of a request carries the id, so you can pull one request out of a busy log. Micrometer Tracing does this for trace and span ids automatically. On virtual threads the MDC works as it does on platform threads, while with Reactor it needs the context propagation library. Across services, the same idea becomes a correlation id, as described in correlation ids.
Practical advice
Log at INFO the events someone would want to read on a normal day, such as a payment completed, and not every method call. Use parameterised messages, log.info("order {} placed", id), which skip the string building when the level is off and keep the message text stable for searching. Never log secrets, tokens or full personal data, and review new log statements for them in code review. Write messages that a stranger can act on, with the identifier and the reason, as in better log messages. Treat a log line that fires thousands of times a minute as a bug, because it costs money in storage and hides everything else.
Switch the logging framework
Logback is the default. To use Log4j2, exclude spring-boot-starter-logging and add spring-boot-starter-log4j2. Mixing frameworks gives duplicate binding warnings and logs that go to two places. Switch only for a feature you need, such as asynchronous loggers, because the default is well supported and documented.
Testing that logging is configured
A misconfigured logger shows up as missing information during an incident, which is the worst moment to learn it. Add a smoke test that starts the application with the production profile and checks that the level for your own package is INFO, that the console appender is attached and that the pattern contains the request id. The OutputCaptureExtension makes the last check easy. Review the logging properties in code review like any other configuration, since a stray logging.level.root=DEBUG in a committed file reaches production unnoticed.
Cost and retention
Logs cost money in proportion to volume, and most of the volume is noise. Review the loudest loggers once a quarter: ask the log platform for the top ten by line count, and decide for each whether the lines are needed. Lower noisy libraries to WARN, and delete statements that print on every iteration of a loop. Keep retention proportional to need, with short retention for debug output and longer for audit trails, and make the audit trail a separate logger so it can have its own rules.
When the defaults are not enough
Platforms that ship logs through an agent still want a console format they can parse, which is the structured option. For very high volume, asynchronous appenders trade durability for throughput, and a decision about what may be lost on a crash belongs to the team, not to a default. The references are the Spring Boot logging chapter and the Logback manual.


