From 06de23dbd2ae73dfffdd71c2e601b3ecb158d166 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 12 Oct 2017 22:32:24 +0200 Subject: [PATCH] SONAR-8798 fix error when sonar.log.maxFiles=0 --- .../logging/Log4JPropertiesBuilder.java | 4 ++ .../logging/Log4JPropertiesBuilderTest.java | 65 ++++++++++++++++--- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java b/server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java index 8e83756002c..eccb7f5b3f5 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java +++ b/server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java @@ -35,6 +35,7 @@ import static java.lang.String.valueOf; public class Log4JPropertiesBuilder extends AbstractLogHelper { private static final String ROOT_LOGGER_NAME = "rootLogger"; + private static final int UNLIMITED_MAX_FILES = 100_000; private final Properties log4j2Properties = new Properties(); private final Props props; @@ -94,6 +95,9 @@ public class Log4JPropertiesBuilder extends AbstractLogHelper { private RollingPolicy createRollingPolicy(File logDir, String filenamePrefix) { String rollingPolicy = props.value(ROLLING_POLICY_PROPERTY, "time:yyyy-MM-dd"); int maxFiles = props.valueAsInt(MAX_FILES_PROPERTY, 7); + if (maxFiles <= 0) { + maxFiles = UNLIMITED_MAX_FILES; + } if (rollingPolicy.startsWith("time:")) { return new TimeRollingPolicy(filenamePrefix, logDir, maxFiles, StringUtils.substringAfter(rollingPolicy, "time:")); diff --git a/server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java b/server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java index 8df33a39273..4feda6aceaf 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java @@ -24,7 +24,6 @@ import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.io.File; -import java.io.IOException; import java.util.HashSet; import java.util.Properties; import java.util.Random; @@ -131,7 +130,7 @@ public class Log4JPropertiesBuilderTest { } @Test - public void configureGlobalFileLog_sets_properties_for_daily_time_rolling_policy_with_max_7_files_for_empty_props() throws IOException { + public void configureGlobalFileLog_sets_properties_for_daily_time_rolling_policy_with_max_7_files_for_empty_props() throws Exception { File logDir = temporaryFolder.newFolder(); String logPattern = randomAlphanumeric(15); underTest.configureGlobalFileLog(esRootLoggerConfig, logDir, logPattern); @@ -140,7 +139,57 @@ public class Log4JPropertiesBuilderTest { } @Test - public void configureGlobalFileLog_throws_MessageException_when_property_is_not_supported() throws IOException { + public void time_rolling_policy_has_large_max_files_if_property_is_zero() throws Exception { + File logDir = temporaryFolder.newFolder(); + String logPattern = "foo"; + Log4JPropertiesBuilder underTest = newLog4JPropertiesBuilder( + ROLLING_POLICY_PROPERTY, "time:yyyy-MM-dd", + PROPERTY_MAX_FILES, "0"); + underTest.configureGlobalFileLog(esRootLoggerConfig, logDir, logPattern); + + verifyTimeRollingPolicy(underTest, logDir, logPattern, "yyyy-MM-dd", 100_000); + } + + @Test + public void time_rolling_policy_has_large_max_files_if_property_is_negative() throws Exception { + File logDir = temporaryFolder.newFolder(); + String logPattern = "foo"; + Log4JPropertiesBuilder underTest = newLog4JPropertiesBuilder( + ROLLING_POLICY_PROPERTY, "time:yyyy-MM-dd", + PROPERTY_MAX_FILES, "-2"); + underTest.configureGlobalFileLog(esRootLoggerConfig, logDir, logPattern); + + verifyTimeRollingPolicy(underTest, logDir, logPattern, "yyyy-MM-dd", 100_000); + } + + @Test + public void size_rolling_policy_has_large_max_files_if_property_is_zero() throws Exception { + File logDir = temporaryFolder.newFolder(); + String logPattern = "foo"; + String sizePattern = "1KB"; + Log4JPropertiesBuilder underTest = newLog4JPropertiesBuilder( + ROLLING_POLICY_PROPERTY, "size:" + sizePattern, + PROPERTY_MAX_FILES, "0"); + underTest.configureGlobalFileLog(esRootLoggerConfig, logDir, logPattern); + + verifySizeRollingPolicy(underTest, logDir, logPattern, sizePattern, 100_000); + } + + @Test + public void size_rolling_policy_has_large_max_files_if_property_is_negative() throws Exception { + File logDir = temporaryFolder.newFolder(); + String logPattern = "foo"; + String sizePattern = "1KB"; + Log4JPropertiesBuilder underTest = newLog4JPropertiesBuilder( + ROLLING_POLICY_PROPERTY, "size:" + sizePattern, + PROPERTY_MAX_FILES, "-2"); + underTest.configureGlobalFileLog(esRootLoggerConfig, logDir, logPattern); + + verifySizeRollingPolicy(underTest, logDir, logPattern, sizePattern, 100_000); + } + + @Test + public void configureGlobalFileLog_throws_MessageException_when_property_is_not_supported() throws Exception { File logDir = temporaryFolder.newFolder(); String logPattern = randomAlphanumeric(15); String invalidPropertyValue = randomAlphanumeric(3); @@ -154,7 +203,7 @@ public class Log4JPropertiesBuilderTest { } @Test - public void configureGlobalFileLog_sets_properties_for_time_rolling_policy_with_max_7_files_when_property_starts_with_time_colon() throws IOException { + public void configureGlobalFileLog_sets_properties_for_time_rolling_policy_with_max_7_files_when_property_starts_with_time_colon() throws Exception { File logDir = temporaryFolder.newFolder(); String logPattern = randomAlphanumeric(15); String timePattern = randomAlphanumeric(6); @@ -166,7 +215,7 @@ public class Log4JPropertiesBuilderTest { } @Test - public void configureGlobalFileLog_sets_properties_for_time_rolling_policy_when_property_starts_with_time_colon_and_specified_max_number_of_files() throws IOException { + public void configureGlobalFileLog_sets_properties_for_time_rolling_policy_when_property_starts_with_time_colon_and_specified_max_number_of_files() throws Exception { File logDir = temporaryFolder.newFolder(); String logPattern = randomAlphanumeric(15); String timePattern = randomAlphanumeric(6); @@ -180,7 +229,7 @@ public class Log4JPropertiesBuilderTest { } @Test - public void configureGlobalFileLog_sets_properties_for_size_rolling_policy_with_max_7_files_when_property_starts_with_size_colon() throws IOException { + public void configureGlobalFileLog_sets_properties_for_size_rolling_policy_with_max_7_files_when_property_starts_with_size_colon() throws Exception { File logDir = temporaryFolder.newFolder(); String logPattern = randomAlphanumeric(15); String sizePattern = randomAlphanumeric(6); @@ -192,7 +241,7 @@ public class Log4JPropertiesBuilderTest { } @Test - public void configureGlobalFileLog_sets_properties_for_size_rolling_policy_when_property_starts_with_size_colon_and_specified_max_number_of_files() throws IOException { + public void configureGlobalFileLog_sets_properties_for_size_rolling_policy_when_property_starts_with_size_colon_and_specified_max_number_of_files() throws Exception { File logDir = temporaryFolder.newFolder(); String logPattern = randomAlphanumeric(15); String sizePattern = randomAlphanumeric(6); @@ -206,7 +255,7 @@ public class Log4JPropertiesBuilderTest { } @Test - public void configureGlobalFileLog_sets_properties_for_no_rolling_policy_when_property_is_none() throws IOException { + public void configureGlobalFileLog_sets_properties_for_no_rolling_policy_when_property_is_none() throws Exception { File logDir = temporaryFolder.newFolder(); String logPattern = randomAlphanumeric(15); Log4JPropertiesBuilder underTest = newLog4JPropertiesBuilder( -- 2.39.5