private void rollIfNeeded()

in metrics-core/src/main/java/software/amazon/swage/metrics/record/file/RollingFileWriter.java [139:177]


    private void rollIfNeeded() throws IOException {
        // Do not roll unless record is complete, as indicated by flush
        if (!flushed) return;
        flushed = false;

        // If we have not yet passed the roll over mark do nothing
        Instant now = Instant.now();
        if (now.isBefore(rollAt)) {
            return;
        }

        // New file time, may not be the rollAt time if one or more intervals
        // have passed without anything being written
        Instant rollTime = now.truncatedTo(rollInterval);

        // Determine the name of the file that will be written to
        String name = this.baseName + format.format(LocalDateTime.ofInstant(rollTime, timeZone));
        this.outPath = this.directory.resolve(name);

        // Finish writing to previous log
        if (writer != null) {
            writer.flush();
            writer.close();
        }

        // Ensure the parent directory always exists, even if it was removed out from under us.
        // A no-op if the directory already exists.
        Files.createDirectories(outPath.getParent());

        // Create new file, set it as our write destination
        writer = Files.newBufferedWriter(
                    outPath,
                    StandardCharsets.UTF_8,
                    StandardOpenOption.CREATE,
                    StandardOpenOption.APPEND);

        // Point to the next time we want to roll the log, update rollAt
        this.rollAt = rollTime.plus(1, rollInterval);
    }