@Override public void run() { getLoggerContext().stop(); }
@Test(timeout = 5000) public void shutdownTest() throws Exception { final LoggerContext ctx = (LoggerContext)LogManager.getContext(false); final Logger logger = ctx.getLogger("Logger"); logger.info("This is a test"); ctx.stop(); }
@Test public void testRandomAccessConfiguration() throws Exception { final Logger logger = this.init.getLogger("com.foo.Bar"); final String message = "This is a test log message brought to you by Slurm."; logger.info(message); this.init.getLoggerContext().stop(); // stop async thread String line; try (final BufferedReader reader = new BufferedReader(new FileReader(this.logFile))) { line = reader.readLine(); } assertNotNull(line); assertThat(line, containsString(message)); final Matcher<String> containsLocationInformation = containsString("testRandomAccessConfiguration"); final Matcher<String> containsLocationInformationIfEnabled = this.locationEnabled ? containsLocationInformation : not(containsLocationInformation); assertThat(line, containsLocationInformationIfEnabled); } }
@After public void tearDown() throws Exception { this.app.clear(); this.loggerContextRule.getLoggerContext().stop(); }
logger.info(logMsg); logger.error(logMsg, new IllegalArgumentException("badarg")); this.loggerContextRule.getLoggerContext().stop(); // stops async thread
@After public void tearDown() throws Exception { this.app.clear(); this.loggerContextRule.getLoggerContext().stop(); }
@After public void tearDown() throws Exception { this.app.clear(); this.loggerContextRule.getLoggerContext().stop(); }
if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) { System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this);
public void testNoNulCharacters(final String message, final String expected) throws IOException { @SuppressWarnings("resource") final LoggerContext loggerContext = loggerContextRule.getLoggerContext(); final Logger logger = loggerContext.getLogger("com.example"); logger.error("log:", message); loggerContext.stop(); final File file = new File(FILE_PATH); final byte[] contents = Files.toByteArray(file); int count0s = 0; final StringBuilder sb = new StringBuilder(); for (int i = 0; i < contents.length; i++) { final byte b = contents[i]; if (b == 0) { sb.append(i); sb.append(", "); count0s++; } } Assert.assertEquals("File contains " + count0s + " 0x00 byte at indices " + sb, 0, count0s); final List<String> readLines = Files.readLines(file, Charset.defaultCharset()); final String actual = readLines.get(0); // Assert.assertTrue(actual, actual.contains(message)); Assert.assertEquals(actual, expected, actual); Assert.assertEquals(1, readLines.size()); }
@Override public void stop() { super.stop(); // Clean up reference to avoid class loader leaks this.artifactDescriptor = null; }
if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) { System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this);
@Override public boolean stop(long timeout, TimeUnit timeUnit) { boolean result = super.stop(timeout, timeUnit); // Clean up reference to avoid class loader leaks this.artifactDescriptor = null; return result; } }
@Test public void testAdvertisementsRemovedOnConfigStop() { verifyExpectedEntriesAdvertised(InMemoryAdvertiser.getAdvertisedEntries()); final LoggerContext ctx = LoggerContext.getContext(); ctx.stop(); final Map<Object, Map<String, String>> entries = InMemoryAdvertiser.getAdvertisedEntries(); assertTrue("Entries found: " + entries, entries.isEmpty()); //reconfigure for subsequent testing ctx.start(); }
if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) { System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this);
if (!loggerContextRule.getLoggerContext().stop(30, TimeUnit.SECONDS)) { System.err.println("Could not stop cleanly " + loggerContextRule + " for " + this);
@Test public void testAdvertisementsAddedOnReconfigAfterStop() { verifyExpectedEntriesAdvertised(InMemoryAdvertiser.getAdvertisedEntries()); final LoggerContext ctx = LoggerContext.getContext(); ctx.stop(); final Map<Object, Map<String, String>> entries = InMemoryAdvertiser.getAdvertisedEntries(); assertTrue("Entries found: " + entries, entries.isEmpty()); ctx.start(); verifyExpectedEntriesAdvertised(InMemoryAdvertiser.getAdvertisedEntries()); } }
private void testSmtpAppender(Logger logger) { ThreadContext.put("MDC1", "mdc1"); logger.error("the message"); ctx.getLoggerContext().stop(); smtpServer.stop(); assertEquals(1, smtpServer.getReceivedEmailSize()); final Iterator<SmtpMessage> messages = smtpServer.getReceivedEmail(); final SmtpMessage email = messages.next(); assertEquals("to@example.com", email.getHeaderValue("To")); assertEquals("from@example.com", email.getHeaderValue("From")); assertEquals("[mdc1]", email.getHeaderValue("Subject")); String body = email.getBody(); if (!body.contains("Body:[mdc1]")) { fail(body); } }
private void stop(LoggerContext loggerContext) { if (loggerContext != null && !loggerContext.isStopping() && !loggerContext.isStopped()) { loggerContext.stop(); } }
@Override public synchronized boolean stop(final long timeout, final TimeUnit timeUnit) { if (!this.isStarted() && !this.isStopped()) { throw new IllegalStateException("Cannot stop this Log4jWebInitializer because it has not started."); } // only do this once if (this.isStarted()) { this.setStopping(); if (this.loggerContext != null) { LOGGER.debug("Removing LoggerContext for [{}].", this.name); this.servletContext.removeAttribute(CONTEXT_ATTRIBUTE); if (this.namedContextSelector != null) { this.namedContextSelector.removeContext(this.name); } this.loggerContext.stop(timeout, timeUnit); this.loggerContext.setExternalContext(null); this.loggerContext = null; } this.setStopped(); } return super.stop(timeout, timeUnit); }
/** * Flushes all appenders and terminate logging. */ private static void logFlush() { ((LoggerContext) LogManager.getContext()).stop(); }