code: public void streamConsoleOutput(final BuildConsoleStreamListener listener, final int poolingInterval, final int poolingTimeout, boolean crumbFlag) throws InterruptedException, IOException {
          // Calculate start and timeout
          final long startTime = System.currentTimeMillis();
          final long timeoutTime = startTime + (poolingTimeout * 1000);
  
          int bufferOffset = 0;
          while (true) {
              Thread.sleep(poolingInterval * 1000);
  
              ConsoleLog consoleLog = null;
              consoleLog = getConsoleOutputText(bufferOffset, crumbFlag);
              String logString = consoleLog.getConsoleLog();
              if (logString != null && !logString.isEmpty()) {
                  listener.onData(logString);
              }
              if (consoleLog.getHasMoreData()) {
                  bufferOffset = consoleLog.getCurrentBufferSize();
              } else {
                  listener.finished();
                  break;
              }
              long currentTime = System.currentTimeMillis();
  
              if (currentTime > timeoutTime) {
                  LOGGER.warn("Pooling for build {0} for {2} timeout! Check if job stuck in jenkins",
                          BuildWithDetails.this.getDisplayName(), BuildWithDetails.this.getNumber());
                  break;
              }
          }
      }
docs: Stream build console output log as text using BuildConsoleStreamListener
  Method can be used to asynchronously obtain logs for running build.
  
  @param listener interface used to asynchronously obtain logs
  @param poolingInterval interval (seconds) used to pool jenkins for logs
  @param poolingTimeout pooling timeout (seconds) used to break pooling in case build stuck
  @throws InterruptedException in case of an error.
  @throws IOException in case of an error.