Source:
private synchronized void rollFile() throws IOException { LogFileName newFileName = null; Path newFile = null; OutputStream newOutputStream = null; for (int i = 0; i < 100; i++) { try { newFileName = LogFileName.newLogFileName(link, compressionType.getExtension()); newFile = link.resolveSibling(newFileName.getFileName()); newOutputStream = new BufferedOutputStream(Files.newOutputStream(newFile, CREATE_NEW), MAX_BATCH_BYTES); break; } catch (FileAlreadyExistsException ignore) { } } if (newOutputStream == null) { throw new IOException("Could not create new a unique log file"); } IOException exception = new IOException(String.format("Unable to %s log file", currentOutputStream == null ? "setup initial" : "roll")); if (currentOutputStream != null) { try { currentOutputStream.close(); } catch (IOException e) { exception.addSuppressed(new IOException("Unable to close old output stream", e)); } logHistoryManager.addFile(currentOutputFile, currentOutputFileName, currentFileSize); if (compressionExecutor != null) { Path originalFile = currentOutputFile; LogFileName originalLogFileName = currentOutputFileName; long originalFileSize = currentFileSize; compressionExecutor.submit(() -> compressInternal(originalFile, originalLogFileName, originalFileSize)); } } currentOutputFile = newFile; currentOutputFileName = newFileName; currentOutputStream = newOutputStream; currentFileSize = 0; try { if (Files.exists(link)) { Files.delete(link); } Files.createSymbolicLink(link, newFile); } catch (IOException e) { exception.addSuppressed(new IOException("Unable to update symlink", e)); } if (exception.getSuppressed().length > 0) { throw exception; } }

Target:
private synchronized void rollFile() throws IOException { LogFileName newFileName = null; Path newFile = null; OutputStream newOutputStream = null; for (int i = 0; i < MAX_OPEN_NEW_LOG_ATTEMPTS; i++) { try { newFileName = LogFileName.generateNextLogFileName(symlink, compressionType.getExtension()); newFile = symlink.resolveSibling(newFileName.getFileName()); newOutputStream = new BufferedOutputStream(Files.newOutputStream(newFile, CREATE_NEW), MAX_BATCH_BYTES); break; } catch (FileAlreadyExistsException ignore) { } } if (newOutputStream == null) { throw new IOException("Could not create new a unique log file: " + newFile); } IOException exception = new IOException(String.format("Unable to %s log file", currentOutputStream == null ? "setup initial" : "roll")); if (currentOutputStream != null) { try { currentOutputStream.close(); } catch (IOException e) { exception.addSuppressed(new IOException("Unable to close old output stream: " + currentOutputFile, e)); } historyManager.addFile(currentOutputFile, currentOutputFileName, currentFileSize); if (compressionExecutor != null) { Path originalFile = currentOutputFile; LogFileName originalLogFileName = currentOutputFileName; long originalFileSize = currentFileSize; compressionExecutor.submit(() -> compressInternal(originalFile, originalLogFileName, originalFileSize)); } } currentOutputFile = newFile; currentOutputFileName = newFileName; currentOutputStream = newOutputStream; currentFileSize = 0; try { if (Files.exists(symlink)) { Files.delete(symlink); } Files.createSymbolicLink(symlink, newFile); } catch (IOException e) { exception.addSuppressed(new IOException("Unable to update symlink", e)); } if (exception.getSuppressed().length > 0) { throw exception; } }