ch.qos.logback.classic.Logger.iteratorForAppenders()方法的使用及代码示例

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

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

Logger.iteratorForAppenders介绍

暂无

代码示例

代码示例来源:origin: gocd/gocd

private Appender[] getAppenders(List<Logger> loggers) {
  LinkedHashSet<Appender<ILoggingEvent>> appenders = new LinkedHashSet<>();
  for (Logger logger : loggers) {
    Iterator<Appender<ILoggingEvent>> appenderIterator = logger.iteratorForAppenders();
    while (appenderIterator.hasNext()) {
      Appender<ILoggingEvent> appender = appenderIterator.next();
      appenders.add(appender);
    }
  }
  return appenders.toArray(new Appender[0]);
}

代码示例来源:origin: bwssytems/ha-bridge

/** 
 * Test whether the provided logger has appenders. 
 * 
 * @param logger The logger to test
 * @return true if the logger has appenders.
 */
public static boolean hasAppenders(ch.qos.logback.classic.Logger logger) {
  Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();
  return it.hasNext();
}

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

@Override
public void activateAppender(Logger logger) {
  if (!(logger.getDelegate() instanceof ch.qos.logback.classic.Logger)) {
    throw new IllegalArgumentException(
      "logger must be ch.qos.logback.classic.Logger, but it's " + logger.getDelegate().getClass());
  }
  this.logger.detachAndStopAllAppenders();
  Iterator<ch.qos.logback.core.Appender<ILoggingEvent>> iter = ((ch.qos.logback.classic.Logger)logger
    .getDelegate()).iteratorForAppenders();
  while (iter.hasNext()) {
    ch.qos.logback.core.Appender<ILoggingEvent> appender = iter.next();
    this.logger.addAppender(appender);
  }
}

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

logbackLogger.iteratorForAppenders(); index.hasNext(); ) {
ch.qos.logback.core.Appender<ch.qos.logback.classic.spi.ILoggingEvent> appender = index.next();
if (appender instanceof ch.qos.logback.core.FileAppender) {

代码示例来源:origin: bwssytems/ha-bridge

/**
 * Get the log file.
 * 
 * @param logFileName The name of the log file 
 * @return The actual file
 */
public static File getLogFile(final String logFileName) {
  if (logFileName == null) {
    throw new IllegalArgumentException("logFileName cannot be null.");
  }
  final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
  final Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
  final Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();
  while (it.hasNext()) {
    final Appender<ILoggingEvent> appender = it.next();
    if (appender instanceof FileAppender) {
      final FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) appender;
      final File logFile = new File(fileAppender.getFile());
      if (logFile.getName().equalsIgnoreCase(logFileName)) {
        return logFile;
      }
    }
  }
  return null;
}

代码示例来源:origin: dropwizard/dropwizard

@Override
public void stop() {
  // Should acquire the lock to avoid concurrent listener changes
  CHANGE_LOGGER_CONTEXT_LOCK.lock();
  try {
    // We need to go through a list of appenders and locate the async ones,
    // as those could have messages left to write. Since there is no flushing
    // mechanism built into logback, we wait for a short period of time before
    // giving up that the appender will be completely flushed.
    final Logger logger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    final List<Appender<ILoggingEvent>> appenders = Lists.of(logger.iteratorForAppenders());
    for (Appender<ILoggingEvent> appender : appenders) {
      if (appender instanceof AsyncAppenderBase) {
        flushAppender((AsyncAppenderBase<?>) appender);
      } else if (appender instanceof AsyncAppenderBaseProxy) {
        flushAppender(((AsyncAppenderBaseProxy<?>) appender).getAppender());
      }
    }
  } catch (InterruptedException ignored) {
    // If the thread waiting for the logs to be flushed is aborted then
    // user clearly wants the application to quit now, so stop trying
    // to flush any appenders
    Thread.currentThread().interrupt();
  } finally {
    CHANGE_LOGGER_CONTEXT_LOCK.unlock();
  }
}

代码示例来源:origin: bwssytems/ha-bridge

/**
 * Get the logfile information for the roor logger.
 * 
 * @return List of LogFileInfo obejcts
 */
public static List<LogFileInfo> getLogFileInfos() {
  
  final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
  final List<LogFileInfo> logFileInfos = new ArrayList<LogFileInfo>();
  final Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
  final Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();
  while (it.hasNext()) {
    final Appender<ILoggingEvent> appender = it.next();
    if (appender instanceof FileAppender) {
      final FileAppender<ILoggingEvent> fileAppender = (FileAppender<ILoggingEvent>) appender;
      final File logFile = new File(fileAppender.getFile());
      final LogFileInfo logFileInfo = new LogFileInfo();
      logFileInfo.setFileName(logFile.getName());
      logFileInfo.setFileLastChanged(new Date(logFile.lastModified()));
      logFileInfo.setFileSize(logFile.length());
      logFileInfos.add(logFileInfo);
    }
  }
  return logFileInfos;
}

代码示例来源:origin: SonarSource/sonarqube

private static void verifyNoFileAppender(Logger logger) {
 Iterator<Appender<ILoggingEvent>> iterator = logger.iteratorForAppenders();
 while (iterator.hasNext()) {
  assertThat(iterator.next()).isNotInstanceOf(FileAppender.class);
 }
}

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

/**
   * When prudent is set to true, file appenders from multiple JVMs can safely write to the same file.
   * <p>
   * Only support by logback
   *
   * @param prudent
   * @since 0.1.8
   */
  public static void activePrudent(Logger logger, boolean prudent) {
    if (logger != null && logger.getDelegate() != null) {
      if (!(logger.getDelegate() instanceof ch.qos.logback.classic.Logger)) {
        throw new IllegalArgumentException("logger must be ch.qos.logback.classic.Logger, but it's "
          + logger.getDelegate().getClass());
      }

      Iterator<Appender<ILoggingEvent>> iter = ((ch.qos.logback.classic.Logger)logger.getDelegate())
        .iteratorForAppenders();
      while (iter.hasNext()) {
        ch.qos.logback.core.Appender<ILoggingEvent> appender = iter.next();
        if (appender instanceof FileAppender) {
          ((FileAppender)appender).setPrudent(prudent);
        } else {
          continue;
        }
      }
    }
  }
}

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

