Commit 999780f3 authored by Robert Thornton's avatar Robert Thornton Committed by Stephane Nicoll

Support for capping archived log files

See gh-15325
parent 6b799da9
...@@ -37,10 +37,13 @@ content into your application. Rather, pick only the properties that you need. ...@@ -37,10 +37,13 @@ content into your application. Rather, pick only the properties that you need.
# LOGGING # LOGGING
logging.config= # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback. logging.config= # Location of the logging configuration file. For instance, `classpath:logback.xml` for Logback.
logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions. logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.
logging.file= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.
logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup. logging.file.max-history=0 # Maximum of archive log files to keep. Only supported with the default logback setup.
logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup. logging.file.max-size=10MB # Maximum log file size. Only supported with the default logback setup.
logging.file.name= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory. logging.file.name= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.
logging.file.path= # Location of the log file. For instance, `/var/log`. logging.file.path= # Location of the log file. For instance, `/var/log`.
logging.file.total-size-cap=0 # Places a cap on the total size of log backups. Only supported with the default logback setup.
logging.file.clean-history-on-start=false # Whether to clean the archive log files on startup. Only supported with the default logback setup.
logging.group.*= # Log groups to quickly change multiple loggers at the same time. For instance, `logging.level.db=org.hibernate,org.springframework.jdbc`. logging.group.*= # Log groups to quickly change multiple loggers at the same time. For instance, `logging.level.db=org.hibernate,org.springframework.jdbc`.
logging.level.*= # Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`. logging.level.*= # Log levels severity mapping. For instance, `logging.level.org.springframework=DEBUG`.
logging.pattern.console= # Appender pattern for output to the console. Supported only with the default Logback setup. logging.pattern.console= # Appender pattern for output to the console. Supported only with the default Logback setup.
......
...@@ -1738,7 +1738,10 @@ relative to the current directory. ...@@ -1738,7 +1738,10 @@ relative to the current directory.
Log files rotate when they reach 10 MB and, as with console output, `ERROR`-level, Log files rotate when they reach 10 MB and, as with console output, `ERROR`-level,
`WARN`-level, and `INFO`-level messages are logged by default. Size limits can be changed `WARN`-level, and `INFO`-level messages are logged by default. Size limits can be changed
using the `logging.file.max-size` property. Previously rotated files are archived using the `logging.file.max-size` property. Previously rotated files are archived
indefinitely unless the `logging.file.max-history` property has been set. indefinitely unless the `logging.file.max-history` property has been set. The total size
of log archives can be capped using `logging.file.total-size-cap`. When the total size of
log archives exceeds that threshold, backups will be deleted. To force log archive cleanup
on application startup, use the `logging.file.clean-history-on-start` property.
NOTE: The logging system is initialized early in the application lifecycle. Consequently, NOTE: The logging system is initialized early in the application lifecycle. Consequently,
logging properties are not found in property files loaded through `@PropertySource` logging properties are not found in property files loaded through `@PropertySource`
...@@ -1870,6 +1873,16 @@ setup.) ...@@ -1870,6 +1873,16 @@ setup.)
|Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with |Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with
the default Logback setup.) the default Logback setup.)
|`logging.file.total-size-cap`
|`LOG_FILE_TOTAL_SIZE_CAP`
|The total size of log backups to be kept (if LOG_FILE enabled). (Only supported with
the default Logback setup.)
|`logging.file.clean-history-on-start`
|`LOG_FILE_CLEAN_HISTORY_ON_START`
|Whether to clean the archive log files on startup (if LOG_FILE enabled). (Only supported with
the default Logback setup.)
|`logging.file.path` |`logging.file.path`
|`LOG_PATH` |`LOG_PATH`
|If defined, it is used in the default log configuration. |If defined, it is used in the default log configuration.
......
...@@ -30,6 +30,7 @@ import org.springframework.util.Assert; ...@@ -30,6 +30,7 @@ import org.springframework.util.Assert;
* @author Phillip Webb * @author Phillip Webb
* @author Madhura Bhave * @author Madhura Bhave
* @author Vedran Pavic * @author Vedran Pavic
* @author Robert Thornton
* @since 2.0.0 * @since 2.0.0
*/ */
public class LoggingSystemProperties { public class LoggingSystemProperties {
...@@ -74,6 +75,16 @@ public class LoggingSystemProperties { ...@@ -74,6 +75,16 @@ public class LoggingSystemProperties {
*/ */
public static final String FILE_MAX_SIZE = "LOG_FILE_MAX_SIZE"; public static final String FILE_MAX_SIZE = "LOG_FILE_MAX_SIZE";
/**
* The name of the System property that contains the file total size cap.
*/
public static final String FILE_TOTAL_SIZE_CAP = "LOG_FILE_TOTAL_SIZE_CAP";
/**
* The name of the System property that contains the clean history on start flag.
*/
public static final String FILE_CLEAN_HISTORY_ON_START = "LOG_FILE_CLEAN_HISTORY_ON_START";
/** /**
* The name of the System property that contains the log level pattern. * The name of the System property that contains the log level pattern.
*/ */
...@@ -108,6 +119,9 @@ public class LoggingSystemProperties { ...@@ -108,6 +119,9 @@ public class LoggingSystemProperties {
setSystemProperty(resolver, FILE_LOG_PATTERN, "pattern.file"); setSystemProperty(resolver, FILE_LOG_PATTERN, "pattern.file");
setSystemProperty(resolver, FILE_MAX_HISTORY, "file.max-history"); setSystemProperty(resolver, FILE_MAX_HISTORY, "file.max-history");
setSystemProperty(resolver, FILE_MAX_SIZE, "file.max-size"); setSystemProperty(resolver, FILE_MAX_SIZE, "file.max-size");
setSystemProperty(resolver, FILE_TOTAL_SIZE_CAP, "file.total-size-cap");
setSystemProperty(resolver, FILE_CLEAN_HISTORY_ON_START,
"file.clean-history-on-start");
setSystemProperty(resolver, LOG_LEVEL_PATTERN, "pattern.level"); setSystemProperty(resolver, LOG_LEVEL_PATTERN, "pattern.level");
setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "pattern.dateformat"); setSystemProperty(resolver, LOG_DATEFORMAT_PATTERN, "pattern.dateformat");
if (logFile != null) { if (logFile != null) {
......
...@@ -45,6 +45,7 @@ import org.springframework.util.ReflectionUtils; ...@@ -45,6 +45,7 @@ import org.springframework.util.ReflectionUtils;
* @author Phillip Webb * @author Phillip Webb
* @author Madhura Bhave * @author Madhura Bhave
* @author Vedran Pavic * @author Vedran Pavic
* @author Robert Thornton
* @since 1.1.2 * @since 1.1.2
*/ */
class DefaultLogbackConfiguration { class DefaultLogbackConfiguration {
...@@ -145,6 +146,11 @@ class DefaultLogbackConfiguration { ...@@ -145,6 +146,11 @@ class DefaultLogbackConfiguration {
this.patterns.getProperty("logging.file.max-size", MAX_FILE_SIZE)); this.patterns.getProperty("logging.file.max-size", MAX_FILE_SIZE));
rollingPolicy.setMaxHistory(this.patterns.getProperty("logging.file.max-history", rollingPolicy.setMaxHistory(this.patterns.getProperty("logging.file.max-history",
Integer.class, CoreConstants.UNBOUND_HISTORY)); Integer.class, CoreConstants.UNBOUND_HISTORY));
rollingPolicy.setTotalSizeCap(
FileSize.valueOf(this.patterns.getProperty("logging.file.total-size-cap",
"" + CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP)));
rollingPolicy.setCleanHistoryOnStart(Boolean.parseBoolean(this.patterns
.getProperty("logging.file.clean-history-on-start", "false")));
appender.setRollingPolicy(rollingPolicy); appender.setRollingPolicy(rollingPolicy);
rollingPolicy.setParent(appender); rollingPolicy.setParent(appender);
config.start(rollingPolicy); config.start(rollingPolicy);
......
...@@ -102,6 +102,20 @@ ...@@ -102,6 +102,20 @@
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener", "sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
"defaultValue": 0 "defaultValue": 0
}, },
{
"name": "logging.file.total-size-cap",
"type": "java.lang.String",
"description": "Total size of log backups to be kept. Only supported with the default logback setup.",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
"defaultValue": 0
},
{
"name": "logging.file.clean-history-on-start",
"type": "java.lang.Boolean",
"description": "Whether to clean log backups on start. Only supported with the default logback setup.",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
"defaultValue": false
},
{ {
"name": "logging.group", "name": "logging.group",
"type": "java.util.Map<java.lang.String,java.util.List<java.lang.String>>", "type": "java.util.Map<java.lang.String,java.util.List<java.lang.String>>",
......
...@@ -16,6 +16,8 @@ initialization performed by Boot ...@@ -16,6 +16,8 @@ initialization performed by Boot
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize> <maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
<maxHistory>${LOG_FILE_MAX_HISTORY:-0}</maxHistory> <maxHistory>${LOG_FILE_MAX_HISTORY:-0}</maxHistory>
<totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}</totalSizeCap>
<cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
</rollingPolicy> </rollingPolicy>
</appender> </appender>
</included> </included>
...@@ -73,6 +73,7 @@ import static org.mockito.Mockito.verify; ...@@ -73,6 +73,7 @@ import static org.mockito.Mockito.verify;
* @author Ben Hale * @author Ben Hale
* @author Madhura Bhave * @author Madhura Bhave
* @author Vedran Pavic * @author Vedran Pavic
* @author Robert Thornton
*/ */
@RunWith(ModifiedClassPathRunner.class) @RunWith(ModifiedClassPathRunner.class)
@ClassPathExclusions("log4j-*.jar") @ClassPathExclusions("log4j-*.jar")
...@@ -411,6 +412,68 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -411,6 +412,68 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(getRollingPolicy().getMaxHistory()).isEqualTo(30); assertThat(getRollingPolicy().getMaxHistory()).isEqualTo(30);
} }
@Test
public void testTotalSizeCapProperty() throws Exception {
String expectedSize = "101 MB";
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.total-size-cap", expectedSize);
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
environment);
File file = new File(tmpDir(), "logback-test.log");
LogFile logFile = getLogFile(file.getPath(), null);
this.loggingSystem.initialize(loggingInitializationContext, null, logFile);
this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).contains("INFO");
assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "totalSizeCap")
.toString()).isEqualTo(expectedSize);
}
@Test
public void testTotalSizeCapPropertyWithXmlConfiguration() throws Exception {
String expectedSize = "101 MB";
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.total-size-cap", expectedSize);
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
environment);
File file = new File(tmpDir(), "logback-test.log");
LogFile logFile = getLogFile(file.getPath(), null);
this.loggingSystem.initialize(loggingInitializationContext,
"classpath:logback-include-base.xml", logFile);
this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).contains("INFO");
assertThat(ReflectionTestUtils.getField(getRollingPolicy(), "totalSizeCap")
.toString()).isEqualTo(expectedSize);
}
@Test
public void testCleanHistoryOnStartProperty() throws Exception {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.clean-history-on-start", "true");
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
environment);
File file = new File(tmpDir(), "logback-test.log");
LogFile logFile = getLogFile(file.getPath(), null);
this.loggingSystem.initialize(loggingInitializationContext, null, logFile);
this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).contains("INFO");
assertThat(getRollingPolicy().isCleanHistoryOnStart()).isTrue();
}
@Test
public void testCleanHistoryOnStartPropertyWithXmlConfiguration() throws Exception {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.clean-history-on-start", "true");
LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(
environment);
File file = new File(tmpDir(), "logback-test.log");
LogFile logFile = getLogFile(file.getPath(), null);
this.loggingSystem.initialize(loggingInitializationContext,
"classpath:logback-include-base.xml", logFile);
this.logger.info("Hello world");
assertThat(getLineWithText(file, "Hello world")).contains("INFO");
assertThat(getRollingPolicy().isCleanHistoryOnStart()).isTrue();
}
@Test @Test
public void exceptionsIncludeClassPackaging() { public void exceptionsIncludeClassPackaging() {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment