private boolean hasAppenderFilter(final Configuration config) { for (Appender appender : config.getAppenders().values()) { if (appender instanceof AbstractFilterable) { if (((AbstractFilterable) appender).hasFilter()) { return true; } } } return false; }
/** * Gets a named Appender for this LoggerContext. * * @param name * the name of the Appender to look up. * @return the named Appender or {@code null} if it wasn't defined in the configuration. */ @SuppressWarnings("unchecked") // Assume the call site knows what it is doing. public <T extends Appender> T getAppender(final String name) { return (T) getConfiguration().getAppenders().get(name); }
/** * Gets a named Appender for this LoggerContext. * * @param <T> * The target Appender class * @param name * the name of the Appender to look up. * @param cls * The target Appender class * @return the named Appender or {@code null} if it wasn't defined in the configuration. */ public <T extends Appender> T getAppender(final String name, final Class<T> cls) { return cls.cast(getConfiguration().getAppenders().get(name)); }
void removeAppender(final String name) { final LoggerContext context = LoggerContext.getContext(false); final Configuration config = context.getConfiguration(); config.getAppenders().remove( name ).stop(); for (final LoggerConfig loggerConfig : config.getLoggers().values()) { loggerConfig.removeAppender( name ); } config.getRootLogger().removeAppender( name ); }
@Test public void testConfiguredAppenders() throws Exception { final Configuration configuration = this.ctx.getConfiguration(); final Map<String, Appender> appenders = configuration.getAppenders(); assertThat(appenders, is(notNullValue())); assertThat(appenders.size(), is(equalTo(3))); }
@Test public void testConfiguredAppenders() throws Exception { final Configuration configuration = this.ctx.getConfiguration(); final Map<String, Appender> appenders = configuration.getAppenders(); assertThat(appenders, is(notNullValue())); assertThat(appenders.size(), is(equalTo(3))); }
@Test public void testBadFileName() throws Exception { final StringBuilder dir = new StringBuilder("/VeryLongDirectoryName"); for (final String element : CHARS) { dir.append(element); dir.append(element.toUpperCase()); } final String value = FILESEP.equals("/") ? dir.toString() + "/test.log" : "1:/target/bad:file.log"; System.setProperty("testfile", value); ctx = Configurator.initialize("Test1", "bad/log4j-badfilename.xml"); LogManager.getLogger("org.apache.test.TestConfigurator"); final Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Unexpected Configuration", "XMLConfigTest", config.getName()); assertThat(config.getAppenders(), hasSize(equalTo(2))); }
@Test public void testPropertiesConfiguration() { final Configuration config = context.getConfiguration(); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 3); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final Logger logger = LogManager.getLogger(getClass()); logger.info("Welcome to Log4j!"); } }
@Test public void testPropertiesConfiguration() { final Configuration config = context.getConfiguration(); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final Logger logger = LogManager.getLogger(getClass()); logger.info("Welcome to Log4j!"); } }
@Test public void testPropertiesConfiguration() { final Configuration config = context.getConfiguration(); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 1); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final Logger logger = LogManager.getLogger(getClass()); logger.info("Welcome to Log4j!"); } }
@Test public void testPropertiesConfiguration() { final Configuration config = context.getConfiguration(); assertNotNull("No configuration created", config); assertEquals("Incorrect State: " + config.getState(), config.getState(), LifeCycle.State.STARTED); final Map<String, Appender> appenders = config.getAppenders(); assertNotNull(appenders); assertTrue("Incorrect number of Appenders: " + appenders.size(), appenders.size() == 1); final Map<String, LoggerConfig> loggers = config.getLoggers(); assertNotNull(loggers); assertTrue("Incorrect number of LoggerConfigs: " + loggers.size(), loggers.size() == 2); final Filter filter = config.getFilter(); assertNotNull("No Filter", filter); assertTrue("Not a Threshold Filter", filter instanceof ThresholdFilter); final Logger logger = LogManager.getLogger(getClass()); assertEquals("Incorrect level " + logger.getLevel(), Level.DEBUG, logger.getLevel()); logger.debug("Welcome to Log4j!"); } }
@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()); }