public void dumpLogs(String level, int timePeriod)
      throws YarnRuntimeException, IOException {
    synchronized (lock){
      if (logFlag) {
        LOG.info("Attempt to dump logs when appender is already running");
        throw new YarnRuntimeException("Appender is already dumping logs");
      }
      Level targetLevel = Level.toLevel(level);
      Logger logger = LogManager.getLogger(name);
      appenderLevels.clear();
      currentLogLevel = logger.getLevel();
      Level currentEffectiveLevel = logger.getEffectiveLevel();

      // make sure we can create the appender first
      Layout layout = new PatternLayout("%d{ISO8601} %p %c: %m%n");
      FileAppender fApp;
      File file =
          new File(System.getProperty("yarn.log.dir"), targetFilename);
      try {
        fApp = new FileAppender(layout, file.getAbsolutePath(), false);
      } catch (IOException ie) {
        LOG.warn("Error creating file, can't dump logs to "
            + file.getAbsolutePath(), ie);
        throw ie;
      }
      fApp.setName(AdHocLogDumper.AD_HOC_DUMPER_APPENDER);
      fApp.setThreshold(targetLevel);

      // get current threshold of all appenders and set it to the effective
      // level
      for (Enumeration appenders = Logger.getRootLogger().getAllAppenders();
          appenders.hasMoreElements();) {
        Object obj = appenders.nextElement();
        if (obj instanceof AppenderSkeleton) {
          AppenderSkeleton appender = (AppenderSkeleton) obj;
          appenderLevels.put(appender.getName(), appender.getThreshold());
          appender.setThreshold(currentEffectiveLevel);
        }
      }

      logger.addAppender(fApp);
      LOG.info("Dumping adhoc logs for " + name + " to "
          + file.getAbsolutePath() + " for " + timePeriod + " milliseconds");
      logger.setLevel(targetLevel);
      logFlag = true;

      TimerTask restoreLogLevel = new RestoreLogLevel();
      Timer restoreLogLevelTimer = new Timer();
      restoreLogLevelTimer.schedule(restoreLogLevel, timePeriod);
    }
  }
@Test
  void testDumpingSchedulerLogs() throws Exception {

    Map<Appender, Priority> levels = new HashMap<>();
    String logFilename = "test.log";
    Logger logger = LoggerFactory.getLogger(TestAdHocLogDumper.class);
    if (isLog4jLogger(this.getClass())) {
      for (Enumeration appenders =
           LogManager.getRootLogger().getAllAppenders(); appenders.hasMoreElements();) {
        Object obj = appenders.nextElement();
        if (obj instanceof AppenderSkeleton) {
          AppenderSkeleton appender = (AppenderSkeleton) obj;
          levels.put(appender, appender.getThreshold());
        }
      }
    }

    AdHocLogDumper dumper = new AdHocLogDumper(this.getClass().getName(),
        logFilename);
    dumper.dumpLogs("DEBUG", 1000);
    LOG.debug("test message 1");
    LOG.info("test message 2");
    File logFile = new File(logFilename);
    assertTrue(logFile.exists());
    Thread.sleep(2000);
    long lastWrite = logFile.lastModified();
    assertTrue(lastWrite < Time.now());
    assertTrue(logFile.length() != 0);

    // make sure levels are set back to their original values
    if (isLog4jLogger(this.getClass())) {
      for (Enumeration appenders = LogManager.getRootLogger().
          getAllAppenders(); appenders.hasMoreElements();) {
        Object obj = appenders.nextElement();
        if (obj instanceof AppenderSkeleton) {
          AppenderSkeleton appender = (AppenderSkeleton) obj;
          assertEquals(levels.get(appender), appender.getThreshold());
        }
      }
    }
    boolean del = logFile.delete();
    if (!del) {
      LOG.info("Couldn't clean up after test");
    }
  }