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

x33g5p2x  于2022-01-23 转载在 其他  
字(11.7k)|赞(0)|评价(0)|浏览(269)

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

Logger.getAppenders介绍

[英]This method is not exposed through the public API and is used primarily for unit testing.
[中]此方法不通过公共API公开,主要用于单元测试。

代码示例

代码示例来源:origin: alibaba/nacos

protected void removeAllAppenders(org.apache.logging.log4j.core.Logger logger) {
    Map<String, Appender> appenders = logger.getAppenders();
    if (appenders != null) {
      for (Appender appender : appenders.values()) {
        logger.removeAppender(appender);
      }
    }
  }
}

代码示例来源:origin: alibaba/nacos

protected void activateAppender(org.apache.logging.log4j.core.Logger logger) {
  removeAllAppenders(this.logger);
  Map<String, Appender> appenders = null;
  if ((appenders = logger.getAppenders()) != null) {
    for (Appender appender : appenders.values()) {
      this.logger.addAppender(appender);
    }
  }
}

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

private static void removeAppenders(final Logger root) {
  final Map<String, Appender> appenders = root.getAppenders();
  for (final Appender appender : appenders.values()) {
    root.removeAppender(appender);
  }
}

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

public void localJobDebugger(int exitVal, String taskId) {
 StringBuilder sb = new StringBuilder();
 sb.append("\n");
 sb.append("Task failed!\n");
 sb.append("Task ID:\n  " + taskId + "\n\n");
 sb.append("Logs:\n");
 console.printError(sb.toString());
 for (Appender appender : ((Logger) LogManager.getRootLogger()).getAppenders().values()) {
  if (appender instanceof FileAppender) {
   console.printError(((FileAppender) appender).getFileName());
  } else if (appender instanceof RollingFileAppender) {
   console.printError(((RollingFileAppender) appender).getFileName());
  }
 }
}

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

String getLogDir(Configuration conf) {
 String logDir = conf.get("hive.log.dir");
 if (logDir == null) {
  logDir = System.getProperty("hive.log.dir");
 }
 if (logDir != null) {
  return logDir;
 }
 LoggerContext context = (LoggerContext)LogManager.getContext(false);
 for (Logger logger: context.getLoggers()) {
  for (Appender appender: logger.getAppenders().values()) {
   if (appender instanceof AbstractOutputStreamAppender) {
    OutputStreamManager manager =
     ((AbstractOutputStreamAppender<?>)appender).getManager();
    if (manager instanceof FileManager) {
     String fileName = ((FileManager)manager).getFileName();
     if (fileName != null) {
      return fileName.substring(0, fileName.lastIndexOf('/'));
     }
    }
   }
  }
 }
 return null;
}

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

protected void removeAppenders() {
  final Map<String,Appender> map = root.getAppenders();
  for (final Map.Entry<String, Appender> entry : map.entrySet()) {
    final Appender app = entry.getValue();
    root.removeAppender(app);
    app.stop();
  }
}

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

void removeAndStopAppenders() {
  final Map<String, Appender> map = logger.getAppenders();
  for (final Map.Entry<String, Appender> entry : map.entrySet()) {
    final Appender appender = entry.getValue();
    logger.removeAppender(appender);
    appender.stop();
  }
}

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

/**
 * Get path of the log file for user to see on the WebUI.
 */
public static String getLogFilePath() {
 String logFilePath = null;
 org.apache.logging.log4j.Logger rootLogger = LogManager.getRootLogger();
 if (rootLogger instanceof org.apache.logging.log4j.core.Logger) {
  org.apache.logging.log4j.core.Logger coreLogger =
    (org.apache.logging.log4j.core.Logger)rootLogger;
  for (Appender appender : coreLogger.getAppenders().values()) {
   if (appender instanceof FileAppender) {
    logFilePath = ((FileAppender) appender).getFileName();
   } else if (appender instanceof RollingFileAppender) {
    logFilePath = ((RollingFileAppender) appender).getFileName();
   } else if (appender instanceof RollingRandomAccessFileAppender) {
    logFilePath = ((RollingRandomAccessFileAppender) appender).getFileName();
   }
  }
 }
 return logFilePath;
}

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

