org.apache.logging.log4j.core.config.Configuration.getAppenders()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(14.8k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中org.apache.logging.log4j.core.config.Configuration.getAppenders()方法的一些代码示例,展示了Configuration.getAppenders()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Configuration.getAppenders()方法的具体详情如下:
包路径:org.apache.logging.log4j.core.config.Configuration
类名称:Configuration
方法名:getAppenders

Configuration.getAppenders介绍

[英]Returns a Map containing all the Appenders and their name.
[中]返回包含所有追加器及其名称的映射。

代码示例

代码示例来源:origin: apache/geode

private boolean hasAppenderFilter(final Configuration config) {
 for (Appender appender : config.getAppenders().values()) {
  if (appender instanceof AbstractFilterable) {
   if (((AbstractFilterable) appender).hasFilter()) {
    return true;
   }
  }
 }
 return false;
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

/**
 * 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);
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

/**
 * 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));
}

代码示例来源:origin: igniterealtime/Openfire

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 );
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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)));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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)));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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)));
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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!");
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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!");
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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!");
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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!");
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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());
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@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());
}

相关文章