void initRFA(RollingFileAppender<Object> rfa, String filename) { rfa.setContext(context); rfa.setEncoder(encoder); if (filename != null) { rfa.setFile(filename); } }
public void setUpTimeBasedTriggeringPolicy(RollingFileAppender<Object> rfa) { String datePattern = "yyyy-MM-dd'T'HH_mm_ss_SSS"; TimeBasedRollingPolicy tbrp = new TimeBasedRollingPolicy(); tbrp.setFileNamePattern(outputDirStr + "test-%d{" + datePattern + "}"); tbrp.setContext(context); tbrp.setParent(rfa); tbrp.start(); rfa.setRollingPolicy(tbrp); rfa.start(); }
@Test public void noCompression_FileSet_StopRestart_WithLongWait_4B() throws Exception { String testId = "test4B"; initRFA(rfa1, testId2FileName(testId)); initTRBP( rfa1, tbrp1, randomOutputDir + testId + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}", currentTime); addExpectedFileName_ByDate(randomOutputDir, testId, getDateOfCurrentPeriodsStart(), false); incCurrentTime(1100); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); for (int i = 0; i <= 2; i++) { rfa1.doAppend("Hello---" + i); addExpectedFileNamedIfItsTime_ByDate(randomOutputDir, testId, false); incCurrentTime(500); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); } rfa1.stop(); // change the timestamp of the currently actively file File activeFile = new File(rfa1.getFile()); activeFile.setLastModified(currentTime); incCurrentTime(2000); initRFA(rfa2, randomOutputDir + "test4B.log"); initTRBP( rfa2, tbrp2, randomOutputDir + testId + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}", currentTime); for (int i = 0; i <= 2; i++) { rfa2.doAppend("World---" + i); addExpectedFileNamedIfItsTime_ByDate(randomOutputDir, testId, false); incCurrentTime(100); tbrp2.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); } massageExpectedFilesToCorresponToCurrentTarget("test4B.log"); int i = 0; for (String fn : expectedFilenameList) { assertTrue( Compare.compare( fn, CoreTestConstants.TEST_SRC_PREFIX + "witness/rolling/tbr-test4B." + i++)); } }
void initTRBP( RollingFileAppender<Object> rfa, TimeBasedRollingPolicy<Object> tbrp, String filenamePattern, long givenTime) { tbrp.setContext(context); tbrp.setFileNamePattern(filenamePattern); tbrp.setParent(rfa); tbrp.timeBasedFileNamingAndTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<Object>(); tbrp.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(givenTime); rfa.setRollingPolicy(tbrp); tbrp.start(); rfa.start(); }
@Before public void setUp() throws Exception { encoder = new EchoEncoder<Object>(); File outputDir = new File(outputDirStr); outputDir.mkdirs(); System.out.println("Output dir [" + outputDirStr + "]"); scriptOS = openScript(); rfa.setName("rolling"); rfa.setEncoder(encoder); rfa.setContext(context); rfa.setFile(outputDirStr + "output.log"); }
/** Test rolling without compression, file option left blank, no stop/start */ @Test public void noCompression_FileBlank_NoRestart_1() throws Exception { String testId = "test1"; initRFA(rfa1, null); initTRBP( rfa1, tbrp1, randomOutputDir + testId + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}", currentTime); // compute the current filename addExpectedFileName_ByDate(randomOutputDir, testId, getDateOfCurrentPeriodsStart(), false); incCurrentTime(1100); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); for (int i = 0; i < 3; i++) { rfa1.doAppend("Hello---" + i); addExpectedFileNamedIfItsTime_ByDate(randomOutputDir, testId, false); incCurrentTime(500); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); } int i = 0; for (String fn : expectedFilenameList) { assertTrue( Compare.compare( fn, CoreTestConstants.TEST_SRC_PREFIX + "witness/rolling/tbr-" + testId + "." + i++)); } }
void genericTest( String testId, String patternPrefix, String compressionSuffix, boolean fileOptionIsSet, int waitDuration) throws IOException { boolean withCompression = compressionSuffix.length() > 0; String fileName = fileOptionIsSet ? testId2FileName(testId) : null; initRFA(rfa1, fileName); String fileNamePatternStr = randomOutputDir + patternPrefix + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}" + compressionSuffix; initTRBP(rfa1, tbrp1, fileNamePatternStr, currentTime); // compute the current filename addExpectedFileName_ByDate(fileNamePatternStr, getMillisOfCurrentPeriodsStart()); incCurrentTime(1100); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); for (int i = 0; i < 3; i++) { rfa1.doAppend("Hello---" + i); addExpectedFileNamedIfItsTime_ByDate(fileNamePatternStr); incCurrentTime(500); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); if (withCompression) waitForCompression(tbrp1); } rfa1.stop(); if (waitDuration != NO_RESTART) { doRestart(testId, patternPrefix, fileOptionIsSet, waitDuration); } massageExpectedFilesToCorresponToCurrentTarget(fileName, fileOptionIsSet); StatusPrinter.print(context); rolloverChecker.check(expectedFilenameList); }
public void setUpSizeBasedTriggeringPolicy(RollingFileAppender<Object> rfa) { SizeBasedTriggeringPolicy<Object> zbtp = new SizeBasedTriggeringPolicy<Object>(); zbtp.setContext(context); zbtp.setMaxFileSize("100KB"); zbtp.start(); rfa.setTriggeringPolicy(zbtp); FixedWindowRollingPolicy fwrp = new FixedWindowRollingPolicy(); fwrp.setContext(context); fwrp.setFileNamePattern(outputDirStr + "test-%i.log"); fwrp.setMaxIndex(20); fwrp.setMinIndex(0); fwrp.setParent(rfa); fwrp.start(); rfa.setRollingPolicy(fwrp); rfa.start(); }
void doRestart(String testId, String patternPart, boolean fileOptionIsSet, int waitDuration) { // change the timestamp of the currently actively file File activeFile = new File(rfa1.getFile()); activeFile.setLastModified(currentTime); incCurrentTime(waitDuration); String filePatternStr = randomOutputDir + patternPart + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}"; String fileName = fileOptionIsSet ? testId2FileName(testId) : null; initRFA(rfa2, fileName); initTRBP(rfa2, tbrp2, filePatternStr, currentTime); for (int i = 0; i < 3; i++) { rfa2.doAppend("World---" + i); addExpectedFileNamedIfItsTime_ByDate(filePatternStr); incCurrentTime(100); tbrp2.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); } rfa2.stop(); }
public void run() { while (!isDone()) { counter++; rfa.doAppend(id + " " + counter); if ((counter % 64 == 0) && withInducedDelay) { try { Thread.sleep(10); } catch (InterruptedException e) { } } } }
/** With compression, file option set, no stop/restart, */ @Test public void withCompression_FileSet_NoRestart_6() throws Exception { String testId = "test6"; initRFA(rfa1, testId2FileName(testId)); initTRBP( rfa1, tbrp1, randomOutputDir + testId + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}.gz", currentTime); addExpectedFileName_ByDate(randomOutputDir, testId, getDateOfCurrentPeriodsStart(), true); incCurrentTime(1100); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); for (int i = 0; i < 3; i++) { rfa1.doAppend("Hello---" + i); addExpectedFileNamedIfItsTime_ByDate(randomOutputDir, testId, true); incCurrentTime(500); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); waitForCompression(tbrp1); } massageExpectedFilesToCorresponToCurrentTarget("test6.log"); int i = 0; for (String fn : expectedFilenameList) { assertTrue( Compare.compare( fn, CoreTestConstants.TEST_SRC_PREFIX + "witness/rolling/tbr-" + testId + "." + i + addGZIfNotLast(i))); i++; } }
@Test public void withMissingTargetDir() throws Exception { String testId = "missingTargetDir"; initRFA(rfa1, testId2FileName(testId)); int secondDiff = RandomUtil.getPositiveInt(); String randomTargetDir = CoreTestConstants.OUTPUT_DIR_PREFIX + secondDiff + '/'; System.out.println("randomOutputDir" + randomOutputDir); System.out.println("randomTargetDir" + randomTargetDir); initTRBP( rfa1, tbrp1, randomTargetDir + testId + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}", currentTime); addExpectedFileName_ByDate(randomTargetDir, testId, getDateOfCurrentPeriodsStart(), false); incCurrentTime(1100); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); for (int i = 0; i < 3; i++) { rfa1.doAppend("Hello---" + i); addExpectedFileNamedIfItsTime_ByDate(randomTargetDir, testId, false); incCurrentTime(500); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); } massageExpectedFilesToCorresponToCurrentTarget("missingTargetDir.log"); int i = 0; for (String fn : expectedFilenameList) { System.out.println("expectedFile=" + fn); assertTrue( Compare.compare( fn, CoreTestConstants.TEST_SRC_PREFIX + "witness/rolling/tbr-test5." + i++)); } }
/** With compression, file option left blank, no stop/restart */ @Test public void withCompression_FileBlank_NoRestart_2() throws Exception { String testId = "test2"; initRFA(rfa1, null); initTRBP( rfa1, tbrp1, randomOutputDir + testId + "-%d{" + DATE_PATTERN_WITH_SECONDS + "}.gz", currentTime); addExpectedFileName_ByDate(randomOutputDir, testId, getDateOfCurrentPeriodsStart(), true); incCurrentTime(1100); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); for (int i = 0; i < 3; i++) { // when i == 2, file name should not have .gz extension addExpectedFileNamedIfItsTime_ByDate(randomOutputDir, testId, i != 2); rfa1.doAppend("Hello---" + i); incCurrentTime(500); tbrp1.timeBasedFileNamingAndTriggeringPolicy.setCurrentTime(currentTime); waitForCompression(tbrp1); } int i = 0; for (String fn : expectedFilenameList) { assertTrue( Compare.compare( fn, CoreTestConstants.TEST_SRC_PREFIX + "witness/rolling/tbr-" + testId + "." + i + addGZIfNotLast(i))); i++; } }
private static void configureLogging(final Level consoleLevel, final Level fileLevel) { final LoggerContext loggerContext = LOG.getLoggerContext(); loggerContext.reset(); // set root log to the most permissive filter (affects performance big time (JAVACLI-90)) final int lowestLogLevel = Math.min(consoleLevel.toInt(), fileLevel.toInt()); LOG.setLevel(Level.toLevel(lowestLogLevel)); if (!consoleLevel.equals(Level.OFF)) { // create and add console appender final PatternLayoutEncoder consoleEncoder = new PatternLayoutEncoder(); consoleEncoder.setContext(loggerContext); consoleEncoder.setPattern(LOG_FORMAT_PATTERN); consoleEncoder.start(); final ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<ILoggingEvent>(); consoleAppender.setContext(loggerContext); consoleAppender.setName("STDOUT"); consoleAppender.setEncoder(consoleEncoder); final ThresholdFilter consoleFilter = new ThresholdFilter(); consoleFilter.setLevel(consoleLevel.levelStr); consoleFilter.setName(consoleLevel.levelStr); consoleFilter.start(); consoleAppender.addFilter(consoleFilter); consoleAppender.start(); LOG.addAppender(consoleAppender); } if (!fileLevel.equals(Level.OFF)) { // create file appender only if needed. // if done in the xml, it will create an empty file final RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); final FixedWindowRollingPolicy sizeBasedRollingPolicy = new FixedWindowRollingPolicy(); final SizeBasedTriggeringPolicy<Object> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<Object>(); fileAppender.setContext(loggerContext); sizeBasedTriggeringPolicy.setContext(loggerContext); sizeBasedRollingPolicy.setContext(loggerContext); fileAppender.setRollingPolicy(sizeBasedRollingPolicy); sizeBasedRollingPolicy.setParent(fileAppender); sizeBasedRollingPolicy.setMinIndex(0); sizeBasedRollingPolicy.setMaxIndex(99); final Path logFilePath = FileSystems.getDefault().getPath(LOG_DIR, LOG_FILE_NAME); fileAppender.setFile(logFilePath.toString()); sizeBasedRollingPolicy.setFileNamePattern(LOG_DIR + LOG_ARCHIVE_FILE_PATTERN); sizeBasedRollingPolicy.start(); sizeBasedTriggeringPolicy.setMaxFileSize("10MB"); sizeBasedTriggeringPolicy.start(); final PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder(); fileEncoder.setContext(loggerContext); fileEncoder.setPattern(LOG_FORMAT_PATTERN); fileEncoder.start(); fileAppender.setTriggeringPolicy((TriggeringPolicy) sizeBasedTriggeringPolicy); fileAppender.setRollingPolicy(sizeBasedRollingPolicy); fileAppender.setEncoder(fileEncoder); fileAppender.setName("LOGFILE"); sizeBasedRollingPolicy.start(); final ThresholdFilter fileFilter = new ThresholdFilter(); fileFilter.setLevel(fileLevel.levelStr); fileFilter.setName(fileLevel.levelStr); fileFilter.start(); fileAppender.addFilter(fileFilter); LOG.addAppender((Appender) fileAppender); fileAppender.start(); } }
@After public void tearDown() throws Exception { rfa.stop(); }