return new SuffixRotator(suffix, suffix, "", CompressionType.NONE); return new SuffixRotator(suffix, datePattern, compressionSuffix, compressionType);
if (compressionType == CompressionType.GZIP) { try { archiveGzip(source, target); } catch (Exception e) { errorManager.error(String.format("Failed to compress %s to %s. Compressed file may be left on the " + archiveZip(source, target); } catch (Exception e) { errorManager.error(String.format("Failed to compress %s to %s. Compressed file may be left on the " + move(errorManager, source, target);
/** * Rotates the file to a new file appending the suffix to the target. If a date suffix was specified the suffix * will be added before the index or compression suffix. The current date will be used for the suffix. * <p> * If the {@code maxBackupIndex} is greater than 0 previously rotated files will be moved to an numerically * incremented target. The compression suffix, if required, will be appended to this indexed file name. * </p> * * @param errorManager the error manager used to report errors to, if {@code null} an {@link IOException} will * be thrown * @param source the file to be rotated * @param maxBackupIndex the number of backups to keep */ void rotate(final ErrorManager errorManager, final Path source, final int maxBackupIndex) { if (formatter == null) { rotate(errorManager, source, "", maxBackupIndex); } else { final String suffix; synchronized (formatter) { suffix = formatter.format(new Date()); } rotate(errorManager, source, suffix, maxBackupIndex); } }
final SuffixRotator suffixRotator = SuffixRotator.parse(suffix); final String dateSuffix = suffixRotator.getDatePattern(); final SimpleDateFormat format = new SimpleDateFormat(dateSuffix); format.setTimeZone(timeZone);
/** * Returns the suffix set to be appended to files during rotation. * * @return the suffix or {@code null} if no suffix should be used */ public String getSuffix() { if (suffixRotator == SuffixRotator.EMPTY) { return null; } return suffixRotator.toString(); }
/** * Sets the suffix to be appended to the file name during the file rotation. The suffix does not play a role in * determining when the file should be rotated. * <p/> * The suffix must be a string understood by the {@link java.text.SimpleDateFormat}. Optionally the suffix can end * with {@code .gz} or {@code .zip} which will compress the file on rotation. * <p/> * <p> * If the suffix ends with {@code .gz} or {@code .zip} the file will be compressed on rotation. * </p> * <p> * <b>Note:</b> The {@link #setMaxBackupIndex(int) maxBackupIndex} only takes into account files rotated with same * suffix. For example if the suffix pattern is {@code .yyyy-MM-dd} and the size rotation is reached only files * with the same date suffix will be purged. A file the day before or after will not be purged. * </p> * * @param suffix the suffix to place after the filename when the file is rotated */ public void setSuffix(final String suffix) { checkAccess(this); synchronized (outputLock) { this.suffixRotator = SuffixRotator.parse(suffix); } }
/** * {@inheritDoc} * * @throws RuntimeException if there is an attempt to rotate file and the rotation fails */ public void setFile(final File file) throws FileNotFoundException { synchronized (outputLock) { // Check for a rotate if (rotateOnBoot && maxBackupIndex > 0 && file != null && file.exists() && file.length() > 0L) { suffixRotator.rotate(getErrorManager(), file.toPath(), maxBackupIndex); } super.setFile(file); if (outputStream != null) outputStream.currentSize = file == null ? 0L : file.length(); } }
/** * {@inheritDoc} * * @throws RuntimeException if there is an attempt to rotate file and the rotation fails */ @Override public void setFile(final File file) throws FileNotFoundException { synchronized (outputLock) { // Check for a rotate if (rotateOnBoot && maxBackupIndex > 0 && file != null && file.exists() && file.length() > 0L) { final String suffix = getNextSuffix(); final SuffixRotator suffixRotator = getSuffixRotator(); if (suffixRotator != SuffixRotator.EMPTY && suffix != null) { suffixRotator.rotate(getErrorManager(), file.toPath(), suffix, maxBackupIndex); } } super.setFile(file); if (outputStream != null) outputStream.currentSize = file == null ? 0L : file.length(); } }
private void rollOver() { try { final File file = getFile(); // first, close the original file (some OSes won't let you move/rename a file that is open) setFile(null); // next, rotate it suffixRotator.rotate(getErrorManager(), file.toPath(), nextSuffix); // start new file setFile(file); } catch (IOException e) { reportError("Unable to rotate log file", e, ErrorManager.OPEN_FAILURE); } }
/** {@inheritDoc} */ protected void preWrite(final ExtLogRecord record) { final int maxBackupIndex = this.maxBackupIndex; final long currentSize = (outputStream == null ? Long.MIN_VALUE : outputStream.currentSize); if (currentSize > rotateSize && maxBackupIndex > 0) { try { final File file = getFile(); if (file == null) { // no file is set; a direct output stream or writer was specified return; } // close the old file. setFile(null); suffixRotator.rotate(getErrorManager(), file.toPath(), maxBackupIndex); // start with new file. setFile(file); } catch (IOException e) { reportError("Unable to rotate log file", e, ErrorManager.OPEN_FAILURE); } } } }
@Override protected void preWrite(final ExtLogRecord record) { super.preWrite(record); final int maxBackupIndex = this.maxBackupIndex; final long currentSize = (outputStream == null ? Long.MIN_VALUE : outputStream.currentSize); if (currentSize > rotateSize && maxBackupIndex > 0) { try { final File file = getFile(); if (file == null) { // no file is set; a direct output stream or writer was specified return; } // close the old file. setFile(null); getSuffixRotator().rotate(getErrorManager(), file.toPath(), getNextSuffix(), maxBackupIndex); // start with new file. setFile(file); } catch (IOException e) { reportError("Unable to rotate log file", e, ErrorManager.OPEN_FAILURE); } } } }