Java Code Examples for org.apache.logging.log4j.test.appender.ListAppender#start()
The following examples show how to use
org.apache.logging.log4j.test.appender.ListAppender#start() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CategoryTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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); ((org.apache.logging.log4j.core.Logger) 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)); }
Example 2
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
/** * Tests logger.trace(Object). */ @Test public void testTrace() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); ((org.apache.logging.log4j.core.Logger) 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(); ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender); }
Example 3
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
/** * Tests isTraceEnabled. */ @Test public void testIsTraceEnabled() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); try { ((org.apache.logging.log4j.core.Logger) 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 { ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender); } }
Example 4
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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 { ((org.apache.logging.log4j.core.Logger) 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 { ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender); } }
Example 5
Source File: CategoryTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testClassName() { final Category category = Category.getInstance("TestCategory"); final Layout<String> layout = PatternLayout.newBuilder().setPattern("%d %p %C{1.} [%t] %m%n").build(); final ListAppender appender = new ListAppender("List2", null, layout, false, false); appender.start(); category.setAdditivity(false); ((org.apache.logging.log4j.core.Logger) 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)); }
Example 6
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
/** * Tests logger.trace(Object). */ @Test public void testTrace() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); ((org.apache.logging.log4j.core.Logger) 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(); ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender); }
Example 7
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
/** * Tests isTraceEnabled. */ @Test public void testIsTraceEnabled() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); try { ((org.apache.logging.log4j.core.Logger) 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 { ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender); } }
Example 8
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("deprecation") public void testLog() { final PatternLayout layout = PatternLayout.newBuilder().setPattern("%d %C %L %m").build(); final ListAppender appender = new ListAppender("List", null, layout, false, false); appender.start(); final Logger root = Logger.getRootLogger(); try { ((org.apache.logging.log4j.core.Logger) 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 { ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender); } }
Example 9
Source File: Log4j2EcsLayoutTest.java From ecs-logging-java with Apache License 2.0 | 5 votes |
@BeforeEach void setUp() { ctx = new LoggerContext("Test"); ctx.reconfigure(); ctx.getConfiguration().getProperties().put("node.id", "foo"); root = ctx.getRootLogger(); for (final Appender appender : root.getAppenders().values()) { root.removeAppender(appender); } EcsLayout ecsLayout = EcsLayout.newBuilder() .setConfiguration(ctx.getConfiguration()) .setServiceName("test") .setIncludeMarkers(true) .setIncludeOrigin(true) .setStackTraceAsArray(true) .setEventDataset("testdataset.log") .setAdditionalFields(new KeyValuePair[]{ new KeyValuePair("cluster.uuid", "9fe9134b-20b0-465e-acf9-8cc09ac9053b"), new KeyValuePair("node.id", "${node.id}"), new KeyValuePair("empty", "${empty}"), new KeyValuePair("clazz", "%C"), new KeyValuePair("custom", "%custom"), new KeyValuePair("emptyPattern", "%notEmpty{%invalidPattern}"), }) .build(); listAppender = new ListAppender("ecs", null, ecsLayout, false, false); listAppender.start(); root.addAppender(listAppender); root.setLevel(Level.DEBUG); }
Example 10
Source File: CsvParameterLayoutTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
static void testLayoutNormalApi(final Logger root, final AbstractCsvLayout layout, final boolean messageApi) throws Exception { removeAppenders(root); // set up appender final ListAppender appender = new ListAppender("List", null, layout, true, false); appender.start(); appender.countDownLatch = new CountDownLatch(4); // set appender on root and set level to debug root.addAppender(appender); root.setLevel(Level.DEBUG); // output messages if (messageApi) { logDebugObjectArrayMessage(root); } else { logDebugNormalApi(root); } final int msgCount = 4; if (appender.getMessages().size() < msgCount) { // wait until background thread finished processing appender.countDownLatch.await(10, 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 char d = layout.getFormat().getDelimiter(); Assert.assertEquals("1" + d + "2" + d + "3", list.get(0)); Assert.assertEquals("2" + d + "3", list.get(1)); Assert.assertEquals("5" + d + "6", list.get(2)); Assert.assertEquals("7" + d + "8" + d + "9" + d + "10", list.get(3)); }
Example 11
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Tests logger.trace(Object, Exception). */ @Test public void testTraceWithException() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); try { ((org.apache.logging.log4j.core.Logger) 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(); } finally { ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender); } }
Example 12
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Tests logger.trace(Object, Exception). */ @Test public void testTraceWithException() { final ListAppender appender = new ListAppender("List"); appender.start(); final Logger root = Logger.getRootLogger(); try { ((org.apache.logging.log4j.core.Logger) 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(); } finally { ((org.apache.logging.log4j.core.Logger) root.getLogger()).removeAppender(appender); } }