@Override public String toString() { return String.format("RotatingFileOutputStream{file=%s}", config.getFile()); }
private void unsafeRotate(RotationPolicy policy, LocalDateTime dateTime) throws Exception { if (config.getFile().length() == 0) { LOGGER.debug("empty file, skipping rotation {file={}}"); config.getCallback().onSuccess(policy, dateTime, null); LOGGER.debug("renaming {file={}, rotatedFile={}}", config.getFile(), rotatedFile); boolean renamed = config.getFile().renameTo(rotatedFile); if (!renamed) { String message = String.format("rename failure {file=%s, rotatedFile=%s}", config.getFile(), rotatedFile); IOException error = new IOException(message); config.getCallback().onFailure(policy, dateTime, rotatedFile, error); LOGGER.debug("re-opening file {file={}}", config.getFile()); FileOutputStream newStream = open(); FileOutputStream oldStream;
@Override public String toString() { return MoreObjects.toStringHelper(this) .add("file", rotationConfig.getFile()) .add("charset", charset) .toString(); }
public HttpRequestRecordWriterRotatingFileTarget(RotationConfig rotationConfig, Charset charset) { this.rotationConfig = checkNotNull(rotationConfig, "rotationConfig"); this.charset = checkNotNull(charset, "charset"); this.writer = createWriter(rotationConfig, charset); LOGGER.trace("instantiated (file={}, charset={})", rotationConfig.getFile(), charset); }
private static BufferedWriter createWriter(RotationConfig rotationConfig, Charset charset) { try { OutputStream outputStream = new RotatingFileOutputStream(rotationConfig); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, charset); return new BufferedWriter(outputStreamWriter); } catch (Exception error) { String message = String.format("failed opening file (file=%s, charset=%s)", rotationConfig.getFile(), charset); throw new RuntimeException(message, error); } }
@Override public void run() { // Get file size. LocalDateTime now = config.getClock().now(); File file = config.getFile(); long byteCount; try { byteCount = file.length(); } catch (Exception error) { String message = String.format("failed accessing file size (file=%s)", file); Exception extendedError = new IOException(message, error); config.getCallback().onFailure(SizeBasedRotationPolicy.this, now, file, extendedError); return; } // Rotate if necessary. if (byteCount > maxByteCount) { LOGGER.debug("triggering {byteCount={}}", byteCount); config.getCallback().onTrigger(SizeBasedRotationPolicy.this, now); rotatable.rotate(SizeBasedRotationPolicy.this, now); } } };