]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8798 fix error when sonar.log.maxFiles=0
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 12 Oct 2017 20:32:24 +0000 (22:32 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 13 Oct 2017 09:45:18 +0000 (11:45 +0200)
server/sonar-process/src/main/java/org/sonar/process/logging/Log4JPropertiesBuilder.java
server/sonar-process/src/test/java/org/sonar/process/logging/Log4JPropertiesBuilderTest.java

index 8e83756002cf70197b5a3354e9bd78b884d0ff7b..eccb7f5b3f50b6172b45e238677e2519cdc7d6aa 100644 (file)
@@ -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:"));
index 8df33a39273fea280d1e34e5f72e49be1e3f5e5c..4feda6aceaf96ec05cde2fdc073b5f6f0ebbe463 100644 (file)
@@ -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(