public RotationConfig build() { prepare(); validate(); return new RotationConfig(this); }
private TimerTask createTimerTask(final Rotatable rotatable, final LocalDateTime triggerDateTime) { final RotationConfig config = rotatable.getConfig(); return new TimerTask() { @Override public void run() { getLogger().debug("triggering {triggerDateTime={}}", triggerDateTime); config.getCallback().onTrigger(TimeBasedRotationPolicy.this, triggerDateTime); rotatable.rotate(TimeBasedRotationPolicy.this, triggerDateTime); start(rotatable); } }; }
public RotatingFilePattern(String pattern, Locale locale) { this.pattern = pattern; this.locale = locale; this.fields = readPattern(pattern, locale); }
@Override public void run() { getLogger().debug("triggering {triggerDateTime={}}", triggerDateTime); config.getCallback().onTrigger(TimeBasedRotationPolicy.this, triggerDateTime); rotatable.rotate(TimeBasedRotationPolicy.this, triggerDateTime); start(rotatable); } };
@Override public void start(Rotatable rotatable) { RotationConfig config = rotatable.getConfig(); LocalDateTime triggerDateTime = getTriggerDateTime(config.getClock()); TimerTask timerTask = createTimerTask(rotatable, triggerDateTime); config.getTimer().schedule(timerTask, triggerDateTime.toDate()); }
@Override public void rotate(RotationPolicy policy, LocalDateTime dateTime) { boolean acquired = rotationLock.tryLock(); if (!acquired) { config.getCallback().onConflict(policy, dateTime); } else { try { unsafeRotate(policy, dateTime); } catch (Exception error) { String message = String.format("rotation failure {dateTime=%s}", dateTime); RuntimeException extendedError = new RuntimeException(message); config.getCallback().onFailure(policy, dateTime, null, extendedError); } finally { rotationLock.unlock(); } } }
@Override public void start(Rotatable rotatable) { TimerTask timerTask = createTimerTask(rotatable); rotatable.getConfig().getTimer().schedule(timerTask, 0, checkIntervalMillis); }
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 String toString() { return String.format("RotatingFileOutputStream{file=%s}", config.getFile()); }
private void startPolicies() { for (RotationPolicy policy : config.getPolicies()) { policy.start(this); } }
@Override public LocalDateTime midnight() { return currentDateTime().plusDays(1).withTimeAtStartOfDay().toLocalDateTime(); }
@Override public LocalDateTime getTriggerDateTime(Clock clock) { return clock.midnight(); }
public Builder filePattern(String filePattern) { this.filePattern = new RotatingFilePattern(filePattern); return this; }
@Override public LocalDateTime getTriggerDateTime(Clock clock) { return clock.sundayMidnight(); }
public static Builder builder() { return new Builder(); }
public File create(LocalDateTime dateTime) { StringBuilder pathNameBuilder = new StringBuilder(); for (Field field : fields) { field.render(pathNameBuilder, dateTime); } String pathName = pathNameBuilder.toString(); return new File(pathName); }
@Override public String toString() { return MoreObjects.toStringHelper(this) .add("file", rotationConfig.getFile()) .add("charset", charset) .toString(); }
private TimerTask createTimerTask(final Rotatable rotatable) { final RotationConfig config = rotatable.getConfig(); return new TimerTask() { @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); } } }; }
public RotatingFilePattern(String pattern) { this.pattern = pattern; this.locale = Locale.getDefault(); this.fields = readPattern(pattern, locale); }
@Override public LocalDateTime sundayMidnight() { DateTime today = currentDateTime(); int dayIndex = today.getDayOfWeek() - 1; int dayOffset = 7 - dayIndex; DateTime monday = today.plusDays(dayOffset); DateTime mondayStart = monday.withTimeAtStartOfDay(); return mondayStart.toLocalDateTime(); }