/** * Print the contents of the context status, but only if they contain * warnings or errors occurring later then the threshold. * * @param context */ public static void printInCaseOfErrorsOrWarnings(Context context, long threshold) { if (context == null) { throw new IllegalArgumentException("Context argument cannot be null"); } StatusManager sm = context.getStatusManager(); if (sm == null) { ps.println("WARN: Context named \"" + context.getName() + "\" has no status manager"); } else { StatusChecker sc = new StatusChecker(context); if (sc.getHighestLevel(threshold) >= ErrorStatus.WARN) { print(sm, threshold); } } }
public boolean hasXMLParsingErrors(long threshold) { return containsMatch(threshold, Status.ERROR, CoreConstants.XML_PARSING); }
private void performXMLConfiguration(LoggerContext lc) { JoranConfigurator jc = new JoranConfigurator(); jc.setContext(context); StatusChecker statusChecker = new StatusChecker(context); List<SaxEvent> eventList = jc.recallSafeConfiguration(); URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(context); lc.reset(); long threshold = System.currentTimeMillis(); try { jc.doConfigure(mainConfigurationURL); if (statusChecker.hasXMLParsingErrors(threshold)) { fallbackConfiguration(lc, eventList, mainURL); } } catch (JoranException e) { fallbackConfiguration(lc, eventList, mainURL); } }
private void checkResetCount(int expected) { StatusChecker checker = new StatusChecker(loggerContext); checker.assertIsErrorFree(); int effectiveResets = checker.matchCount(CoreConstants.RESET_MSG_PREFIX); assertEquals(expected, effectiveResets); // String failMsg = "effective=" + effectiveResets + ", expected=" + expected; // // there might be more effective resets than the expected amount // since the harness may be sleeping while a reset occurs //assertTrue(failMsg, expected <= effectiveResets && (expected + 2) >= effectiveResets); }
@Test public void lbcore193() throws JoranException { String configFileAsStr = ClassicTestConstants.ISSUES_PREFIX + "lbcore193.xml"; configure(configFileAsStr); checker.asssertContainsException(ScanException.class); checker.assertContainsMatch(Status.ERROR, "Expecting RIGHT_PARENTHESIS token but got null"); checker.assertContainsMatch(Status.ERROR, "See also " + Parser.MISSING_RIGHT_PARENTHESIS); }
public void immediateFlushInEncoder(Boolean immediateFlush) throws JoranException { loggerContext.putProperty("immediateFlush", immediateFlush.toString()); jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "compatibility/immediateFlushInEncoder.xml"); StatusPrinter.print(loggerContext); StatusChecker checker = new StatusChecker(loggerContext); checker.assertContainsMatch(Status.WARN, "As of version 1.2.0 \"immediateFlush\" property should be set within the enclosing Appender."); checker.assertContainsMatch(Status.WARN, "Please move \"immediateFlush\" property into the enclosing appender."); checker.assertContainsMatch(Status.WARN, "Setting the \"immediateFlush\" property of the enclosing appender to " + immediateFlush); ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) root.getAppender("LIOE"); assertTrue(fileAppender.isStarted()); assertEquals(immediateFlush.booleanValue(), fileAppender.isImmediateFlush()); } }
@Test public void test3() throws Exception { Compressor compressor = new Compressor(CompressionMode.ZIP); compressor.setContext(context); compressor.compress(CoreTestConstants.TEST_DIR_PREFIX + "input/compress3.txt", CoreTestConstants.OUTPUT_DIR_PREFIX + "compress3.txt", "compress3.txt"); StatusChecker checker = new StatusChecker(context); assertTrue(checker.isErrorFree(0)); // we don't know how to compare .zip files // assertTrue(Compare.compare(CoreTestConstants.OUTPUT_DIR_PREFIX // + "compress3.txt.zip", CoreTestConstants.TEST_DIR_PREFIX // + "witness/compress3.txt.zip")); }
@SuppressWarnings("deprecation") @Test public void encoderCharset() throws JoranException, IOException, InterruptedException { String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "encoderCharset.xml"; configure(configFileAsStr); ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) root.getAppender("CONSOLE"); assertNotNull(consoleAppender); LayoutWrappingEncoder<ILoggingEvent> encoder = (LayoutWrappingEncoder<ILoggingEvent>) consoleAppender.getEncoder(); assertEquals("UTF-8", encoder.getCharset().displayName()); StatusChecker checker = new StatusChecker(loggerContext); checker.assertIsErrorFree(); }
@Test public void collidingTimeformat() { rfa.setContext(context); rfa.setAppend(false); rfa.setPrudent(true); tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "toto-%d{dd}.log.zip"); tbrp.start(); rfa.setRollingPolicy(tbrp); rfa.start(); StatusChecker checker = new StatusChecker(context); assertFalse(rfa.isStarted()); assertEquals(Status.ERROR, checker.getHighestLevel(0)); StatusPrinter.print(context); checker.assertContainsMatch("The date format in FileNamePattern will result"); }
public boolean isErrorFree(long threshold) { return Status.ERROR > getHighestLevel(threshold); }
public final void doConfigure(final InputSource inputSource) throws JoranException { long threshold = System.currentTimeMillis(); if (!ConfigurationWatchListUtil.wasConfigurationWatchListReset(context)) { informContextOfURLUsedForConfiguration(getContext(), null); } SaxEventRecorder recorder = new SaxEventRecorder(); recorder.setContext(context); recorder.recordEvents(inputSource); doConfigure(recorder.saxEventList); // no exceptions a this level StatusChecker statusChecker = new StatusChecker(context); if (statusChecker.noXMLParsingErrorsOccurred(threshold)) { addInfo("Registering current configuration as safe fallback point"); registerSafeConfiguration(); } }
/** * Test for http://jira.qos.ch/browse/LOGBACK-796 */ @Test public void testFileShouldNotMatchFileNamePattern() { rfa.setContext(context); rfa.setFile(CoreTestConstants.OUTPUT_DIR_PREFIX + "x-2013-04.log"); tbrp.setFileNamePattern(CoreTestConstants.OUTPUT_DIR_PREFIX + "x-%d{yyyy-MM}.log"); tbrp.start(); rfa.setRollingPolicy(tbrp); rfa.start(); StatusChecker statusChecker = new StatusChecker(context); final String msg = "File property collides with fileNamePattern. Aborting."; boolean containsMatch = statusChecker.containsMatch(Status.ERROR, msg); assertTrue("Missing error: " + msg, containsMatch); }
@Test public void malformedURL() throws JoranException { System.setProperty(INCLUDE_KEY, "htp://logback.qos.ch"); tc.doConfigure(TOP_BY_URL); assertEquals(Status.ERROR, statusChecker.getHighestLevel(0)); assertTrue(statusChecker.containsException(MalformedURLException.class)); }
@Test(timeout = 3000L) public void fallbackToSafe_FollowedByRecovery() throws IOException, JoranException, InterruptedException { String path = CoreTestConstants.OUTPUT_DIR_PREFIX + "reconfigureOnChangeConfig_fallbackToSafe-" + diff + ".xml"; File topLevelFile = new File(path); writeToFile(topLevelFile, "<configuration debug=\"true\" scan=\"true\" scanPeriod=\"5 millisecond\"><root level=\"ERROR\"/></configuration> "); configure(topLevelFile); CountDownLatch changeDetectedLatch = waitForReconfigurationToBeDone(null); ReconfigureOnChangeTask oldRoct = getRegisteredReconfigureTask(); assertNotNull(oldRoct); writeToFile(topLevelFile, "<configuration debug=\"true\" scan=\"true\" scanPeriod=\"5 millisecond\">\n" + " <root></configuration>"); changeDetectedLatch.await(); statusChecker.assertContainsMatch(Status.WARN, FALLING_BACK_TO_SAFE_CONFIGURATION); statusChecker.assertContainsMatch(Status.INFO, RE_REGISTERING_PREVIOUS_SAFE_CONFIGURATION); loggerContext.getStatusManager().clear(); CountDownLatch secondDoneLatch = waitForReconfigurationToBeDone(oldRoct); writeToFile(topLevelFile, "<configuration debug=\"true\" scan=\"true\" scanPeriod=\"5 millisecond\"><root level=\"ERROR\"/></configuration> "); secondDoneLatch.await(); statusChecker.assertIsErrorFree(); statusChecker.containsMatch(DETECTED_CHANGE_IN_CONFIGURATION_FILES); }
@Test public void charset() { setter.setProperty("charset", "UTF-8"); assertEquals(Charset.forName("UTF-8"), house.getCharset()); house.setCharset(null); setter.setProperty("charset", "UTF"); assertNull(house.getCharset()); StatusChecker checker = new StatusChecker(context); checker.containsException(UnsupportedCharsetException.class); } }
@Test public void fileUnderCatalinaBaseShouldBeFound() throws LifecycleException { System.setProperty(LogbackValve.CATALINA_BASE_KEY, AccessTestConstants.JORAN_INPUT_PREFIX + "tomcat/"); final String fileName = "logback-access.xml"; setupValve(fileName); valve.start(); checker.assertContainsMatch("Found configuration file"); checker.assertContainsMatch("Done configuring"); checker.assertIsErrorFree(); }
@Test public void noClass() throws JoranException { simpleConfigurator.doConfigure(DEFINE_INPUT_DIR + NOCLASS_XML); String inContextFoo = context.getProperty("foo"); assertNull(inContextFoo); checker.assertContainsMatch(Status.ERROR, "Missing class name for property definer. Near \\[define\\] line 1"); }
public void assertIsErrorFree() { assertTrue(isErrorFree(0)); }
@Test public void properties() throws JoranException { String configFileAsStr = ClassicTestConstants.JORAN_INPUT_PREFIX + "properties.xml"; assertNull(loggerContext.getProperty(CoreConstants.HOSTNAME_KEY)); assertNull(System.getProperty("sys")); configure(configFileAsStr); // Not supported in Android due to Issue #3 //assertNotNull(loggerContext.getProperty(CoreConstants.HOSTNAME_KEY)); assertNull(loggerContext.getProperty("transientKey1")); assertNull(loggerContext.getProperty("transientKey2")); assertEquals("node0", loggerContext.getProperty("nodeId")); assertEquals("tem", System.getProperty("sys")); assertNotNull(loggerContext.getProperty("path")); checker.assertIsErrorFree(); }
public boolean noXMLParsingErrorsOccurred(long threshold) { return !hasXMLParsingErrors(threshold); }