/** {@inheritDoc} */
@Nullable @Override public String fileName() {
  for (Logger log = impl; log != null; log = log.getParent()) {
    for (Appender a : log.getAppenders().values()) {
      if (a instanceof FileAppender)
        return ((FileAppender)a).getFileName();
      if (a instanceof RollingFileAppender)
        return ((RollingFileAppender)a).getFileName();
      if (a instanceof RoutingAppender) {
        try {
          RoutingAppender routing = (RoutingAppender)a;
          Field appsFiled = routing.getClass().getDeclaredField("appenders");
          appsFiled.setAccessible(true);
          Map<String, AppenderControl> appenders = (Map<String, AppenderControl>)appsFiled.get(routing);
          for (AppenderControl control : appenders.values()) {
            Appender innerApp = control.getAppender();
            if (innerApp instanceof FileAppender)
              return normalize(((FileAppender)innerApp).getFileName());
            if (innerApp instanceof RollingFileAppender)
              return normalize(((RollingFileAppender)innerApp).getFileName());
          }
        }
        catch (IllegalAccessException | NoSuchFieldException e) {
          error("Failed to get file name (was the implementation of log4j2 changed?).", e);
        }
      }
    }
  }
  return null;
}

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

@Test
public void testLogger() throws Exception {
  final Logger logger = this.ctx.getLogger(LOGGER_NAME);
  assertThat(logger, is(instanceOf(org.apache.logging.log4j.core.Logger.class)));
  final org.apache.logging.log4j.core.Logger l = (org.apache.logging.log4j.core.Logger) logger;
  assertThat(l.getLevel(), is(equalTo(Level.DEBUG)));
  assertThat(l.filterCount(), is(equalTo(1)));
  final Iterator<Filter> iterator = l.getFilters();
  assertThat(iterator.hasNext(), is(true));
  final Filter filter = iterator.next();
  assertThat(filter, is(instanceOf(ThreadContextMapFilter.class)));
  final Map<String, Appender> appenders = l.getAppenders();
  assertThat(appenders, is(notNullValue()));
  assertThat(appenders.size(), is(equalTo(1)));
  final Appender appender = appenders.get(APPENDER_NAME);
  assertThat(appender, is(notNullValue()));
  assertThat(appender.getName(), is(equalTo("STDOUT")));
}

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

@Test
public void testLogger() throws Exception {
  final Logger logger = this.ctx.getLogger(LOGGER_NAME);
  assertThat(logger, is(instanceOf(org.apache.logging.log4j.core.Logger.class)));
  final org.apache.logging.log4j.core.Logger l = (org.apache.logging.log4j.core.Logger) logger;
  assertThat(l.getLevel(), is(equalTo(Level.DEBUG)));
  assertThat(l.filterCount(), is(equalTo(1)));
  final Iterator<Filter> iterator = l.getFilters();
  assertThat(iterator.hasNext(), is(true));
  final Filter filter = iterator.next();
  assertThat(filter, is(instanceOf(ThreadContextMapFilter.class)));
  final Map<String, Appender> appenders = l.getAppenders();
  assertThat(appenders, is(notNullValue()));
  assertThat(appenders, hasSize(1));
  final Appender appender = appenders.get(APPENDER_NAME);
  assertThat(appender, is(notNullValue()));
  assertThat(appender.getName(), is(equalTo("STDOUT")));
}

代码示例来源:origin: alibaba/nacos

@Override
public void activateAsync(List<Object[]> args) {
  Map<String, Appender> appenders = logger.getAppenders();
  if (appenders == null) {
    throw new IllegalStateException("Activate async appender failed, no appender exist.");
  }
  AppenderRef[] refs = new AppenderRef[appenders.size()];
  int i = 0;
  for (Appender appender : appenders.values()) {
    configuration.addAppender(appender);
    refs[i++] = AppenderRef.createAppenderRef(appender.getName(), null, null);
  }
  AsyncAppender.Builder builder = AsyncAppender.newBuilder()
    .setName(productName + "." + logger.getName() + ".AsyncAppender")
    .setConfiguration(configuration)
    .setAppenderRefs(refs)
    .setBlockingQueueFactory(ArrayBlockingQueueFactory.<LogEvent>createFactory());
  invokeMethod(builder, args);
  AsyncAppender asyncAppender = builder.build();
  asyncAppender.start();
  removeAllAppenders(logger);
  logger.addAppender(asyncAppender);
  setProductName(productName);
}

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

