public synchronized void logRotate(boolean isStartup) throws IOException { if (!isStartup) { closeLogFile(); super.close(); setPrintStream(null); } rotationAlgo.rotate(); openLogFile(); }
public synchronized LogEvent log (LogEvent ev) { if (msgCount++ > CHECK_INTERVAL) { checkSize(); msgCount = 0; } return super.log (ev); } protected synchronized void openLogFile() throws IOException {
public void destroy () { if (rotate != null) rotate.cancel(); try { closeLogFile (); } catch (IOException e) { logDebug(e.getMessage()); } }
private void runPostConfiguration() throws ConfigurationException { try { if (rotateOnStartup) { logRotate(rotateOnStartup); } else { openLogFile(); } } catch (IOException e) { throw new ConfigurationException (e); } timer.schedule(); } }
private RotateLogListener createRotateLogListenerWithIsoDateFormat( String logFileName, Properties customConfig) throws ConfigurationException { RotateLogListener listener = new RotateLogListener(); Properties configuration = new Properties(); configuration.setProperty("file", logRotationTestDirectory.getDirectory().getAbsolutePath() + "/" + logFileName); configuration.setProperty("copies", "10"); configuration.setProperty("maxsize", "1000000"); if (customConfig != null) { configuration.putAll(customConfig); } logRotationTestDirectory.allowNewFileCreation(); listener.setConfiguration(new SimpleConfiguration(configuration)); return listener; }
@Test @Ignore("This feature doesn't work in Windows so we reverted the patch c94ff02f2") public void testLogRotateAbortsWhenCreatingNewFileFails() throws Exception { String logFileName = "RotateAbortsTestLog"; RotateLogListener listener = createRotateLogListenerWithIsoDateFormat(logFileName, null); listener.log(new LogEvent("Message 1")); // when: a rotation is required but a new file cannot be created logRotationTestDirectory.preventNewFileCreation(); listener.logRotate(); // then: no error should escape and the existing log file should continue being written to listener.log(new LogEvent("Message 2")); logRotationTestDirectory.allowNewFileCreation(); String logFileContents = getStringFromFile(logRotationTestDirectory.getFile(logFileName)); System.out.println("logFileContents = " + logFileContents); assertTrue("Log file should contain first message", logFileContents.contains("Message 1")); assertTrue("Log file should contain second message", logFileContents.contains("Message 2")); assertFalse("Logger element should not have been closed", logFileContents.contains("</logger>")); File archiveFile = logRotationTestDirectory.getFile(logFileName + ".1"); assertFalse("Archive file should not exist", archiveFile.exists()); }
@Test public void testEnvironmentCodeParsing() { Map<String, String> env = System.getenv(); RotateLogListener listener = new RotateLogListener(); Map.Entry<String, String> entry = env.entrySet().iterator().next(); String replaced = listener.fileNameFromPattern("%s-log", "e{" + entry.getKey() + "}"); assertEquals(entry.getValue() + "-log", replaced); }
@Test @Ignore ("test causes problems, closes stdout") public void testCloseLogFile() throws Throwable { RotateLogListener rotateLogListener = new RotateLogListener(); rotateLogListener.closeLogFile(); assertNull("rotateLogListener.f", rotateLogListener.f); }
logName = fileNameFromPattern(logName, fileNamePattern); runPostConfiguration();
@Test @Ignore ("test causes problems, closes stdout") public void testLogDebug1() throws Throwable { RotateLogListener dailyLogListener = new DailyLogListener(); dailyLogListener.close(); dailyLogListener.logDebug("testRotateLogListenerMsg"); assertNull("(DailyLogListener) dailyLogListener.p", ((DailyLogListener) dailyLogListener).p); }
@Test public void testCheckSizeThrowsNullPointerException() throws Throwable { RotateLogListener rotateLogListener = new RotateLogListener(); try { rotateLogListener.openLogFile(); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { assertNull("ex.getMessage()", ex.getMessage()); assertNull("rotateLogListener.f", rotateLogListener.f); assertNotNull("rotateLogListener.p", rotateLogListener.p); } }
@Test @Ignore ("test causes problems, closes stdout") public void testLogRotateThrowsNullPointerException() throws Throwable { RotateLogListener rotateLogListener = new RotateLogListener(); try { rotateLogListener.logRotate(); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { assertNull("rotateLogListener.f", rotateLogListener.f); assertNull("rotateLogListener.p", rotateLogListener.p); } }
@Test @Ignore ("test causes problems, closes stdout") public void testLog1() throws Throwable { RotateLogListener dailyLogListener = new DailyLogListener(); dailyLogListener.close(); LogEvent result = dailyLogListener.log(null); assertNull("result", result); assertNull("(DailyLogListener) dailyLogListener.p", ((DailyLogListener) dailyLogListener).p); assertEquals("(DailyLogListener) dailyLogListener.msgCount", 1, ((DailyLogListener) dailyLogListener).msgCount); }
public void logRotate () throws IOException { logRotate(false); }
}; super.setConfiguration(cfg);
@Test public void testLogDebug() throws Throwable { RotateLogListener dailyLogListener = new DailyLogListener(); dailyLogListener.logDebug("testRotateLogListenerMsg"); assertNotNull("(DailyLogListener) dailyLogListener.p", ((DailyLogListener) dailyLogListener).p); }
@Test public void testOpenLogFileThrowsNullPointerException() throws Throwable { RotateLogListener dailyLogListener = new DailyLogListener(); try { dailyLogListener.openLogFile(); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { assertNull("(DailyLogListener) dailyLogListener.f", ((DailyLogListener) dailyLogListener).f); assertNotNull("(DailyLogListener) dailyLogListener.p", ((DailyLogListener) dailyLogListener).p); } }
@Test public void testRotateConstructor() throws Throwable { new RotateLogListener().new Rotate(); assertTrue("Test completed without Exception", true); }
@Test public void testDestroy() throws Throwable { RotateLogListener dailyLogListener = new DailyLogListener(); dailyLogListener.destroy(); assertNull("(DailyLogListener) dailyLogListener.f", ((DailyLogListener) dailyLogListener).f); }