|
qflib 0.98.1 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--de.qfs.lib.log.StreamLogWriter | +--de.qfs.lib.log.FileLogWriter | +--de.qfs.lib.log.RingFileLogWriter
This class is similar to FileLogWriter
but limits the
size of the files it writes to. It keeps a limited number of log files in a
kind of ring buffer as follows:
The first time the RingFileLogWriter logs to a file, lets call it
myfile.log
, the file is created. When the size limit is
reached, it is renamed to myfile1.log
and a new file named
myfile.log
is created, to which the RingFileLogWriter now
sends its output. When this file fills up as well, myfile1.log
is renamed to myfile2.log
and myfile.log
is again
renamed to myfile1.log
. This continues until the file number
limit is reached (or ad infinitum if the limit is negative). Then every
time a new log file is created, the oldest log file (the one with the
highest number) is removed.
Field Summary | |
protected java.lang.String |
basename
The basename of the log file. |
protected java.lang.String |
client
The client name, needed for subsequent new log files. |
static int |
DEFAULT_FILE_LIMIT
Default file limit. |
static int |
DEFAULT_SIZE_LIMIT
Default size limit. |
protected java.io.File |
dir
The directory of the log files. |
protected java.lang.String |
ext
The extension of the log file (including '.'). |
protected int |
fileLimit
The maximum number of files to keep around. |
protected int |
sizeLimit
The maximum size of a log file (in kB). |
Fields inherited from class de.qfs.lib.log.FileLogWriter |
file, keepClosed, MODE_APPEND, MODE_CREATE, MODE_UNIQUE |
Fields inherited from class de.qfs.lib.log.StreamLogWriter |
closed, format, mustClose, ps, pw |
Constructor Summary | |
RingFileLogWriter(java.lang.String client,
java.lang.String file,
int sizeLimit,
int fileLimit)
Create a new RingFileLogWriter that uses a DefaultLogFormat to write LogEntries to a file. |
|
RingFileLogWriter(java.lang.String client,
java.lang.String file,
int sizeLimit,
int fileLimit,
LogFormat format)
Create a new RingFileLogWriter that writes LogEntries
to a file. |
Method Summary | |
protected void |
checkLimit()
Check whether the size limit is exceeded and act accordingly. |
int |
getFileLimit()
Get the number of old log files to keep. |
protected int |
getFileNum(java.lang.String file)
Get the number of a file. |
int |
getSizeLimit()
Get the file size limit (in kB) of the RingFileLogWriter. |
static LevelFilter |
logToFile(java.lang.String client,
java.lang.String file,
int sizeLimit,
int fileLimit)
Log messages to a log file by creating a LevelFilter with a RingFileLogWriter and adding it to the Log filter chain. |
static LevelFilter |
logToFile(java.lang.String client,
java.lang.String file,
int sizeLimit,
int fileLimit,
LogFormat format)
Log messages to a log file by creating a LevelFilter with a RingFileLogWriter and adding it to the Log filter chain. |
protected void |
moveFiles(int max)
Rename files, incrementing their number by 1. |
protected int |
removeFiles()
Delete files that will exceed the file limit. |
void |
setFileLimit(int fileLimit)
Get the number of old log files to keep. |
void |
setSizeLimit(int sizeLimit)
Set the file size limit (in kB) of the RingFileLogWriter. |
static void |
stopLogging()
Remove the last LevelFilter instance created with
logToFile from the filter chain and close the log
file. |
void |
write(LogEntry entry)
Write one LogEntry. |
void |
write(LogEntry[] entries)
Write an array of LogEntires in one go. |
Methods inherited from class de.qfs.lib.log.FileLogWriter |
logToFile, logToFile, openFile |
Methods inherited from class de.qfs.lib.log.StreamLogWriter |
close, getFormat, setFormat |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int DEFAULT_SIZE_LIMIT
public static final int DEFAULT_FILE_LIMIT
protected int sizeLimit
LogEntries
in write
.protected int fileLimit
protected java.lang.String client
protected java.lang.String basename
protected java.lang.String ext
protected java.io.File dir
Constructor Detail |
public RingFileLogWriter(java.lang.String client, java.lang.String file, int sizeLimit, int fileLimit) throws java.io.IOException
DefaultLogFormat
to write LogEntries
to a file.client
- Name of the client, used by qflog.file
- The name of the file to print to. If the file already
exists, append to it.sizeLimit
- The maximum size of a log file (in kB).fileLimit
- The maximum number of old log files to keep around.java.io.IOException
- If the file cannot be opened for writing.public RingFileLogWriter(java.lang.String client, java.lang.String file, int sizeLimit, int fileLimit, LogFormat format) throws java.io.IOException
LogEntries
to a file.client
- Name of the client, used by qflog.file
- The name of the file to print to. If the file already
exists, append to it.sizeLimit
- The maximum size of a log file (in kB).fileLimit
- The maximum number of old log files to keep around.format
- The format used to print LogEntries.java.io.IOException
- If the file cannot be opened for writing.Method Detail |
public static LevelFilter logToFile(java.lang.String client, java.lang.String file, int sizeLimit, int fileLimit) throws java.io.IOException
client
- Name of the client, used by qflog.file
- The name of the file to save in.sizeLimit
- The maximum size of a log file (in kB).fileLimit
- The maximum number of old log files to keep around.LevelFilter.setLevel
, it will log
all messages to the file. If you only call this method once in your
application, you can use stopLogging
to remove the
filter and close the writer, otherwise you have to save this reference
to the filter and clean up yourself.java.io.IOException
- If the file cannot be created.public static LevelFilter logToFile(java.lang.String client, java.lang.String file, int sizeLimit, int fileLimit, LogFormat format) throws java.io.IOException
client
- Name of the client, used by qflog.file
- The name of the file to save in.sizeLimit
- The maximum size of a log file (in kB).fileLimit
- The maximum number of old log files to keep around.format
- The format used to print LogEntries.LevelFilter.setLevel
, it will log
all messages to the file. If you only call this method once in your
application, you can use stopLogging
to remove the
filter and close the writer, otherwise you have to save this reference
to the filter and clean up yourself.java.io.IOException
- If the file cannot be created.public static void stopLogging()
LevelFilter
instance created with
logToFile
from the filter chain and close the log
file. Use this method if you only call logToFile
once in your application.public final int getSizeLimit()
public final void setSizeLimit(int sizeLimit)
sizeLimit
- The sizeLimit to set.public final int getFileLimit()
public final void setFileLimit(int fileLimit)
fileLimit
- The file limit to set.public void write(LogEntry entry)
write
in class FileLogWriter
entry
- The entry to write.public void write(LogEntry[] entries)
write(LogEntry)
, since it is more efficient.write
in class FileLogWriter
entries
- The entries to write.protected void checkLimit()
protected int removeFiles()
protected void moveFiles(int max)
max
- The maximim number to start at if umlimited files are
kept.protected int getFileNum(java.lang.String file)
file
- The file to check.
|
qflib 0.98.1 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |