Polish "Support for capping archived log files"

Closes gh-15325
This commit is contained in:
Stephane Nicoll
2019-02-12 16:29:22 +01:00
parent 999780f342
commit f42cec9eac
7 changed files with 64 additions and 65 deletions

View File

@@ -37,13 +37,12 @@ content into your application. Rather, pick only the properties that you need.
# LOGGING
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.file= # Log file name (for instance, `myapp.log`). Names can be an exact location or relative to the current directory.
logging.file.clean-history-on-start=false # Whether to clean the archive log files on startup. 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.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.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.file.total-size-cap=0 # Total size of log backups to be kept. 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.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.

View File

@@ -1859,6 +1859,11 @@ To help with the customization, some other properties are transferred from the S
|`LOG_EXCEPTION_CONVERSION_WORD`
|The conversion word used when logging exceptions.
|`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.name`
|`LOG_FILE`
|If defined, it is used in the default log configuration.
@@ -1873,20 +1878,15 @@ setup.)
|Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with
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`
|`LOG_PATH`
|If defined, it is used in the default log configuration.
|`logging.file.total-size-cap`
|`LOG_FILE_TOTAL_SIZE_CAP`
|Total size of log backups to be kept (if LOG_FILE enabled). (Only supported with the
default Logback setup.)
|`logging.pattern.console`
|`CONSOLE_LOG_PATTERN`
|The log pattern to use on the console (stdout). (Only supported with the default Logback

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -60,6 +60,11 @@ public class LoggingSystemProperties {
*/
public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN";
/**
* 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 file log pattern.
*/
@@ -80,11 +85,6 @@ public class LoggingSystemProperties {
*/
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.
*/
@@ -117,11 +117,11 @@ public class LoggingSystemProperties {
setSystemProperty(PID_KEY, new ApplicationPid().toString());
setSystemProperty(resolver, CONSOLE_LOG_PATTERN, "pattern.console");
setSystemProperty(resolver, FILE_LOG_PATTERN, "pattern.file");
setSystemProperty(resolver, FILE_CLEAN_HISTORY_ON_START,
"file.clean-history-on-start");
setSystemProperty(resolver, FILE_MAX_HISTORY, "file.max-history");
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_DATEFORMAT_PATTERN, "pattern.dateformat");
if (logFile != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -141,6 +141,8 @@ class DefaultLogbackConfiguration {
private void setRollingPolicy(RollingFileAppender<ILoggingEvent> appender,
LogbackConfigurator config, String logFile) {
SizeAndTimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new SizeAndTimeBasedRollingPolicy<>();
rollingPolicy.setCleanHistoryOnStart(this.patterns.getProperty(
"logging.file.clean-history-on-start", Boolean.class, false));
rollingPolicy.setFileNamePattern(logFile + ".%d{yyyy-MM-dd}.%i.gz");
setMaxFileSize(rollingPolicy,
this.patterns.getProperty("logging.file.max-size", MAX_FILE_SIZE));
@@ -148,9 +150,7 @@ class DefaultLogbackConfiguration {
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")));
String.valueOf(CoreConstants.UNBOUNDED_TOTAL_SIZE_CAP))));
appender.setRollingPolicy(rollingPolicy);
rollingPolicy.setParent(appender);
config.start(rollingPolicy);

View File

@@ -76,6 +76,13 @@
"replacement": "logging.file.name"
}
},
{
"name": "logging.file.clean-history-on-start",
"type": "java.lang.Boolean",
"description": "Whether to clean the archive log files on startup. Only supported with the default logback setup.",
"sourceType": "org.springframework.boot.context.logging.LoggingApplicationListener",
"defaultValue": false
},
{
"name": "logging.file.name",
"type": "java.lang.String",
@@ -107,14 +114,7 @@
"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
"defaultValue": "0"
},
{
"name": "logging.group",

View File

@@ -13,11 +13,11 @@ initialization performed by Boot
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
<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>
</appender>
</included>

View File

@@ -352,6 +352,35 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(getLineWithText(file, "Hello world")).doesNotContain("INFO");
}
@Test
public void testCleanHistoryOnStartProperty() {
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() {
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
public void testMaxFileSizeProperty() {
MockEnvironment environment = new MockEnvironment();
@@ -413,7 +442,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
}
@Test
public void testTotalSizeCapProperty() throws Exception {
public void testTotalSizeCapProperty() {
String expectedSize = "101 MB";
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.total-size-cap", expectedSize);
@@ -429,7 +458,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
}
@Test
public void testTotalSizeCapPropertyWithXmlConfiguration() throws Exception {
public void testTotalSizeCapPropertyWithXmlConfiguration() {
String expectedSize = "101 MB";
MockEnvironment environment = new MockEnvironment();
environment.setProperty("logging.file.total-size-cap", expectedSize);
@@ -445,35 +474,6 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
.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
public void exceptionsIncludeClassPackaging() {
this.loggingSystem.beforeInitialize();