use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class TestConfigurator method testEnvironment.
@Test
public void testEnvironment() throws Exception {
ctx = Configurator.initialize("-config", null);
LogManager.getLogger("org.apache.test.TestConfigurator");
final 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("No ListAppender named List2", map, hasKey("List2"));
final Appender app = map.get("List2");
final Layout<? extends Serializable> layout = app.getLayout();
assertNotNull("Appender List2 does not have a Layout", layout);
assertThat("Appender List2 is not configured with a PatternLayout", layout, instanceOf(PatternLayout.class));
final String pattern = ((PatternLayout) layout).getConversionPattern();
assertNotNull("No conversion pattern for List2 PatternLayout", pattern);
assertFalse("Environment variable was not substituted", pattern.startsWith("${env:PATH}"));
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class TestConfigurator method testInitialize_NullClassLoader_ConfigurationSourceWithInputStream_NoId.
@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());
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class TestConfigurator method testFromClassPathProperty.
@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());
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class TestConfigurator method testReconfiguration.
@Test
public void testReconfiguration() throws Exception {
final File file = new File("target/test-classes/log4j2-config.xml");
assertTrue("setLastModified should have succeeded.", file.setLastModified(System.currentTimeMillis() - 120000));
ctx = Configurator.initialize("Test1", "target/test-classes/log4j2-config.xml");
final Logger logger = 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"));
// Sleep and check
Thread.sleep(50);
if (!file.setLastModified(System.currentTimeMillis())) {
Thread.sleep(500);
}
assertTrue("setLastModified should have succeeded.", file.setLastModified(System.currentTimeMillis()));
TimeUnit.SECONDS.sleep(config.getWatchManager().getIntervalSeconds() + 1);
for (int i = 0; i < 17; ++i) {
logger.debug("Test message " + i);
}
// Sleep and check
Thread.sleep(50);
if (is(theInstance(config)).matches(ctx.getConfiguration())) {
Thread.sleep(500);
}
final Configuration newConfig = ctx.getConfiguration();
assertThat("Configuration not reset", newConfig, is(not(theInstance(config))));
Configurator.shutdown(ctx);
config = ctx.getConfiguration();
assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class TestConfigurator method testByName.
@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());
}
Aggregations