public static void stopLoggerContext(final boolean currentContext, final File checkFilePresence) throws InterruptedException { stopLoggerContext(currentContext); sleepAndCheck(checkFilePresence); }
@Override public void shutdown() { CoreLoggerContexts.stopLoggerContext(); // stop async thread }
@Test public void validateXmlSchemaSimple() throws Exception { final File file = new File("target", "XmlCompactFileAsyncAppenderValidationTest.log.xml"); file.delete(); final Logger log = LogManager.getLogger("com.foo.Bar"); log.warn("Message 1"); log.info("Message 2"); log.debug("Message 3"); CoreLoggerContexts.stopLoggerContext(file); // stop async thread this.validateXmlSchema(file); }
@Test public void validateXmlSchemaNoEvents() throws Exception { final File file = new File("target", "XmlCompactFileAsyncAppenderValidationTest.log.xml"); file.delete(); CoreLoggerContexts.stopLoggerContext(file); // stop async thread this.validateXmlSchema(file); }
public static void stopLoggerContext(final File checkFilePresence) throws InterruptedException { stopLoggerContext(); sleepAndCheck(checkFilePresence); }
@Test public void testCustomAsyncSelectorLocation() throws Exception { final File file = new File("target", "AsyncLoggerCustomSelectorLocationTest.log"); final Logger log = LogManager.getLogger("com.foo.Bar"); final Logger logIncludingLocation = LogManager.getLogger("com.include.location.Bar"); final String msg = "Async logger msg with location"; log.info(msg); logIncludingLocation.info(msg); CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String firstLine = reader.readLine(); final String secondLine = reader.readLine(); final String thirdLine = reader.readLine(); reader.close(); file.delete(); // By default we expect location to be disabled assertThat(firstLine, containsString(msg)); assertThat(firstLine, not(containsString("testCustomAsyncSelectorLocation"))); // Configuration allows us to retain location assertThat(secondLine, containsString(msg)); assertThat(secondLine, containsString("testCustomAsyncSelectorLocation")); assertThat(thirdLine, nullValue()); }
@Test public void testFlushAtEndOfBatch() throws Exception { final File file = new File("target", "AsyncLoggerConfigAutoFlushTest.log"); assertTrue("Deleted old file before test", !file.exists() || file.delete()); final Logger log = LogManager.getLogger("com.foo.Bar"); final String msg = "Message flushed with immediate flush=false"; log.info(msg); CoreLoggerContexts.stopLoggerContext(file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); reader.close(); file.delete(); assertNotNull("line1", line1); assertTrue("line1 correct", line1.contains(msg)); } }
@Test public void testNoErrorIfLogAfterShutdown() throws Exception { final Logger log = LogManager.getLogger("com.foo.Bar"); final String msg = "Async logger msg"; log.info(msg, new InternalError("this is not a real error")); CoreLoggerContexts.stopLoggerContext(); // stop async thread // call the #logMessage() method to bypass the isEnabled check: // before the LOG4J2-639 fix this would throw a NPE ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, new SimpleMessage("msg"), null); }
@Test public void testParametersAreAvailableToLayout() throws Exception { final File file = new File("target", "AsyncLoggerConfigTest4.log"); assertTrue("Deleted old file before test", !file.exists() || file.delete()); final Logger log = LogManager.getLogger("com.foo.Bar"); String format = "Additive logging: {} for the price of {}!"; log.info(format, 2, 1); CoreLoggerContexts.stopLoggerContext(file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); final String line2 = reader.readLine(); reader.close(); file.delete(); String expected = "Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!"; assertThat(line1, containsString(expected)); assertThat(line2, containsString(expected)); } }
@Test public void testAsyncLogWritesToLog() throws Exception { final File file = new File("target", "AsyncLoggerLocationTest.log"); // System.out.println(f.getAbsolutePath()); final Logger log = LogManager.getLogger("com.foo.Bar"); final String msg = "Async logger msg with location"; log.info(msg); CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); reader.close(); file.delete(); assertNotNull("line1", line1); assertTrue("line1 correct", line1.contains(msg)); final String location = "testAsyncLogWritesToLog"; assertTrue("has location", line1.contains(location)); }
@Test public void testError() throws Exception { final File file = new File("target", "AsyncLoggerConfigErrorOnFormat.log"); assertTrue("Deleted old file before test", !file.exists() || file.delete()); final Logger log = LogManager.getLogger("com.foo.Bar"); log.info(new ThrowsErrorOnFormatMessage()); log.info("Second message"); CoreLoggerContexts.stopLoggerContext(file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); final String line2 = reader.readLine(); reader.close(); file.delete(); assertThat(line1, containsString("Second message")); assertNull("Expected only one line", line2); }
@Test public void testNoErrorIfLogAfterShutdown() throws Exception { final Logger log = LogManager.getLogger("com.foo.Bar"); log.info("some message"); CoreLoggerContexts.stopLoggerContext(); // stop async thread // call the #logMessage() method to bypass the isEnabled check: // before the LOG4J2-639 fix this would throw a NPE ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, new SimpleMessage("msg"), null); } }
@Test public void testFlushAtEndOfBatch() throws Exception { final File file = new File("target", "XmlFileAppenderTest.log"); // System.out.println(f.getAbsolutePath()); file.delete(); final Logger log = LogManager.getLogger("com.foo.Bar"); final String logMsg = "Message flushed with immediate flush=false"; log.info(logMsg); CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread List<String> lines = Files.readAllLines(file.toPath(), Charset.forName("UTF8")); file.delete(); String[] expect = { "", // ? unsure why initial empty line... "<Event ", // "<Instant epochSecond=", // logMsg, // "</Event>", // }; for (int i = 0; i < expect.length; i++) { assertTrue("Expected line " + i + " to contain " + expect[i] + " but got: " + lines.get(i), lines.get(i).contains(expect[i])); } final String location = "testFlushAtEndOfBatch"; assertTrue("no location", !lines.get(0).contains(location)); } }
@Test public void testAsyncLogWritesToLog() throws Exception { final File file = new File("target", "AsyncLoggerTimestampMessageTest.log"); // System.out.println(f.getAbsolutePath()); file.delete(); final Logger log = LogManager.getLogger("com.foo.Bar"); assertFalse(PoisonClock.called); log.info((Message) new TimeMsg("Async logger msg with embedded timestamp", 123456789000L)); assertFalse(PoisonClock.called); CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); reader.close(); file.delete(); assertNotNull(line1); assertTrue("line1 correct", line1.equals("123456789000 Async logger msg with embedded timestamp")); }
@Test public void testAsyncLogUsesCurrentThreadName() throws Exception { final File file = new File("target", "AsyncLoggerTest.log"); // System.out.println(f.getAbsolutePath()); file.delete(); final Logger log = LogManager.getLogger("com.foo.Bar"); final String msg = "Async logger msg"; log.info(msg); Thread.currentThread().setName("MODIFIED-THREADNAME"); log.info(msg); CoreLoggerContexts.stopLoggerContext(file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); final String line2 = reader.readLine(); // System.out.println(line1); // System.out.println(line2); reader.close(); file.delete(); assertNotNull("line1", line1); assertNotNull("line2", line2); assertTrue("line1", line1.endsWith(" INFO c.f.Bar [main] Async logger msg ")); assertTrue("line2", line2.endsWith(" INFO c.f.Bar [MODIFIED-THREADNAME] Async logger msg ")); }
@Test public void testAsyncLogUsesCachedThreadName() throws Exception { final File file = new File("target", "AsyncLoggerTest.log"); // System.out.println(f.getAbsolutePath()); file.delete(); final Logger log = LogManager.getLogger("com.foo.Bar"); final String msg = "Async logger msg"; log.info(msg); Thread.currentThread().setName("MODIFIED-THREADNAME"); log.info(msg); CoreLoggerContexts.stopLoggerContext(file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); final String line2 = reader.readLine(); // System.out.println(line1); // System.out.println(line2); reader.close(); file.delete(); assertNotNull("line1", line1); assertNotNull("line2", line2); assertTrue("line1", line1.endsWith(" INFO c.f.Bar [main] Async logger msg ")); assertTrue("line2", line2.endsWith(" INFO c.f.Bar [main] Async logger msg ")); }
@Test public void testAsyncLogWritesToLog() throws Exception { final File file = new File("target", "AsyncLoggerTest.log"); // System.out.println(f.getAbsolutePath()); file.delete(); ThreadContext.push("stackvalue"); ThreadContext.put("KEY", "mapvalue"); final Logger log = LogManager.getLogger("com.foo.Bar"); final String msg = "Async logger msg"; log.info(msg, new InternalError("this is not a real error")); CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); reader.close(); file.delete(); assertNotNull("line1", line1); assertTrue("line1 correct", line1.contains(msg)); assertTrue("ThreadContext.map", line1.contains("mapvalue")); assertTrue("ThreadContext.stack", line1.contains("stackvalue")); }
@Test public void testParameters() throws Exception { final File file = new File("target", "AsyncLoggerConfigTest4.log"); assertTrue("Deleted old file before test", !file.exists() || file.delete()); final Logger log = LogManager.getLogger("com.foo.Bar"); log.info("Additive logging: {} for the price of {}!", 2, 1); CoreLoggerContexts.stopLoggerContext(file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); final String line2 = reader.readLine(); final String line3 = reader.readLine(); final String line4 = reader.readLine(); final String line5 = reader.readLine(); reader.close(); file.delete(); assertThat(line1, containsString("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!")); assertThat(line2, containsString("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!")); assertThat(line3, containsString("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!")); assertThat(line4, containsString("Additive logging: {} for the price of {}! [2,1] Additive logging: 2 for the price of 1!")); assertNull("Expected only two lines to be logged", line5); } }
@Test public void testAdditivity() throws Exception { final File file = new File("target", "AsyncLoggerConfigTest.log"); assertTrue("Deleted old file before test", !file.exists() || file.delete()); final Logger log = LogManager.getLogger("com.foo.Bar"); final String msg = "Additive logging: 2 for the price of 1!"; log.info(msg); CoreLoggerContexts.stopLoggerContext(file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); final String line2 = reader.readLine(); reader.close(); file.delete(); assertNotNull("line1", line1); assertNotNull("line2", line2); assertTrue("line1 correct", line1.contains(msg)); assertTrue("line2 correct", line2.contains(msg)); final String location = "testAdditivity"; assertTrue("location", line1.contains(location) || line2.contains(location)); }
@Test public void testAsyncLogWritesToLog() throws Exception { final File file = new File("target", "AsyncLoggerTest.log"); // System.out.println(f.getAbsolutePath()); file.delete(); final AsyncLogger log = (AsyncLogger) LogManager.getLogger("com.foo.Bar"); assertTrue(log.getNanoClock() instanceof DummyNanoClock); final String msg = "Async logger msg"; log.info(msg, new InternalError("this is not a real error")); CoreLoggerContexts.stopLoggerContext(false, file); // stop async thread final BufferedReader reader = new BufferedReader(new FileReader(file)); final String line1 = reader.readLine(); reader.close(); file.delete(); assertNotNull("line1", line1); assertTrue("line1 correct", line1.contains(msg)); final String location = "testAsyncLogWritesToLog"; assertTrue("no location", !line1.contains(location)); }