@After public void after() { // Just in case, an @Test blew up Configurator.shutdown(this.loggerContext); }
@After public void cleanup() { System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); if (ctx != null) { Configurator.shutdown(ctx); ctx = null; } }
@Test public void validateXmlSchema() throws Exception { final File file = new File("target", "XmlCompactFileAppenderValidationTest.log.xml"); file.delete(); final Logger log = LogManager.getLogger("com.foo.Bar"); log.warn("Message 1"); log.info("Message 2"); log.debug("Message 3"); Configurator.shutdown(this.loggerContext); this.validateXmlSchema(file); }
@Test public void validateXmlSchemaThrowable() throws Exception { final File file = new File("target", "XmlCompactFileAppenderValidationTest.log.xml"); file.delete(); final Logger log = LogManager.getLogger("com.foo.Bar"); try { throw new IllegalArgumentException("IAE"); } catch (final IllegalArgumentException e) { log.warn("Message 1", e); } Configurator.shutdown(this.loggerContext); this.validateXmlSchema(file); }
@Test public void validateXmlNoEvents() throws Exception { final File file = new File("target", "XmlCompactFileAppenderValidationTest.log.xml"); file.delete(); Configurator.shutdown(this.loggerContext); this.validateXmlSchema(file); }
public void test(final String[] args) { System.setProperty("log4j.skipJansi", "false"); // LOG4J2-2087: explicitly enable // System.out.println(System.getProperty("java.class.path")); final String config = args == null || args.length == 0 ? "target/test-classes/log4j2-console-xex-ansi.xml" : args[0]; final LoggerContext ctx = Configurator.initialize(ConsoleAppenderAnsiMessagesMain.class.getName(), config); final Logger logger = LogManager.getLogger(ConsoleAppenderJAnsiXExceptionMain.class); try { Files.getFileStore(Paths.get("?BOGUS?")); } catch (final Exception e) { final IllegalArgumentException logE = new IllegalArgumentException("Bad argument foo", e); logger.error("Gotcha!", logE); } finally { Configurator.shutdown(ctx); } }
private static void closeNode(Node node) { try { LoggerContext context = (LoggerContext) LogManager.getContext(false); Configurator.shutdown(context); node.close(); Thread.sleep(250); } catch (Throwable e) { //ignore } }
@AfterClass public static void tearDownClass() { Configurator.shutdown(context); StatusLogger.getLogger().reset(); }
@Override public void evaluate() throws Throwable { if (contextSelectorClass != null) { System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, contextSelectorClass.getName()); } // TODO Consider instead of the above: // LogManager.setFactory(new Log4jContextFactory(LoaderUtil.newInstanceOf(contextSelectorClass))); System.setProperty(SYS_PROP_KEY_CLASS_NAME, description.getClassName()); System.setProperty(SYS_PROP_KEY_DISPLAY_NAME, description.getDisplayName()); loggerContext = Configurator.initialize(description.getDisplayName(), description.getTestClass().getClassLoader(), configLocation); try { base.evaluate(); } finally { if (!Configurator.shutdown(loggerContext, shutdownTimeout, shutdownTimeUnit)) { StatusLogger.getLogger().error("Logger context {} did not shutdown completely after {} {}.", loggerContext.getName(), shutdownTimeout, shutdownTimeUnit); } loggerContext = null; contextSelectorClass = null; StatusLogger.getLogger().reset(); System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR); System.clearProperty(SYS_PROP_KEY_CLASS_NAME); System.clearProperty(SYS_PROP_KEY_DISPLAY_NAME); } } };
Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
@Test public void testInitialize_Name_ClassLoader_URI() throws Exception { ctx = Configurator.initialize("Test1", null, new File("target/test-classes/log4j2-config.xml").toURI()); LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); assertThat("Wrong configuration", map, hasKey("List")); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName()); }
@Test public void testInitialize_InputStream_File() throws Exception { final File file = new File("target/test-classes/log4j2-config.xml"); final InputStream is = new FileInputStream(file); final ConfigurationSource source = new ConfigurationSource(is, file); ctx = Configurator.initialize(null, source); LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); assertThat("Wrong configuration", map, hasKey("List")); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName()); }
@Test public void testFromClassPathProperty() throws Exception { System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "classpath:log4j2-config.xml"); ctx = Configurator.initialize("Test1", null); LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); assertThat("Wrong configuration", map, hasKey("List")); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName()); }
@Test public void testInitialize_NullClassLoader_ConfigurationSourceWithInputStream_NoId() throws Exception { final InputStream is = new FileInputStream("target/test-classes/log4j2-config.xml"); final ConfigurationSource source = new ConfigurationSource(is); ctx = Configurator.initialize(null, source); LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); assertThat("Wrong configuration", map, hasKey("List")); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName()); }
@Test public void testInitialize_Name_LocationName() throws Exception { ctx = Configurator.initialize("Test1", "log4j2-config.xml"); LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); assertThat("Wrong configuration", map, hasKey("List")); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName()); }
@Test public void testFromClassPathWithClassPathPrefix() throws Exception { ctx = Configurator.initialize("Test1", "classpath:log4j2-config.xml"); LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); assertThat("Wrong configuration", map, hasKey("List")); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Incorrect Configuration.", NullConfiguration.NULL_NAME, config.getName()); }
@Test public void testByName() throws Exception { ctx = Configurator.initialize("-config", null); LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); assertThat("Wrong configuration", map, hasKey("List")); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName()); }
@Test public void testInitialize_Name_PathName() throws Exception { ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-config.xml"); LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); assertThat("Wrong configuration", map, hasKey("List")); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName()); }
@Test public void testFromClassPathWithClassLoaderPrefix() throws Exception { ctx = Configurator.initialize("Test1", "classloader:log4j2-config.xml"); LogManager.getLogger("org.apache.test.TestConfigurator"); Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName()); final Map<String, Appender> map = config.getAppenders(); assertNotNull("Appenders map should not be null.", map); assertThat(map, hasSize(greaterThan(0))); assertThat("Wrong configuration", map, hasKey("List")); Configurator.shutdown(ctx); config = ctx.getConfiguration(); assertEquals("Incorrect Configuration.", NullConfiguration.NULL_NAME, config.getName()); }
Configurator.shutdown(ctx);