@Override
public void activateAsync(List<Object[]> args) {
  AsyncAppender asynAppender = new AsyncAppender();
  invokeMethod(asynAppender, args);
  asynAppender.setName(productName + "." + logger.getName() + ".AsyncAppender");
  asynAppender.setContext(LogbackLoggerContextUtil.getLoggerContext());
  Iterator<Appender<ILoggingEvent>> iterator = logger.iteratorForAppenders();
  boolean hasAppender = false;
  while (iterator.hasNext()) {
    hasAppender = true;
    asynAppender.addAppender(iterator.next());
  }
  if (!hasAppender) {
    throw new IllegalStateException("Activate async appender failed, no appender exist.");
  }
  asynAppender.start();
  iterator = logger.iteratorForAppenders();
  while (iterator.hasNext()) {
    logger.detachAppender(iterator.next());
  }
  logger.addAppender(asynAppender);
  setProductName(productName);
}

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

@SuppressFBWarnings("NP_BOOLEAN_RETURN_NULL")
public static Boolean setDepth(int depth) {
  if (Logback) {
    if (depth == -1) {
      depth = Integer.MAX_VALUE;
    }
    try {
      LoggerContext loggerContext = (LoggerContext)lcObject;
      List<Logger> loggers = loggerContext.getLoggerList();
      for (ch.qos.logback.classic.Logger logger : loggers) {
        Iterator<Appender<ILoggingEvent>> iter = logger.iteratorForAppenders();
        doSetDepth(iter, depth);
      }
    } catch (Throwable t) {
      LogLog.error("failed to set depth for logback", t);
      return false;
    }
    LogLog.info("set logback throwable depth success, depth: " + depth);
    return true;
  }
  return null;
}

代码示例来源:origin: gocd/gocd

