public class RolloverFileOutputStream
extends java.io.OutputStream
This output stream puts content in a file that is rolled over every 24 hours. The filename must include the string "yyyy_mm_dd", which is replaced with the actual date when creating and rolling over the file.
Old files are retained for a number of days before being deleted.
Modifier and Type | Class and Description |
---|---|
private class |
RolloverFileOutputStream.RollTask |
Modifier and Type | Field and Description |
---|---|
private static java.util.Timer |
__rollover |
private boolean |
_append |
private java.io.File |
_file |
private java.text.SimpleDateFormat |
_fileBackupFormat |
private java.text.SimpleDateFormat |
_fileDateFormat |
private java.lang.String |
_filename |
private java.io.OutputStream |
_out |
private int |
_retainDays |
private RolloverFileOutputStream.RollTask |
_rollTask |
(package private) static java.lang.String |
ROLLOVER_FILE_BACKUP_FORMAT |
(package private) static java.lang.String |
ROLLOVER_FILE_DATE_FORMAT |
(package private) static int |
ROLLOVER_FILE_RETAIN_DAYS |
(package private) static java.lang.String |
YYYY_MM_DD |
Constructor and Description |
---|
RolloverFileOutputStream(java.lang.String filename) |
RolloverFileOutputStream(java.lang.String filename,
boolean append) |
RolloverFileOutputStream(java.lang.String filename,
boolean append,
int retainDays) |
RolloverFileOutputStream(java.lang.String filename,
boolean append,
int retainDays,
java.util.TimeZone zone) |
RolloverFileOutputStream(java.lang.String filename,
boolean append,
int retainDays,
java.util.TimeZone zone,
java.lang.String dateFormat,
java.lang.String backupFormat) |
RolloverFileOutputStream(java.lang.String filename,
boolean append,
int retainDays,
java.util.TimeZone zone,
java.lang.String dateFormat,
java.lang.String backupFormat,
java.time.ZonedDateTime now) |
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
flush() |
java.lang.String |
getDatedFilename() |
java.lang.String |
getFilename() |
int |
getRetainDays() |
(package private) void |
removeOldFiles(java.time.ZonedDateTime now) |
private void |
renameFile(java.io.File src,
java.io.File dest) |
protected void |
rollover(java.io.File oldFile,
java.io.File backupFile,
java.io.File newFile)
This method is called whenever a log file is rolled over
|
private void |
scheduleNextRollover(java.time.ZonedDateTime now) |
(package private) void |
setFile(java.time.ZonedDateTime now) |
static java.time.ZonedDateTime |
toMidnight(java.time.ZonedDateTime now)
Get the "start of day" for the provided DateTime at the zone specified.
|
void |
write(byte[] buf) |
void |
write(byte[] buf,
int off,
int len) |
void |
write(int b) |
private static java.util.Timer __rollover
static final java.lang.String YYYY_MM_DD
static final java.lang.String ROLLOVER_FILE_DATE_FORMAT
static final java.lang.String ROLLOVER_FILE_BACKUP_FORMAT
static final int ROLLOVER_FILE_RETAIN_DAYS
private java.io.OutputStream _out
private RolloverFileOutputStream.RollTask _rollTask
private java.text.SimpleDateFormat _fileBackupFormat
private java.text.SimpleDateFormat _fileDateFormat
private java.lang.String _filename
private java.io.File _file
private boolean _append
private int _retainDays
public RolloverFileOutputStream(java.lang.String filename) throws java.io.IOException
filename
- The filename must include the string "yyyy_mm_dd",
which is replaced with the actual date when creating and rolling over the file.java.io.IOException
- if unable to create outputpublic RolloverFileOutputStream(java.lang.String filename, boolean append) throws java.io.IOException
filename
- The filename must include the string "yyyy_mm_dd",
which is replaced with the actual date when creating and rolling over the file.append
- If true, existing files will be appended to.java.io.IOException
- if unable to create outputpublic RolloverFileOutputStream(java.lang.String filename, boolean append, int retainDays) throws java.io.IOException
filename
- The filename must include the string "yyyy_mm_dd",
which is replaced with the actual date when creating and rolling over the file.append
- If true, existing files will be appended to.retainDays
- The number of days to retain files before deleting them. 0 to retain forever.java.io.IOException
- if unable to create outputpublic RolloverFileOutputStream(java.lang.String filename, boolean append, int retainDays, java.util.TimeZone zone) throws java.io.IOException
filename
- The filename must include the string "yyyy_mm_dd",
which is replaced with the actual date when creating and rolling over the file.append
- If true, existing files will be appended to.retainDays
- The number of days to retain files before deleting them. 0 to retain forever.zone
- the timezone for the outputjava.io.IOException
- if unable to create outputpublic RolloverFileOutputStream(java.lang.String filename, boolean append, int retainDays, java.util.TimeZone zone, java.lang.String dateFormat, java.lang.String backupFormat) throws java.io.IOException
filename
- The filename must include the string "yyyy_mm_dd",
which is replaced with the actual date when creating and rolling over the file.append
- If true, existing files will be appended to.retainDays
- The number of days to retain files before deleting them. 0 to retain forever.zone
- the timezone for the outputdateFormat
- The format for the date file substitution. The default is "yyyy_MM_dd". If set to the
empty string, the file is rolledover to the same filename, with the current file being renamed to the backup filename.backupFormat
- The format for the file extension of backup files. The default is "HHmmssSSS".java.io.IOException
- if unable to create outputRolloverFileOutputStream(java.lang.String filename, boolean append, int retainDays, java.util.TimeZone zone, java.lang.String dateFormat, java.lang.String backupFormat, java.time.ZonedDateTime now) throws java.io.IOException
java.io.IOException
public static java.time.ZonedDateTime toMidnight(java.time.ZonedDateTime now)
now
- the date time to calculate fromprivate void scheduleNextRollover(java.time.ZonedDateTime now)
public java.lang.String getFilename()
public java.lang.String getDatedFilename()
public int getRetainDays()
void setFile(java.time.ZonedDateTime now) throws java.io.IOException
java.io.IOException
private void renameFile(java.io.File src, java.io.File dest) throws java.io.IOException
java.io.IOException
protected void rollover(java.io.File oldFile, java.io.File backupFile, java.io.File newFile)
oldFile
- The original filename or null if this is the first creationbackupFile
- The backup filename or null if the filename is dated.newFile
- The new filename that is now being used for loggingvoid removeOldFiles(java.time.ZonedDateTime now)
public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] buf) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] buf, int off, int len) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException