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

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

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

Configuration.getAppender介绍

[英]Returns the Appender with the specified name.
[中]返回具有指定名称的追加器。

代码示例

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

/**
 * Given an appender name, as configured, get the parent directory of the appender's log file.
 * Note that if anything goes wrong, this will throw an Error and exit.
 */
private String logRootDir(String appenderName) {
  Appender appender = ((LoggerContext) LogManager.getContext()).getConfiguration().getAppender(appenderName);
  if (appenderName != null && appender != null && RollingFileAppender.class.isInstance(appender)) {
    return new File(((RollingFileAppender) appender).getFileName()).getParent();
  } else {
    throw new RuntimeException("Log viewer could not find configured appender, or the appender is not a FileAppender. "
        + "Please check that the appender name configured in storm and log4j agree.");
  }
}

代码示例来源:origin: Graylog2/graylog2-server

private Appender getAppender(final String appenderName) {
  final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
  final Configuration configuration = loggerContext.getConfiguration();
  return configuration.getAppender(appenderName);
}

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

@Override
public void enableLoggingToStandardOutput() {
 Configuration log4jConfiguration = getRootLoggerContext().getConfiguration();
 Appender appender = log4jConfiguration.getAppender(GEODE_CONSOLE_APPENDER_NAME);
 if (GeodeConsoleAppender.class.isInstance(appender)) {
  GeodeConsoleAppender geodeConsoleAppender = (GeodeConsoleAppender) appender;
  geodeConsoleAppender.resume();
 }
}

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

@Override
public void disableLoggingToStandardOutput() {
 Configuration log4jConfiguration = getRootLoggerContext().getConfiguration();
 Appender appender = log4jConfiguration.getAppender(GEODE_CONSOLE_APPENDER_NAME);
 if (GeodeConsoleAppender.class.isInstance(appender)) {
  GeodeConsoleAppender geodeConsoleAppender = (GeodeConsoleAppender) appender;
  geodeConsoleAppender.pause();
 }
}

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

private void testInitializeFromFilePath(final String path) {
    try (final LoggerContext loggerContext = Configurator.initialize(getClass().getName(), null, path)) {
      Assert.assertNotNull(loggerContext.getConfiguration().getAppender("List"));
    }
  }
}

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

/**
 * Gets the named ListAppender if it has been registered.
 *
 * @param name the name of the ListAppender
 * @return the named ListAppender or {@code null} if it does not exist
 * @see org.apache.logging.log4j.junit.LoggerContextRule#getListAppender(String)
 */
public static ListAppender getListAppender(final String name) {
  return ((ListAppender) (LoggerContext.getContext(false)).getConfiguration().getAppender(name));
}

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

private boolean configureSecurityAppenders(final String name, final Level level) {
 Configuration log4jConfiguration = getRootLoggerContext().getConfiguration();
 LoggerConfig loggerConfig = log4jConfiguration.getLoggerConfig(name);
 if (!loggerConfig.getName().equals(SECURITY_LOGGER_NAME)) {
  return false;
 }
 Appender stdoutAppender = log4jConfiguration.getAppender(GEODE_CONSOLE_APPENDER_NAME);
 Appender mainLogWriterAppender = log4jConfiguration.getAppender(LOGWRITER_APPENDER_NAME);
 if (stdoutAppender != null) {
  loggerConfig.addAppender(stdoutAppender, level, null);
 }
 if (mainLogWriterAppender != null) {
  loggerConfig.addAppender(mainLogWriterAppender, level, null);
 }
 loggerConfig.setAdditive(true);
 getRootLoggerContext().updateLoggers();
 return true;
}

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

private void validateAppender(final LoggerContext loggerContext, final String expectedFilePattern) {
    final RollingFileAppender appender = loggerContext.getConfiguration().getAppender("fooAppender");
    Assert.assertNotNull(appender);
    Assert.assertEquals(expectedFilePattern, appender.getFilePattern());
    LogManager.getLogger("root").info("just to show it works.");
  }
}

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

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

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

@Test(timeout = 5000L)
public void xmlSecurity() throws IOException {
  final LoggerContext context = Configurator.initialize("XmlConfigurationSecurity", "XmlConfigurationSecurity.xml");
  assertNotNull(context.getConfiguration().getAppender("list"));
}

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

final RollingFileAppender app = (RollingFileAppender) loggerContextRule.getLoggerContext().getConfiguration().getAppender("RollingFile");
final TriggeringPolicy policy = app.getManager().getTriggeringPolicy();
assertNotNull("No triggering policy", policy);

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

logger.debug("This is test message number 1, waiting for rolling");
final RollingFileAppender app = (RollingFileAppender) loggerContextRule.getLoggerContext().getConfiguration().getAppender("RollingFile");
final TriggeringPolicy policy = app.getManager().getTriggeringPolicy();
assertNotNull("No triggering policy", policy);

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

@Test
public void testConfiguration() {
  final Configuration configuration = context.getConfiguration();
  assertNotNull(configuration);
  final FileAppender appender = configuration.getAppender("info");
  assertNotNull(appender);
  final CompositeFilter compFilter = (CompositeFilter) appender.getFilter();
  assertNotNull(compFilter);
  final Filter[] filters = compFilter.getFiltersArray();
  assertNotNull(filters);
  boolean foundLevel = false;
  for (final Filter filter : filters) {
    final ThresholdFilter tFilter = (ThresholdFilter) filter;
    if (infom1Level.equals(tFilter.getLevel())) {
      foundLevel = true;
      break;
    }
  }
  Assert.assertTrue("Level not found: " + infom1Level, foundLevel);
}

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

@Test
  public void test() {
    final Configuration configuration = context.getConfiguration();
    assertNotNull(configuration);
    final RollingFileAppender appender = configuration.getAppender("File");
    assertNotNull(appender);
    final CompositeTriggeringPolicy compositeTriggeringPolicy = appender.getTriggeringPolicy();
    assertNotNull(compositeTriggeringPolicy);
    final TriggeringPolicy[] triggeringPolicies = compositeTriggeringPolicy.getTriggeringPolicies();
    SizeBasedTriggeringPolicy sizeBasedTriggeringPolicy = null;
    TimeBasedTriggeringPolicy timeBasedTriggeringPolicy = null;
    for (final TriggeringPolicy triggeringPolicy : triggeringPolicies) {
      if (triggeringPolicy instanceof TimeBasedTriggeringPolicy) {
        timeBasedTriggeringPolicy = (TimeBasedTriggeringPolicy) triggeringPolicy;
        assertEquals(7, timeBasedTriggeringPolicy.getInterval());
      }
      if (triggeringPolicy instanceof SizeBasedTriggeringPolicy) {
        sizeBasedTriggeringPolicy = (SizeBasedTriggeringPolicy) triggeringPolicy;
        assertEquals(100 * 1024 * 1024, sizeBasedTriggeringPolicy.getMaxFileSize());
      }
    }
    if (timeBasedTriggeringPolicy == null) {
      fail("Missing TimeBasedTriggeringPolicy");
    }
    if (sizeBasedTriggeringPolicy == null) {
      fail("Missing SizeBasedTriggeringPolicy");
    }
  }
}

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

final Configuration config = builder.build();
config.initialize();
assertNotNull("No rolling file appender", config.getAppender("rolling"));
assertEquals("Unexpected Configuration", "RollingBuilder", config.getName());

代码示例来源:origin: org.graylog2/graylog2-server

private Appender getAppender(final String appenderName) {
  final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
  final Configuration configuration = loggerContext.getConfiguration();
  return configuration.getAppender(appenderName);
}

代码示例来源:origin: ops4j/org.ops4j.pax.logging

@Override
public void start() {
  for (final AppenderRef ref : appenderRefs) {
    final String name = ref.getRef();
    final Appender appender = config.getAppender(name);
    if (appender != null) {
      final Filter filter = appender instanceof AbstractAppender ?
        ((AbstractAppender) appender).getFilter() : null;
      appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
    } else {
      LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
    }
  }
  super.start();
}

代码示例来源:origin: org.infinispan/infinispan-core

@BeforeMethod
public void setUpTest() {
 final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
 final Configuration config = ctx.getConfiguration();
 appender = (InMemoryAppender) config.getAppender("InMemory");
 appender.enable(Thread.currentThread());
 ctx.updateLoggers();
 configRoot = "../distribution/src/main/release/common/configs/config-samples".replace('/', File.separatorChar);
}

代码示例来源:origin: org.appenders.log4j/log4j2-elasticsearch-core

private void resolveAppender() {
  if (appenderControl == null) {
    Appender appender = configuration.getAppender(appenderRef.getRef());
    if (appender == null) {
      throw new ConfigurationException("No failover appender named " + appenderRef.getRef() + " found");
    }
    appenderControl = new AppenderControl(appender, appenderRef.getLevel(), appenderRef.getFilter());
  }
}

代码示例来源:origin: rfoltyns/log4j2-elasticsearch

private void resolveAppender() {
  if (appenderControl == null) {
    Appender appender = configuration.getAppender(appenderRef.getRef());
    if (appender == null) {
      throw new ConfigurationException("No failover appender named " + appenderRef.getRef() + " found");
    }
    appenderControl = new AppenderControl(appender, appenderRef.getLevel(), appenderRef.getFilter());
  }
}

相关文章