# HG changeset patch # User Andrea Gualano # Date 1334144090 -7200 # Branch release71_fixes # Node ID fd18ee36e8039db05826b5e7326dcd433f3b30c1 # Parent a903faa1d680d27d8d991defd10971504d0e8e48 issue #209786: Allow configuring the number of log files in the var/log directory diff -r a903faa1d680 -r fd18ee36e803 core.startup/arch.xml --- a/core.startup/arch.xml Thu Mar 01 21:52:54 2012 +0100 +++ b/core.startup/arch.xml Wed Apr 11 13:34:50 2012 +0200 @@ -602,6 +602,12 @@ Allows unit tests to prevent changes to System.err and System.out by the logging infrastructure. + + Changes the number of files used for log rotation. If started with + -J-Dorg.netbeans.log.numberOfFiles=number then the log files for + number-1 previous executions will be preserved. Default and + minimum number is 3. + Disables #113341 so that the first version of a module JAR to be encountered is loaded, even if a later cluster contains a newer version of the same module. diff -r a903faa1d680 -r fd18ee36e803 core.startup/src/org/netbeans/core/startup/TopLogging.java --- a/core.startup/src/org/netbeans/core/startup/TopLogging.java Thu Mar 01 21:52:54 2012 +0100 +++ b/core.startup/src/org/netbeans/core/startup/TopLogging.java Wed Apr 11 13:34:50 2012 +0200 @@ -406,21 +406,27 @@ try { File dir = new File(new File(home, "var"), "log"); dir.mkdirs (); - File f = new File(dir, "messages.log"); - File f1 = new File(dir, "messages.log.1"); - File f2 = new File(dir, "messages.log.2"); - if (f2.exists()) { - f2.delete(); + int n = Integer.getInteger("org.netbeans.log.numberOfFiles", 3); // NOI18N + if (n < 3) { + n = 3; } - if (f1.exists()) { - f1.renameTo(f2); - } - if (f.exists()) { - f.renameTo(f1); + File[] f = new File[n]; + f[0] = new File(dir, "messages.log"); + for (int i = 1; i < n; i++) { + f[i] = new File(dir, "messages.log." + i); } - FileOutputStream fout = new FileOutputStream(f, false); + if (f[n - 1].exists()) { + f[n - 1].delete(); + } + for (int i = n - 2; i >= 0; i--) { + if (f[i].exists()) { + f[i].renameTo(f[i + 1]); + } + } + + FileOutputStream fout = new FileOutputStream(f[0], false); Handler h = new StreamHandler(fout, NbFormatter.FORMATTER); h.setLevel(Level.ALL); h.setFormatter(NbFormatter.FORMATTER); diff -r a903faa1d680 -r fd18ee36e803 core.startup/test/unit/src/org/netbeans/core/startup/TopLoggingTest.java --- a/core.startup/test/unit/src/org/netbeans/core/startup/TopLoggingTest.java Thu Mar 01 21:52:54 2012 +0100 +++ b/core.startup/test/unit/src/org/netbeans/core/startup/TopLoggingTest.java Wed Apr 11 13:34:50 2012 +0200 @@ -300,6 +300,82 @@ } + public void testFileRotationByDefault() throws Exception { + for (int i = 0; i < 5; i++) { + // simulate shutdown + TopLogging.flush(false); + TopLogging.close(); + // simulate restart + TopLogging.flush(true); + TopLogging.initialize(); + } + + File log = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log"); + assertTrue("Log file exists: " + log, log.canRead()); + + File log1 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.1"); + assertTrue("Backup file exists: " + log1, log1.canRead()); + + File log2 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.2"); + assertTrue("Backup file exists: " + log2, log2.canRead()); + + File log3 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.3"); + assertFalse("Backup file does not exist: " + log3, log3.canRead()); + } + + public void testFileRotationWithSystemProperty() throws Exception { + for (int i = 0; i < 6; i++) { + // simulate shutdown + TopLogging.flush(false); + TopLogging.close(); + // set system property + System.setProperty("org.netbeans.log.numberOfFiles", "4"); + // simulate restart + TopLogging.flush(true); + TopLogging.initialize(); + } + + File log = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log"); + assertTrue("Log file exists: " + log, log.canRead()); + + File log1 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.1"); + assertTrue("Backup file exists: " + log1, log1.canRead()); + + File log2 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.2"); + assertTrue("Backup file exists: " + log2, log2.canRead()); + + File log3 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.3"); + assertTrue("Backup file exists: " + log3, log3.canRead()); + + File log4 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.4"); + assertFalse("Backup file does not exist: " + log4, log4.canRead()); + } + + public void testFileRotationAtLeastThreeFiles() throws Exception { + for (int i = 0; i < 5; i++) { + // simulate shutdown + TopLogging.flush(false); + TopLogging.close(); + // set system property + System.setProperty("org.netbeans.log.numberOfFiles", "2"); + // simulate restart + TopLogging.flush(true); + TopLogging.initialize(); + } + + File log = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log"); + assertTrue("Log file exists: " + log, log.canRead()); + + File log1 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.1"); + assertTrue("Backup file exists: " + log1, log1.canRead()); + + File log2 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.2"); + assertTrue("Backup file exists: " + log2, log2.canRead()); + + File log3 = new File(new File(new File(getWorkDir(), "var"), "log"), "messages.log.3"); + assertFalse("Backup file does not exist: " + log3, log3.canRead()); + } + public void testCanInfluenceBehaviourBySettingALevelProperty() throws Exception { System.setProperty(TopLoggingTest.class.getName() + ".level", "100"); LogManager.getLogManager().readConfiguration();