public class FileRotator extends Object
logrotate
. There is
a single "active" file, which is periodically rotated into historical files,
and eventually deleted entirely. Files are stored under a specific directory
with a well-known prefix.
Instead of manipulating files directly, users implement interfaces that
perform operations on InputStream
and OutputStream
. This
enables atomic rewriting of file contents in
rewriteActive(Rewriter, long)
.
Users must periodically call maybeRotate(long)
to perform actual
rotation. Not inherently thread safe.
Modifier and Type | Class and Description |
---|---|
static interface |
FileRotator.Reader
External class that reads data from a given
InputStream . |
static interface |
FileRotator.Rewriter
External class that reads existing data from given
InputStream ,
then writes any modified data to OutputStream . |
static interface |
FileRotator.Writer
External class that writes data to a given
OutputStream . |
Constructor and Description |
---|
FileRotator(File basePath,
String prefix,
long rotateAgeMillis,
long deleteAgeMillis)
Create a file rotator.
|
Modifier and Type | Method and Description |
---|---|
void |
combineActive(FileRotator.Reader reader,
FileRotator.Writer writer,
long currentTimeMillis)
Deprecated.
|
void |
deleteAll()
Delete all files managed by this rotator.
|
void |
dumpAll(OutputStream os)
Dump all files managed by this rotator for debugging purposes.
|
void |
maybeRotate(long currentTimeMillis)
Examine all files managed by this rotator, renaming or deleting if their
age matches the configured thresholds.
|
void |
readMatching(FileRotator.Reader reader,
long matchStartMillis,
long matchEndMillis)
Read any rotated data that overlap the requested time range.
|
void |
rewriteActive(FileRotator.Rewriter rewriter,
long currentTimeMillis)
Process currently active file, first reading any existing data, then
writing modified data.
|
void |
rewriteAll(FileRotator.Rewriter rewriter)
Process all files managed by this rotator, usually to rewrite historical
data.
|
public FileRotator(File basePath, String prefix, long rotateAgeMillis, long deleteAgeMillis)
basePath
- Directory under which all files will be placed.prefix
- Filename prefix used to identify this rotator.rotateAgeMillis
- Age in milliseconds beyond which an active file
may be rotated into a historical file.deleteAgeMillis
- Age in milliseconds beyond which a rotated file
may be deleted.public void deleteAll()
public void dumpAll(OutputStream os) throws IOException
IOException
public void rewriteActive(FileRotator.Rewriter rewriter, long currentTimeMillis) throws IOException
IOException
@Deprecated public void combineActive(FileRotator.Reader reader, FileRotator.Writer writer, long currentTimeMillis) throws IOException
IOException
public void rewriteAll(FileRotator.Rewriter rewriter) throws IOException
IOException
public void readMatching(FileRotator.Reader reader, long matchStartMillis, long matchEndMillis) throws IOException
IOException
public void maybeRotate(long currentTimeMillis)