private synchronized void switchFileIfNeeded(Path file, String fileName) throws IOException {
        if (file.toFile().exists()
                && (entryCounter.get() >= maxEntriesPerFile || firstWriteTimestamp.get().plus(maxDelayAfterFirstWrite).isBefore(clock.instant()))) {
            Path target = spoolPath.resolve(readyPath).resolve(file.relativize(file)).resolve(fileName);
            log.log(Level.INFO, "Finished writing file " + file + " with " + entryCounter.get() + " entries, moving it to " + target);
            Files.move(file, target);
            entryCounter.set(1);
            fileCounter.incrementAndGet();
            fileNameBase.set(newFileNameBase(clock));
            firstWriteTimestamp.set(Instant.EPOCH);
        }
    }
Spooler that will write an entry to a file and read files that are ready to be sent.
Files are written in JSON Lines text file format.

@author hmusum
container-search/src/main/java/com/yahoo/search/logging/Spooler.java