@Test public void testClassName() { final Category category = Category.getInstance("TestCategory"); final Layout<String> layout = PatternLayout.createLayout("%d %p %C{1.} [%t] %m%n", null, null, null, null); final ListAppender appender = new ListAppender("List2", null, layout, false, false); appender.start(); category.setAdditivity(false); category.getLogger().addAppender(appender); category.error("Test Message"); final List<String> msgs = appender.getMessages(); assertTrue("Incorrect number of messages. Expected 1 got " + msgs.size(), msgs.size() == 1); final String msg = msgs.get(0); appender.clear(); final String threadName = Thread.currentThread().getName(); final String expected = "ERROR o.a.l.CategoryTest [" + threadName + "] Test Message" + Constants.LINE_SEP; assertTrue("Incorrect message \"" + msg + "\"" + " expected \"" + expected +"\"", msg.endsWith(expected)); }
/** * Tests logger.trace(Object). */ @Test public void testTrace() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); root.getLogger().addAppender(appender); root.setLevel(Level.INFO); final Logger tracer = Logger.getLogger("com.example.Tracer"); tracer.setLevel(Level.TRACE); tracer.trace("Message 1"); root.trace("Discarded Message"); root.trace("Discarded Message"); final List<LogEvent> msgs = appender.getEvents(); assertEquals(1, msgs.size()); final LogEvent event = msgs.get(0); assertEquals(org.apache.logging.log4j.Level.TRACE, event.getLevel()); assertEquals("Message 1", event.getMessage().getFormat()); appender.stop(); root.getLogger().removeAppender(appender); }
/** * Tests logger.trace(Object, Exception). */ @Test public void testTraceWithException() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); root.getLogger().addAppender(appender); root.setLevel(Level.INFO); final Logger tracer = Logger.getLogger("com.example.Tracer"); tracer.setLevel(Level.TRACE); final NullPointerException ex = new NullPointerException(); tracer.trace("Message 1", ex); root.trace("Discarded Message", ex); root.trace("Discarded Message", ex); final List<LogEvent> msgs = appender.getEvents(); assertEquals(1, msgs.size()); final LogEvent event = msgs.get(0); assertEquals(org.apache.logging.log4j.Level.TRACE, event.getLevel()); assertEquals("Message 1", event.getMessage().getFormattedMessage()); appender.stop(); root.getLogger().removeAppender(appender); }
/** * Tests isTraceEnabled. */ @Test public void testIsTraceEnabled() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); root.getLogger().addAppender(appender); root.setLevel(Level.INFO); final Logger tracer = Logger.getLogger("com.example.Tracer"); tracer.setLevel(Level.TRACE); assertTrue(tracer.isTraceEnabled()); assertFalse(root.isTraceEnabled()); appender.stop(); root.getLogger().removeAppender(appender); }
@Test @SuppressWarnings("deprecation") public void testLog() { final PatternLayout layout = PatternLayout.createLayout("%d %C %L %m", null, null, null, null); final ListAppender appender = new ListAppender("List", null, layout, false, false); appender.start(); final Logger root = Logger.getRootLogger(); root.getLogger().addAppender(appender); root.setLevel(Level.INFO); final MyLogger log = new MyLogger(root); log.logInfo("This is a test", null); root.log(Priority.INFO, "Test msg2", null); root.log(Priority.INFO, "Test msg3"); final List<String> msgs = appender.getMessages(); assertTrue("Incorrect number of messages", msgs.size() == 3); final String msg = msgs.get(0); assertTrue("Message contains incorrect class name: " + msg, msg.contains(LoggerTest.class.getName())); appender.stop(); root.getLogger().removeAppender(appender); }
private void verify(final String expected) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Map<String, Appender> list = ctx.getConfiguration().getAppenders(); final Appender listApp = list.get("List"); assertNotNull("Missing Appender", listApp); assertTrue("Not a ListAppender", listApp instanceof ListAppender); final List<String> events = ((ListAppender) listApp).getMessages(); try { assertEquals("Incorrect number of messages.", 1, events.size()); assertEquals("Incorrect message.", "o.a.l.l.t.CatchingTagTest " + expected, events.get(0)); } finally { ((ListAppender) listApp).clear(); } }
private void verify(final String expected) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Map<String, Appender> list = ctx.getConfiguration().getAppenders(); final Appender listApp = list.get("List"); assertNotNull("Missing Appender", listApp); assertTrue("Not a ListAppender", listApp instanceof ListAppender); final List<String> events = ((ListAppender) listApp).getMessages(); try { assertEquals("Incorrect number of messages.", 1, events.size()); assertEquals("Incorrect message.", "o.a.l.l.t.EntryTagTest " + expected, events.get(0)); } finally { ((ListAppender) listApp).clear(); } }
@SuppressWarnings("unchecked") private void verify(final String expected) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Map<String, Appender> list = ctx.getConfiguration().getAppenders(); final Appender listApp = list.get("List"); assertNotNull("Missing Appender", listApp); assertTrue("Not a ListAppender", listApp instanceof ListAppender); final List<String> events = ((ListAppender) listApp).getMessages(); try { assertEquals("Incorrect number of messages.", 1, events.size()); assertEquals("Incorrect message.", "o.a.l.l.t.LoggingMessageTagSupportTest " + expected, events.get(0)); } finally { ((ListAppender) listApp).clear(); } }
private void verify(final String expected) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Map<String, Appender> list = ctx.getConfiguration().getAppenders(); final Appender listApp = list.get("List"); assertNotNull("Missing Appender", listApp); assertTrue("Not a ListAppender", listApp instanceof ListAppender); final List<String> events = ((ListAppender) listApp).getMessages(); try { assertEquals("Incorrect number of messages.", 1, events.size()); assertEquals("Incorrect message.", "o.a.l.l.t.ExitTagTest " + expected, events.get(0)); } finally { ((ListAppender) listApp).clear(); } }
@Before public void before() { config = ctx.getConfiguration(); for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) { if (entry.getKey().equals("List")) { app = (ListAppender) entry.getValue(); } else if (entry.getKey().equals("HostTest")) { host = (ListAppender) entry.getValue(); } else if (entry.getKey().equals("NoThrowable")) { noThrown = (ListAppender) entry.getValue(); } } assertNotNull("No Appender", app); assertNotNull("No Host Appender", host); app.clear(); host.clear(); }
@BeforeClass public static void setupClass() { System.setProperty(Constants.LOG4J_LOG_EVENT_FACTORY, TestLogEventFactory.class.getName()); System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); ctx = (LoggerContext) LogManager.getContext(false); config = ctx.getConfiguration(); for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) { if (entry.getKey().equals("List")) { app = (ListAppender) entry.getValue(); break; } } if (app == null) { fail("No List Appender could be found"); } }
@Test public void testConfig() { final Configuration config = context.getConfiguration(); final Filter filter = config.getFilter(); assertNotNull("No MapFilter", filter); assertTrue("Not a MapFilter", filter instanceof MapFilter); final MapFilter mapFilter = (MapFilter) filter; assertFalse("Should not be And filter", mapFilter.isAnd()); final Map<String, List<String>> map = mapFilter.getMap(); assertNotNull("No Map", map); assertFalse("No elements in Map", map.isEmpty()); assertEquals("Incorrect number of elements in Map", 1, map.size()); assertTrue("Map does not contain key eventId", map.containsKey("eventId")); assertEquals("List does not contain 2 elements", 2, map.get("eventId").size()); final Logger logger = LogManager.getLogger(MapFilterTest.class); final Map<String, String> eventMap = new HashMap<>(); eventMap.put("eventId", "Login"); logger.debug(new StringMapMessage(eventMap)); final ListAppender app = context.getListAppender("LIST"); final List<String> msgs = app.getMessages(); assertNotNull("No messages", msgs); assertFalse("No messages", msgs.isEmpty()); }
@Test public void testLogJsonArgument() throws InterruptedException { final ListAppender appender = (ListAppender) init.getAppender("List"); appender.countDownLatch = new CountDownLatch(4); appender.clear(); final Logger logger = (Logger) LogManager.getRootLogger(); final String json = "{\"id\":10,\"name\":\"Alice\"}"; logger.error("log:{}", json); // wait until background thread finished processing final int msgCount = 1; if (appender.getMessages().size() < msgCount) { appender.countDownLatch.await(5, TimeUnit.SECONDS); } assertEquals("Background thread did not finish processing: msg count", msgCount, appender.getMessages().size()); // don't stop appender until background thread is done appender.stop(); final List<String> list = appender.getMessages(); final String eventStr = list.get(0).toString(); Assert.assertTrue(eventStr, eventStr.contains(json)); }
@Test public void testClassName() { final Category category = Category.getInstance("TestCategory"); final Layout<String> layout = PatternLayout.newBuilder().withPattern("%d %p %C{1.} [%t] %m%n").build(); final ListAppender appender = new ListAppender("List2", null, layout, false, false); appender.start(); category.setAdditivity(false); category.getLogger().addAppender(appender); category.error("Test Message"); final List<String> msgs = appender.getMessages(); assertTrue("Incorrect number of messages. Expected 1 got " + msgs.size(), msgs.size() == 1); final String msg = msgs.get(0); appender.clear(); final String threadName = Thread.currentThread().getName(); final String expected = "ERROR o.a.l.CategoryTest [" + threadName + "] Test Message" + Strings.LINE_SEPARATOR; assertTrue("Incorrect message " + Strings.dquote(msg) + " expected " + Strings.dquote(expected), msg.endsWith(expected)); }
/** * Tests isTraceEnabled. */ @Test public void testIsTraceEnabled() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); try { root.getLogger().addAppender(appender); root.setLevel(Level.INFO); final Logger tracer = Logger.getLogger("com.example.Tracer"); tracer.setLevel(Level.TRACE); assertTrue(tracer.isTraceEnabled()); assertFalse(root.isTraceEnabled()); appender.stop(); } finally { root.getLogger().removeAppender(appender); } }
@Test @SuppressWarnings("deprecation") public void testLog() { final PatternLayout layout = PatternLayout.newBuilder().withPattern("%d %C %L %m").build(); final ListAppender appender = new ListAppender("List", null, layout, false, false); appender.start(); final Logger root = Logger.getRootLogger(); try { root.getLogger().addAppender(appender); root.setLevel(Level.INFO); final MyLogger log = new MyLogger(root); log.logInfo("This is a test", null); root.log(Priority.INFO, "Test msg2", null); root.log(Priority.INFO, "Test msg3"); final List<String> msgs = appender.getMessages(); assertTrue("Incorrect number of messages", msgs.size() == 3); final String msg = msgs.get(0); assertTrue("Message contains incorrect class name: " + msg, msg.contains(LoggerTest.class.getName())); appender.stop(); } finally { root.getLogger().removeAppender(appender); } }
@Test public void test() { logger.info("This is a test.", new Exception("Test exception!")); ListAppender listAppender = loggerContextRule.getListAppender("ListAppender"); Assert.assertNotNull(listAppender); List<String> events = listAppender.getMessages(); Assert.assertNotNull(events); Assert.assertEquals(1, events.size()); String logEvent = events.get(0); Assert.assertNotNull(logEvent); Assert.assertFalse("\"org.junit\" should not be here", logEvent.contains("org.junit")); Assert.assertFalse("\"org.eclipse\" should not be here", logEvent.contains("org.eclipse")); // Layout<? extends Serializable> layout = listAppender.getLayout(); PatternLayout pLayout = (PatternLayout) layout; Assert.assertNotNull(pLayout); Serializer eventSerializer = pLayout.getEventSerializer(); Assert.assertNotNull(eventSerializer); // Assert.assertTrue("Missing \"|\"", logEvent.contains("|")); }
@Test public void testMarkerPatternSelector() throws Exception { final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestMarkerPatternSelector"); logger.traceEntry(); logger.info("Hello World"); logger.traceExit(); final ListAppender app = (ListAppender) context.getRequiredAppender("List"); assertNotNull("No ListAppender", app); final List<String> messages = app.getMessages(); assertNotNull("No Messages", messages); assertTrue("Incorrect number of messages. Expected 3, Actual " + messages.size() + ": " + messages, messages.size() == 3); final String expect = String.format("[TRACE] TestMarkerPatternSelector ====== " + "o.a.l.l.c.PatternSelectorTest.testMarkerPatternSelector:43 Enter ======%n"); assertEquals(expect, messages.get(0)); assertEquals("[INFO ] TestMarkerPatternSelector Hello World" + Strings.LINE_SEPARATOR, messages.get(1)); app.clear(); }
@Test public void testScriptPatternSelector() throws Exception { final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestScriptPatternSelector"); final org.apache.logging.log4j.Logger logger2 = LogManager.getLogger("NoLocation"); logger.traceEntry(); logger.info("Hello World"); logger2.info("No location information"); logger.traceExit(); final ListAppender app = (ListAppender) context.getRequiredAppender("List2"); assertNotNull("No ListAppender", app); final List<String> messages = app.getMessages(); assertNotNull("No Messages", messages); assertTrue("Incorrect number of messages. Expected 4, Actual " + messages.size() + ": " + messages, messages.size() == 4); String expect = "[TRACE] TestScriptPatternSelector ====== " + "o.a.l.l.c.PatternSelectorTest.testScriptPatternSelector:62 Enter ======" + Strings.LINE_SEPARATOR; assertEquals(expect, messages.get(0)); expect = "[INFO ] TestScriptPatternSelector o.a.l.l.c.PatternSelectorTest.testScriptPatternSelector.63 " + "Hello World" + Strings.LINE_SEPARATOR; assertEquals(expect, messages.get(1)); assertEquals("[INFO ] NoLocation No location information" + Strings.LINE_SEPARATOR, messages.get(2)); app.clear(); }
@Test public void testJavaScriptPatternSelector() throws Exception { final org.apache.logging.log4j.Logger logger = LogManager.getLogger("TestJavaScriptPatternSelector"); final org.apache.logging.log4j.Logger logger2 = LogManager.getLogger("JavascriptNoLocation"); logger.traceEntry(); logger.info("Hello World"); logger2.info("No location information"); logger.traceExit(); final ListAppender app = (ListAppender) context.getRequiredAppender("List3"); assertNotNull("No ListAppender", app); final List<String> messages = app.getMessages(); assertNotNull("No Messages", messages); assertTrue("Incorrect number of messages. Expected 4, Actual " + messages.size() + ": " + messages, messages.size() == 4); String expect = "[TRACE] TestJavaScriptPatternSelector ====== " + "o.a.l.l.c.PatternSelectorTest.testJavaScriptPatternSelector:85 Enter ======" + Strings.LINE_SEPARATOR; assertEquals(expect, messages.get(0)); expect = "[INFO ] TestJavaScriptPatternSelector " + "o.a.l.l.c.PatternSelectorTest.testJavaScriptPatternSelector.86 Hello World" + Strings.LINE_SEPARATOR; assertEquals(expect, messages.get(1)); assertEquals("[INFO ] JavascriptNoLocation No location information" + Strings.LINE_SEPARATOR, messages.get(2)); app.clear(); }
@Test public void testGroovyFilter() throws Exception { final Logger logger = LogManager.getLogger("TestGroovyFilter"); logger.traceEntry(); logger.info("This should not be logged"); ThreadContext.put("UserId", "JohnDoe"); logger.info("This should be logged"); ThreadContext.clearMap(); final ListAppender app = getContext().getListAppender("List"); try { final List<String> messages = app.getMessages(); assertNotNull("No Messages", messages); assertTrue("Incorrect number of messages. Expected 2, Actual " + messages.size(), messages.size() == 2); } finally { app.clear(); } }
private void verify(final String name, final String expected) { //LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Map<String, Appender> list = ctx.getConfiguration().getAppenders(); final Appender listApp = list.get(name); assertNotNull("Missing Appender", listApp); assertTrue("Not a ListAppender", listApp instanceof ListAppender); final List<String> events = ((ListAppender) listApp).getMessages(); assertTrue("Incorrect number of messages. Expected 1 Actual " + events.size(), events.size()== 1); final String actual = events.get(0); assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual); ((ListAppender) listApp).clear(); }
@Before public void cleanup() { final Map<String, Appender> list = ctx.getConfiguration().getAppenders(); final Appender listApp = list.get("List"); ((ListAppender) listApp).clear(); final Appender eventApp = list.get("EventLogger"); ((ListAppender) eventApp).clear(); }
private void verify(final String name, final String expected) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Map<String, Appender> list = ctx.getConfiguration().getAppenders(); final Appender listApp = list.get(name); assertNotNull("Missing Appender", listApp); assertTrue("Not a ListAppender", listApp instanceof ListAppender); final List<String> events = ((ListAppender) listApp).getMessages(); assertTrue("Incorrect number of messages. Expected 1 Actual " + events.size(), events.size()== 1); final String actual = events.get(0); assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual); ((ListAppender) listApp).clear(); }
@BeforeClass public static void setupClass() { System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); ctx = (LoggerContext) LogManager.getContext(false); config = ctx.getConfiguration(); for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) { if (entry.getKey().equals("List")) { app = (ListAppender) entry.getValue(); break; } } }
@BeforeClass public static void setupClass() { System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); ctx = (LoggerContext) LogManager.getContext(false); config = ctx.getConfiguration(); for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) { if (entry.getKey().equals("List")) { app = (ListAppender) entry.getValue(); } if (entry.getKey().equals("List2")) { app2 = (ListAppender) entry.getValue(); } } }
@BeforeClass public static void setupClass() { System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); ctx = (LoggerContext) LogManager.getContext(false); config = ctx.getConfiguration(); for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) { if (entry.getKey().equals("List")) { app = (ListAppender) entry.getValue(); } } }
@BeforeClass public static void setupClass() { System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); ctx = (LoggerContext) LogManager.getContext(false); config = ctx.getConfiguration(); for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) { if (entry.getKey().equals("List")) { app = (ListAppender) entry.getValue(); assertNotNull(app); break; } } }
@BeforeClass public static void setupClass() { System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); System.setProperty("test", "test"); ctx = (LoggerContext) LogManager.getContext(false); config = ctx.getConfiguration(); for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) { if (entry.getKey().equals("List")) { app = (ListAppender) entry.getValue(); break; } } }
@Before public void before() { config = ctx.getConfiguration(); for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) { if (entry.getKey().equals("List")) { app = (ListAppender) entry.getValue(); break; } } assertNotNull("No Appender", app); app.clear(); }
@Before public void before() { config = ctx.getConfiguration(); for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) { if (entry.getKey().equals("List")) { app = (ListAppender) entry.getValue(); break; } } assertNotNull("No Appender", app); app.clear(); ThreadContext.clear(); }
protected void testServer(final String message1, final String message2) throws Exception { final Filter socketFilter = new ThreadFilter(Filter.Result.NEUTRAL, Filter.Result.DENY); final Filter serverFilter = new ThreadFilter(Filter.Result.DENY, Filter.Result.NEUTRAL); final SocketAppender appender = SocketAppender.createAppender("localhost", this.port, this.protocol, "-1", null, "Test", null, "false", null, socketFilter, null, null); appender.start(); final ListAppender listApp = new ListAppender("Events", serverFilter, null, false, false); listApp.start(); final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null); final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true"); final Logger serverLogger = ctx.getLogger(this.getClass().getName()); serverLogger.addAppender(console); serverLogger.setAdditive(false); // set appender on root and set level to debug root.addAppender(appender); root.addAppender(listApp); root.setAdditive(false); root.setLevel(Level.DEBUG); root.debug(message1); root.debug(message2); Thread.sleep(100); final List<LogEvent> events = listApp.getEvents(); assertNotNull("No event retrieved", events); assertTrue("No events retrieved", events.size() > 0); assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals(message1)); assertTrue("Incorrect number of events received: " + events.size(), events.size() == 2); assertTrue("Incorrect event", events.get(1).getMessage().getFormattedMessage().equals(message2)); }
@Test public void testServer() throws Exception { final Filter clientFilter = new MessageFilter(Filter.Result.NEUTRAL, Filter.Result.DENY); final Filter serverFilter = new MessageFilter(Filter.Result.DENY, Filter.Result.NEUTRAL); final CompositeFilter clientFilters = CompositeFilter.createFilters(new Filter[]{clientFilter}); final JMSQueueAppender appender = JMSQueueAppender.createAppender("Test", null, null, null, null, null, FACTORY_NAME, QUEUE_NAME, null, null, null, clientFilters, "true"); appender.start(); final CompositeFilter serverFilters = CompositeFilter.createFilters(new Filter[]{serverFilter}); final ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false); listApp.start(); final PatternLayout layout = PatternLayout.createLayout("%m %ex%n", null, null, null, null); final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true"); console.start(); final Logger serverLogger = ctx.getLogger(JMSTopicReceiver.class.getName()); serverLogger.addAppender(console); serverLogger.setAdditive(false); // set appender on root and set level to debug root.addAppender(listApp); root.addAppender(appender); root.setAdditive(false); root.setLevel(Level.DEBUG); root.debug("This is a test message"); Thread.sleep(100); final List<LogEvent> events = listApp.getEvents(); assertNotNull("No event retrieved", events); assertTrue("No events retrieved", events.size() > 0); assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals("This is a test message")); }
@Test public void testDateLookupInMessage() { final String template = "${date:YYYY-MM-dd}"; context.getLogger(PatternLayoutNoLookupDateTest.class.getName()).info(template); final ListAppender listAppender = context.getListAppender("List"); final String string = listAppender.getMessages().get(0); Assert.assertTrue(string, string.contains(template)); }