diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java b/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java index 221cfa0727..d6c4921154 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.boot.logging.logback; +import java.lang.reflect.Method; import java.nio.charset.Charset; import ch.qos.logback.classic.Level; @@ -35,6 +36,7 @@ import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertyResolver; import org.springframework.core.env.PropertySourcesPropertyResolver; +import org.springframework.util.ReflectionUtils; /** * Default logback configuration used by Spring Boot. Uses {@link LogbackConfigurator} to @@ -133,22 +135,36 @@ class DefaultLogbackConfiguration { encoder.setPattern(OptionHelper.substVars(logPattern, config.getContext())); appender.setEncoder(encoder); config.start(encoder); - appender.setFile(logFile); + getRollingPolicy(appender, config, logFile); + getMaxFileSize(appender, config); + config.appender("FILE", appender); + return appender; + } + private void getRollingPolicy(RollingFileAppender appender, + LogbackConfigurator config, String logFile) { FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy(); rollingPolicy.setFileNamePattern(logFile + ".%i"); appender.setRollingPolicy(rollingPolicy); rollingPolicy.setParent(appender); config.start(rollingPolicy); + } + private void getMaxFileSize(RollingFileAppender appender, + LogbackConfigurator config) { SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy(); - triggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB")); + try { + triggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB")); + } + catch (NoSuchMethodError ex) { + // Logback < 1.1.8 used String configuration + Method method = ReflectionUtils.findMethod(SizeBasedTriggeringPolicy.class, + "setMaxFileSize", String.class); + ReflectionUtils.invokeMethod(method, triggeringPolicy, "10MB"); + } appender.setTriggeringPolicy(triggeringPolicy); config.start(triggeringPolicy); - - config.appender("FILE", appender); - return appender; } }