Logback SizeBasedTriggeringPolicy未触发滚动问题技术求助
Hey Marcelo, let's figure out why your log file isn't rolling once it hits the 5MB mark—this is a common gotcha with Spring Boot's logging configurations, so let's break down the most likely fixes step by step.
1. Double-Check Your Configuration Syntax
First, make sure you're using the correct properties for Spring Boot's default logging framework (Logback). A frequent mistake is mixing up property names or using deprecated settings:
Using application.yml:
For pure size-based rolling, your config should look like this:
logging: file: name: ./your-app.log # Path to your active log file logback: rollingpolicy: max-file-size: 5MB # The key threshold for rolling file-name-pattern: ./your-app.%i.log.gz # Naming pattern for rolled logs max-history: 30 # Optional: keep up to 30 archived logs
Using application.properties:
logging.file.name=./your-app.log logging.logback.rollingpolicy.max-file-size=5MB logging.logback.rollingpolicy.file-name-pattern=./your-app.%i.log.gz
Important: Avoid using logging.file.max-size—it’s deprecated in newer Spring Boot versions and won’t work as expected.
Using a custom logback-spring.xml:
Ensure you’ve explicitly defined a SizeBasedTriggeringPolicy within your rolling appender:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>./your-app.log</file> <!-- Handles naming of rolled log files --> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>./your-app.%i.log.gz</fileNamePattern> <minIndex>1</minIndex> <maxIndex>10</maxIndex> </rollingPolicy> <!-- Triggers roll when file hits 5MB --> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>5MB</maxFileSize> </triggeringPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender>
Don’t forget to link this appender to your root logger:
<root level="INFO"> <appender-ref ref="FILE"/> </root>
2. Confirm Your Configuration Is Being Loaded
Sometimes Spring Boot picks up an unexpected logging config by mistake. Verify:
- Your custom
logback-spring.xmllives insrc/main/resources(for Maven/Gradle projects). - If using a non-standard config path, explicitly set it in
application.properties:logging.config=classpath:path/to/your-custom-logback.xml - Restart your app after changing configs—Spring Boot doesn’t hot-reload logging settings by default.
3. Rule Out Buffering Delays
Logback uses output buffering to optimize performance, which might delay the size check until the buffer flushes. For testing (or if you need immediate log writes), enable immediate flushing:
In your Logback XML encoder:
<encoder immediateFlush="true"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder>
Or in application.properties:
logging.logback.encoder.immediate-flush=true
4. Test with a Controlled Log Load
To confirm the threshold works, add a quick test endpoint to generate a large volume of logs:
@RestController public class LogTestController { private static final Logger logger = LoggerFactory.getLogger(LogTestController.class); @GetMapping("/test-log-roll") public String triggerLogRoll() { for (int i = 0; i < 10000; i++) { logger.info("Test log line {}: Filling up the log file to trigger rolling.", i); } return "Log test done—check your log directory!"; } }
Hit this endpoint and monitor your log file size. If it rolls at 5MB now, the issue was likely related to how your app was generating logs before, not the config itself.
5. Check for Framework Version Bugs
If you’re on an older Spring Boot version (pre-2.0), there might be known bugs in the logging integration. Try upgrading to the latest stable LTS version (e.g., 2.7.x or 3.x) to rule out legacy issues.
内容的提问来源于stack exchange,提问作者Marcelo Ribeiro




