public void assertMessages(final String msg, final int size, final String methodName) { final ListAppender appender = ctx.getListAppender("ClassAndMethod"); assertEquals(msg + ".size", size, appender.getMessages().size()); for (final String message : appender.getMessages()) { assertEquals( msg + " has incorrect caller info", this.getClass().getName() + '.' + methodName, message); } }
@Test public void test() throws Exception { // To ensure our custom plugin is NOT included in the log4j plugin metadata file, // we make sure the class does not exist until after the build is finished. // So we don't create the custom plugin class until this test is run. final File orig = new File("target/test-classes/customplugin/FixedStringLayout.java.source"); final File f = new File(orig.getParentFile(), "FixedStringLayout.java"); assertTrue("renamed source file OK", orig.renameTo(f)); compile(f); assertTrue("reverted source file OK", f.renameTo(orig)); // load the compiled class Class.forName("customplugin.FixedStringLayout"); // now that the custom plugin class exists, we load the config // with the packages element pointing to our custom plugin ctx = Configurator.initialize("Test1", "customplugin/log4j2-741.xml"); config = ctx.getConfiguration(); listAppender = (ListAppender) config.getAppender("List"); final Logger logger = LogManager.getLogger(PluginManagerPackagesTest.class); logger.info("this message is ignored"); final List<String> messages = listAppender.getMessages(); assertEquals(messages.toString(), 1, messages.size()); assertEquals("abc123XYZ", messages.get(0)); }
@Test public void testLogUsingCustomLevel() throws Exception { logger.config("Config level"); final List<LogEvent> events = eventAppender.getEvents(); assertThat(events, hasSize(1)); final LogEvent event = events.get(0); assertThat(event.getLevel(), equalTo(LevelTranslator.CONFIG)); }
@Test public void testLogParamMarkers() { final Logger flowLogger = Logger.getLogger("TestFlow"); flowLogger.logp( java.util.logging.Level.FINER, "sourceClass", "sourceMethod", "ENTRY {0}", "params"); final List<LogEvent> events = flowAppender.getEvents(); assertEquals("ENTRY params", events.get(0).getMessage().getFormattedMessage()); }
@Test public void testLogWithCallingClass() throws Exception { final Logger log = Logger.getLogger("Test.CallerClass"); log.config("Calling from LoggerTest"); final List<String> messages = stringAppender.getMessages(); assertThat(messages, hasSize(1)); final String message = messages.get(0); assertEquals(AbstractLoggerTest.class.getName(), message); }
@Test public void testReplacement() { // See org.fusesource.jansi.AnsiRenderer logger.error("@|red,bold Warning!|@ Pants on @|red fire!|@"); final List<String> msgs = app.getMessages(); assertNotNull(msgs); assertEquals("Incorrect number of messages. Should be 1 is " + msgs.size(), 1, msgs.size()); assertTrue( "Replacement failed - expected ending " + EXPECTED + ", actual " + msgs.get(0), msgs.get(0).endsWith(EXPECTED)); }
@Test public void noInterpolation() { final Logger logger = Logger.getLogger("Test"); logger.info("{raw}"); logger.log( new LogRecord( INFO, "{raw}")); // should lead to the same as previous but was not the case LOG4J2-1251 final List<LogEvent> events = ListAppender.getListAppender("TestAppender").getEvents(); assertThat(events, hasSize(2)); assertEquals("{raw}", events.get(0).getMessage().getFormattedMessage()); assertEquals("{raw}", events.get(1).getMessage().getFormattedMessage()); }
@Test public void testLog() throws Exception { logger.info("Informative message here."); final List<LogEvent> events = eventAppender.getEvents(); assertThat(events, hasSize(1)); final LogEvent event = events.get(0); assertThat(event, instanceOf(Log4jLogEvent.class)); assertEquals(Level.INFO, event.getLevel()); assertEquals(LOGGER_NAME, event.getLoggerName()); assertEquals("Informative message here.", event.getMessage().getFormattedMessage()); assertEquals(ApiLogger.class.getName(), event.getLoggerFqcn()); }
private void testMessage(String string) { final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); root.info("Test info " + string); root.config("Test info " + string); root.fine("Test info " + string); final List<LogEvent> events = eventAppender.getEvents(); assertThat(events, hasSize(3)); for (final LogEvent event : events) { final String message = event.getMessage().getFormattedMessage(); assertThat(message, equalTo("Test info " + string)); } }
@Test public void testGlobalLogger() throws Exception { final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); root.info("Test info message"); root.config("Test info message"); root.fine("Test info message"); final List<LogEvent> events = eventAppender.getEvents(); assertThat(events, hasSize(3)); for (final LogEvent event : events) { final String message = event.getMessage().getFormattedMessage(); assertThat(message, equalTo("Test info message")); } }
@Test public void testFlowMessages() { final Logger flowLogger = Logger.getLogger("TestFlow"); flowLogger.entering("com.example.TestSourceClass1", "testSourceMethod1(String)"); flowLogger.entering("com.example.TestSourceClass2", "testSourceMethod2(String)", "TestParam"); flowLogger.entering( "com.example.TestSourceClass3", "testSourceMethod3(String)", new Object[] {"TestParam0", "TestParam1"}); final List<LogEvent> events = flowAppender.getEvents(); assertThat(events, hasSize(3)); assertEquals("entry", events.get(0).getMessage().getFormattedMessage()); assertEquals("entry params(TestParam)", events.get(1).getMessage().getFormattedMessage()); assertEquals( "entry params(TestParam0, TestParam1)", events.get(2).getMessage().getFormattedMessage()); }