@Test
public void testSubstituteStructuredData() {
  final String mdcId = "RequestContext";
  final String expectedToContain = String.format("ATM %s MSG-ID - Message", PROCESSID);
  for (final Appender appender : root.getAppenders().values()) {
    root.removeAppender(appender);
  }
  final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, false, mdcId,
      null, null, true, null, "ATM", "MSG-ID", "key1, key2, locale", null, null, null, false, null, null);
  final ListAppender appender = new ListAppender("List", null, layout, true, false);
  appender.start();
  root.addAppender(appender);
  root.setLevel(Level.DEBUG);
  root.info("Message");
  try {
    final List<String> list = appender.getMessages();
    assertTrue("Not enough list entries", list.size() > 0);
    final String message =  list.get(0);
    Assert.assertTrue("Not the expected message received", message.contains(expectedToContain));
    appender.clear();
  } finally {
    root.removeAppender(appender);
    appender.stop();
  }
}

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

@Test
  public void testParameterizedMessage() {
    for (final Appender appender : root.getAppenders().values()) {
      root.removeAppender(appender);
    }
    // set up appender
    final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext",
      null, null, true, null, "ATM", null, "key1, key2, locale", null, null, null, true, null, null);
    final ListAppender appender = new ListAppender("List", null, layout, true, false);

    appender.start();

    // set appender on root and set level to debug
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    root.info("Hello {}", "World");
    try {
      final List<String> list = appender.getMessages();
      assertTrue("Not enough list entries", list.size() > 0);
      final String message =  list.get(0);
      assertTrue("Incorrect message. Expected - Hello World, Actual - " + message, message.contains("Hello World"));
    } finally {
      root.removeAppender(appender);
      appender.stop();
    }
  }
}

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

/**
 * Test case for MDC exception conversion pattern.
 */
@Test
public void testException() throws Exception {
  for (final Appender appender : root.getAppenders().values()) {
    root.removeAppender(appender);
  }
  // set up layout/appender
  final AbstractStringLayout layout = Rfc5424Layout.createLayout(Facility.LOCAL0, "Event", 3692, true, "RequestContext",
    null, null, true, null, "ATM", null, "key1, key2, locale", null, "loginId", "%xEx", true, null, null);
  final ListAppender appender = new ListAppender("List", null, layout, true, false);
  appender.start();
  // set appender on root and set level to debug
  root.addAppender(appender);
  root.setLevel(Level.DEBUG);
  ThreadContext.put("loginId", "JohnDoe");
  // output starting message
  root.debug("starting mdc pattern test", new IllegalArgumentException("Test"));
  try {
    final List<String> list = appender.getMessages();
    assertTrue("Not enough list entries", list.size() > 1);
    final String string = list.get(1);
       assertTrue("No Exception in " + string, string.contains("IllegalArgumentException"));
    appender.clear();
  } finally {
    root.removeAppender(appender);
    appender.stop();
  }
}

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

);
for (final Appender appender : root.getAppenders().values()) {
  root.removeAppender(appender);

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

for (final Appender appender : root.getAppenders().values()) {
  root.removeAppender(appender);

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

final Map<String, Appender> appenders = this.rootLogger.getAppenders();
for (final Appender appender : appenders.values()) {
  this.rootLogger.removeAppender(appender);

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

@Test
public void testEscapeLayout() throws Exception {
  final Map<String, Appender> appenders = this.rootLogger.getAppenders();
  for (final Appender appender : appenders.values()) {
    this.rootLogger.removeAppender(appender);

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

@Test
public void testEscapeLayout() throws Exception {
  final Map<String, Appender> appenders = this.rootLogger.getAppenders();
  for (final Appender appender : appenders.values()) {
    this.rootLogger.removeAppender(appender);

相关文章