private void assertPluginLogFile(String pluginId, String expectedPluginLogFileName) {
    SystemEnvironment systemEnvironment = mock(SystemEnvironment.class);

    DefaultPluginLoggingService loggingService = new DefaultPluginLoggingService(systemEnvironment);
    loggingService.debug(pluginId, "some-logger-name", "message");

    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("plugin." + pluginId);
    ArrayList<Appender<ILoggingEvent>> appenders = new ArrayList<>();
    logger.iteratorForAppenders().forEachRemaining(new Consumer<Appender<ILoggingEvent>>() {
      @Override
      public void accept(Appender<ILoggingEvent> iLoggingEventAppender) {
        appenders.add(iLoggingEventAppender);
      }
    });

    String loggingDirectory = loggingService.getCurrentLogDirectory();
    assertThat(appenders.size(), is(1));
    assertThat(new File(((FileAppender) appenders.get(0)).rawFileProperty()), is(new File(loggingDirectory, expectedPluginLogFileName)));
  }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void gobbler_logger_writes_to_console_without_formatting_when_running_from_command_line() {
 emulateRunFromCommandLine(false);
 LoggerContext ctx = underTest.configure();
 Logger gobblerLogger = ctx.getLogger(LOGGER_GOBBLER);
 verifyGobblerConsoleAppender(gobblerLogger);
 assertThat(gobblerLogger.iteratorForAppenders()).hasSize(1);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void gobbler_logger_writes_to_console_without_formatting_when_running_from_sonar_script() {
 emulateRunFromSonarScript();
 LoggerContext ctx = underTest.configure();
 Logger gobblerLogger = ctx.getLogger(LOGGER_GOBBLER);
 verifyGobblerConsoleAppender(gobblerLogger);
 assertThat(gobblerLogger.iteratorForAppenders()).hasSize(1);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void gobbler_logger_writes_to_console_without_formatting_when_running_from_ITs() {
 emulateRunFromCommandLine(true);
 LoggerContext ctx = underTest.configure();
 Logger gobblerLogger = ctx.getLogger(LOGGER_GOBBLER);
 verifyGobblerConsoleAppender(gobblerLogger);
 assertThat(gobblerLogger.iteratorForAppenders()).hasSize(1);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void root_logger_writes_to_console_with_formatting_and_to_sonar_log_file_when_running_from_ITs() {
 emulateRunFromCommandLine(true);
 LoggerContext ctx = underTest.configure();
 Logger rootLogger = ctx.getLogger(ROOT_LOGGER_NAME);
 verifyAppConsoleAppender(rootLogger.getAppender("APP_CONSOLE"));
 verifySonarLogFileAppender(rootLogger.getAppender("file_sonar"));
 assertThat(rootLogger.iteratorForAppenders()).hasSize(2);
 ctx.getLoggerList()
  .stream()
  .filter(logger -> !ROOT_LOGGER_NAME.equals(logger.getName()))
  .forEach(AppLoggingTest::verifyNoFileAppender);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void root_logger_writes_to_console_with_formatting_and_to_sonar_log_file_when_running_from_command_line() {
 emulateRunFromCommandLine(false);
 LoggerContext ctx = underTest.configure();
 Logger rootLogger = ctx.getLogger(ROOT_LOGGER_NAME);
 verifyAppConsoleAppender(rootLogger.getAppender("APP_CONSOLE"));
 verifySonarLogFileAppender(rootLogger.getAppender("file_sonar"));
 assertThat(rootLogger.iteratorForAppenders()).hasSize(2);
 // verify no other logger writes to sonar.log
 ctx.getLoggerList()
  .stream()
  .filter(logger -> !ROOT_LOGGER_NAME.equals(logger.getName()))
  .forEach(AppLoggingTest::verifyNoFileAppender);
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void root_logger_only_writes_to_console_with_formatting_when_running_from_sonar_script() {
 emulateRunFromSonarScript();
 LoggerContext ctx = underTest.configure();
 Logger rootLogger = ctx.getLogger(ROOT_LOGGER_NAME);
 ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) rootLogger.getAppender("APP_CONSOLE");
 verifyAppFormattedLogEncoder(consoleAppender.getEncoder());
 assertThat(rootLogger.iteratorForAppenders()).hasSize(1);
}

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

private static LoggerInfo doGetLoggerInfo(Logger logger) {
  LoggerInfo info = new LoggerInfo(logger.getName(), logger.isAdditive());
  Level level = logger.getLevel(), effectiveLevel = logger.getEffectiveLevel();
  if (level != null) {
    info.setLevel(level.toString());
  }
  if (effectiveLevel != null) {
    info.setEffectiveLevel(effectiveLevel.toString());
  }
  List<AppenderInfo> result = doGetLoggerAppenders(logger.iteratorForAppenders());
  info.setAppenders(result);
  return info;
}

代码示例来源:origin: jsevellec/cassandra-unit

private boolean hasAppenders(ch.qos.logback.classic.Logger logger)
{
  Iterator<Appender<ILoggingEvent>> it = logger.iteratorForAppenders();
  return it.hasNext();
}

相关文章