@Test public void t04_TestTxtLogger() throws BasicIOException { final String filename1 = "test01.log"; final String filename2 = "test01.txt"; final LogFile logFile = new LogFile(_testLocation, filename1); Logger logger = new Logger(logFile); // The file should be created immediately: File file = _testLocation.getFile(filename1); if (file == null) { fail(); } // It should be locked: try { file.getOutputStream(); fail(); } catch (FileLockedException fle) { println(fle); } // The .txt file should NOT have been created: if (_testLocation.exists(filename2)) { fail(); } // Change output, should delete the file: logger.setOutput(System.out); if (_testLocation.exists(filename1)) { fail(); } // Again open the same file: logger.setOutput(logFile); assertEquals(file.getSize(), 0L); logger.info("first message"); long size = file.getSize(); assertTrue(size > 0); // Change to stdout and check the file is still there: logger.setOutput(System.out); if (!_testLocation.exists(filename1)) { fail(); } // We can now open it: ExtendedReader in = new ExtendedReader(file.getInputStream()); assertTrue(in.readLine().startsWith("INFO")); in.close(); // Ensure that if we disable the logger it does not write to the file: logger.setOutput(logFile); logger.setEnabled(false); logger.info("second message"); logger.setOutput(System.out); assertEquals(file.getSize(), size); file.delete(); // Print an exception to stdout: logger.setEnabled(true); logger.error("Oops!", new Exception()); // Print an exception to TXT: logger.setOutput(logFile); logger.error("Oops!", new Exception()); logger.setOutput(System.out); file.delete(); }
@Test public void t05_TestCsvLogger() throws BasicIOException { final String filename1 = "test01.csv"; final String filename2 = "test01.txt"; final LogFile logFile = new LogFile(_testLocation, filename1); Logger logger = new Logger(logFile); // The file should be created immediately: File file = _testLocation.getFile(filename1); if (file == null) { fail(); } // It should be locked: try { file.getOutputStream(); fail(); } catch (FileLockedException fle) { println(fle); } // Get the initial file size (it has the header): long initialSize = file.getSize(); // The .txt file should also have been created: if (!_testLocation.exists(filename2)) { fail(); } // Change output, should delete both files: logger.setOutput(System.out); if (_testLocation.exists(filename1)) { fail(); } if (_testLocation.exists(filename2)) { fail(); } // Again open the same file: logger.setOutput(logFile); assertEquals(file.getSize(), initialSize); logger.info("first message"); long size = file.getSize(); assertTrue(size > initialSize); // Change to stdout and check the file is still there: logger.setOutput(System.out); if (!_testLocation.exists(filename1)) { fail(); } // ... and the TXT file is not there: if (_testLocation.exists(filename2)) { fail(); } // We can now open it: ExtendedReader in = new ExtendedReader(file.getInputStream()); assertTrue(in.readLine().startsWith("Timestamp")); String s = in.readLine(); s = s.substring(s.indexOf(",") + 1); assertTrue(s.startsWith("INFO")); assertTrue(s.endsWith(",")); in.close(); // Ensure that if we disable the logger it does not write to the file: logger.setOutput(new LogFile(_testLocation, filename1)); logger.setEnabled(false); logger.info("second message"); logger.setOutput(System.out); assertEquals(file.getSize(), size); // Write an error and ensure the ex file remains there: logger.setOutput(logFile); logger.setEnabled(true); logger.error("an error", getException(5)); logger.setOutput(System.out); if (!_testLocation.exists(filename1)) { fail(); } if (!_testLocation.exists(filename2)) { fail(); } _testLocation.getFile(filename1).delete(); _testLocation.getFile(filename2).delete(); }