qflib 0.98.1

de.qfs.lib.log
Class FileLogWriter

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

public class FileLogWriter
extends StreamLogWriter

An extension of the StreamLogWriter that writes LogEntries to a File.

The constructors leave three choices for how the file should be opened:

MODE_CREATE
Create a new file, overwriting existing files (the default).
MODE_APPEND
If the file already exists, append to it, otherwise create a new file.
MODE_UNIQUE
First make the filename unique by appending numbers after the basename, starting with 1, until an unused name is found. Then open that file. So the first time you create a FileLogWriter for the file myname.log, this will create myname1.log, the next time its myname2.log and so on.
The FileLogWriter has two modes of operation: it can simply write the logs to the file or it can close the file after each write, reopening it for the next write. This takes a little more time but shouldn't hurt too badly, since messages typically arrive in bunches.

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

Field Summary
protected  java.io.File file
          The log file to write to.
protected  boolean keepClosed
          Whether to close the log file after each write.
static int MODE_APPEND
          Append to a log file, creating a new one if necessary.
static int MODE_CREATE
          Create a new log file, overwriting existing files.
static int MODE_UNIQUE
          Create a new log file.
 
Fields inherited from class de.qfs.lib.log.StreamLogWriter
closed, format, mustClose, ps, pw
 
Constructor Summary
FileLogWriter(java.lang.String client, java.lang.String file)
          Create a new FileLogWriter that uses a DefaultLogFormat to write LogEntries to a file.
FileLogWriter(java.lang.String client, java.lang.String file, int mode, boolean keepClosed)
          Create a new FileLogWriter that uses a DefaultLogFormat to write LogEntries to a file.
FileLogWriter(java.lang.String client, java.lang.String file, int mode, boolean keepClosed, LogFormat format)
          Create a new FileLogWriter that writes LogEntries to a file.
FileLogWriter(java.lang.String client, java.lang.String file, LogFormat format)
          Create a new FileLogWriter that writes LogEntries to a file.
 
Method Summary
static LevelFilter logToFile(java.lang.String client, java.lang.String file, int mode, boolean keepClosed)
          Log messages to a log file by creating a LevelFilter with a FileLogWriter and adding it to the Log filter chain.
static LevelFilter logToFile(java.lang.String client, java.lang.String file, int mode, boolean keepClosed, LogFormat format)
          Log messages to a log file by creating a LevelFilter with a FileLogWriter and adding it to the Log filter chain.
protected  void openFile(java.lang.String client, java.lang.String filename, int mode)
          Open the file for writing.
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.StreamLogWriter
close, getFormat, setFormat
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MODE_CREATE

public static final int MODE_CREATE
Create a new log file, overwriting existing files.

MODE_APPEND

public static final int MODE_APPEND
Append to a log file, creating a new one if necessary.

MODE_UNIQUE

public static final int MODE_UNIQUE
Create a new log file. A number appended to the basename is incremented until the file name is unique.

file

protected java.io.File file
The log file to write to.

keepClosed

protected boolean keepClosed
Whether to close the log file after each write.
Constructor Detail

FileLogWriter

public FileLogWriter(java.lang.String client,
                     java.lang.String file)
              throws java.io.IOException
Create a new FileLogWriter 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.
Throws:
java.io.IOException - If the file cannot be opened for writing.

FileLogWriter

public FileLogWriter(java.lang.String client,
                     java.lang.String file,
                     int mode,
                     boolean keepClosed)
              throws java.io.IOException
Create a new FileLogWriter 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.
mode - How the file should be created. Must be one of MODE_CREATE, MODE_APPEND, or MODE_UNIQUE.
keepClosed - Whether to close the file after each write.
Throws:
java.io.IOException - If the file cannot be opened for writing.

FileLogWriter

public FileLogWriter(java.lang.String client,
                     java.lang.String file,
                     LogFormat format)
              throws java.io.IOException
Create a new FileLogWriter that writes LogEntries to a file.
Parameters:
client - Name of the client, used by qflog.
file - The name of the file to print to.
format - The format used to print LogEntries.
Throws:
java.io.IOException - If the file cannot be opened for writing.

FileLogWriter

public FileLogWriter(java.lang.String client,
                     java.lang.String file,
                     int mode,
                     boolean keepClosed,
                     LogFormat format)
              throws java.io.IOException
Create a new FileLogWriter that writes LogEntries to a file.
Parameters:
client - Name of the client, used by qflog.
file - The name of the file to print to.
mode - How the file should be created. Must be one of MODE_CREATE, MODE_APPEND, or MODE_UNIQUE.
keepClosed - Whether to close the file after each write.
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 mode,
                                    boolean keepClosed)
                             throws java.io.IOException
Log messages to a log file by creating a LevelFilter with a FileLogWriter 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.
mode - How the file should be created. Must be one of MODE_CREATE, MODE_APPEND, or MODE_UNIQUE.
keepClosed - Whether to close the file after each write.
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 mode,
                                    boolean keepClosed,
                                    LogFormat format)
                             throws java.io.IOException
Log messages to a log file by creating a LevelFilter with a FileLogWriter 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.
mode - How the file should be created. Must be one of MODE_CREATE, MODE_APPEND, or MODE_UNIQUE.
keepClosed - Whether to close the file after each write.
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.

write

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

write

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

openFile

protected void openFile(java.lang.String client,
                        java.lang.String filename,
                        int mode)
                 throws java.io.IOException
Open the file for writing.
Parameters:
client - Name of the client, used by qflog.
mode - How the file should be created. Must be one of MODE_CREATE, MODE_APPEND, or MODE_UNIQUE.
Throws:
java.io.IOException - If the file cannot be opened.

qflib 0.98.1