qflib 0.98.1

de.qfs.lib.log
Class RingFileLogWriter

java.lang.Object
  |
  +--de.qfs.lib.log.StreamLogWriter
        |
        +--de.qfs.lib.log.FileLogWriter
              |
              +--de.qfs.lib.log.RingFileLogWriter
All Implemented Interfaces:
LogWriter

public class RingFileLogWriter
extends FileLogWriter

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.

Since:
0.98.0
Version:
$Revision: 1.1 $
Author:
Gregor Schmid

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

DEFAULT_SIZE_LIMIT

public static final int DEFAULT_SIZE_LIMIT
Default size limit.

DEFAULT_FILE_LIMIT

public static final int DEFAULT_FILE_LIMIT
Default file limit.

sizeLimit

protected int sizeLimit
The maximum size of a log file (in kB). Actually files may get quite a bit larger than this, since the size is only checked after writing the whole array of LogEntries in write.

fileLimit

protected int fileLimit
The maximum number of files to keep around. Set this to -1 to keep an unlimited number of log files.

client

protected java.lang.String client
The client name, needed for subsequent new log files.

basename

protected java.lang.String basename
The basename of the log file.

ext

protected java.lang.String ext
The extension of the log file (including '.').

dir

protected java.io.File dir
The directory of the log files.
Constructor Detail

RingFileLogWriter

public RingFileLogWriter(java.lang.String client,
                         java.lang.String file,
                         int sizeLimit,
                         int fileLimit)
                  throws java.io.IOException
Create a new RingFileLogWriter that uses a DefaultLogFormat to write LogEntries to a file.
Parameters:
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.
Throws:
java.io.IOException - If the file cannot be opened for writing.

RingFileLogWriter

public RingFileLogWriter(java.lang.String client,
                         java.lang.String file,
                         int sizeLimit,
                         int fileLimit,
                         LogFormat format)
                  throws java.io.IOException
Create a new RingFileLogWriter that writes LogEntries to a file.
Parameters:
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.
Throws:
java.io.IOException - If the file cannot be opened for writing.
Method Detail

logToFile

public static LevelFilter logToFile(java.lang.String client,
                                    java.lang.String file,
                                    int sizeLimit,
                                    int fileLimit)
                             throws java.io.IOException
Log messages to a log file by creating a LevelFilter with a RingFileLogWriter and adding it to the Log filter chain. The log file will be in the format recognized by the qflog log server.
Parameters:
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.
Returns:
The new LevelFilter. Unless you change its level via 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.
Throws:
java.io.IOException - If the file cannot be created.

logToFile

public static LevelFilter logToFile(java.lang.String client,
                                    java.lang.String file,
                                    int sizeLimit,
                                    int fileLimit,
                                    LogFormat format)
                             throws java.io.IOException
Log messages to a log file by creating a LevelFilter with a RingFileLogWriter and adding it to the Log filter chain. The log file will be in the format recognized by the qflog log server.
Parameters:
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.
Returns:
The new LevelFilter. Unless you change its level via 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.
Throws:
java.io.IOException - If the file cannot be created.

stopLogging

public static void stopLogging()
Remove the last 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.

getSizeLimit

public final int getSizeLimit()
Get the file size limit (in kB) of the RingFileLogWriter.
Returns:
The sizeLimit of the RingFileLogWriter.

setSizeLimit

public final void setSizeLimit(int sizeLimit)
Set the file size limit (in kB) of the RingFileLogWriter. This causes an immediate check of the current file size.
Parameters:
sizeLimit - The sizeLimit to set.

getFileLimit

public final int getFileLimit()
Get the number of old log files to keep.
Returns:
The number of old log files to keep (negative means umlimited)

setFileLimit

public final void setFileLimit(int fileLimit)
Get the number of old log files to keep. A negative value means no limit.
Parameters:
fileLimit - The file limit to set.

write

public void write(LogEntry entry)
Write one LogEntry.
Overrides:
write in class FileLogWriter
Parameters:
entry - The entry to write.

write

public void write(LogEntry[] entries)
Write an array of LogEntires in one go. Clients of the RingFileLogWriter should use this method in preference to write(LogEntry), since it is more efficient.
Overrides:
write in class FileLogWriter
Parameters:
entries - The entries to write.

checkLimit

protected void checkLimit()
Check whether the size limit is exceeded and act accordingly.

removeFiles

protected int removeFiles()
Delete files that will exceed the file limit.
Returns:
The maximum number of the files left on disk.

moveFiles

protected void moveFiles(int max)
Rename files, incrementing their number by 1.
Parameters:
max - The maximim number to start at if umlimited files are kept.

getFileNum

protected int getFileNum(java.lang.String file)
Get the number of a file.
Parameters:
file - The file to check.
Returns:
The files number if it starts with basename and ends with ext or -1 if it doesn't match.

qflib 0.98.1