Class RollingFileStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
This class borrows extensively from the java.util.logger.FileHandler class for its file handling ability and instead of handling logger messages it extends java.io.OutputStream.
A pattern consists of a string that includes the following special components that will be replaced at runtime:
- "/" the local pathname separator
- "%t" the system temporary directory
- "%h" the value of the "user.home" system property
- "%d" the value of the "derby.system.home" system property
- "%g" the generation number to distinguish rotated logs
- "%u" a unique number to resolve conflicts
- "%%" translates to a single percent sign "%"
Thus for example a pattern of "%t/java%g.log" with a count of 2 would typically cause files to be written on Solaris to /var/tmp/java0.log and /var/tmp/java1.log whereas on Windows 95 they would be typically written to C:\TEMP\java0.log and C:\TEMP\java1.log
Generation numbers follow the sequence 0, 1, 2, etc.
Normally the "%u" unique field is set to 0. However, if the FileHandler tries to open the filename and finds the file is currently in use by another process it will increment the unique number field and try again. This will be repeated until FileHandler finds a file name that is not currently in use. If there is a conflict and no "%u" field has been specified, it will be added at the end of the filename after a dot. (This will be after any automatically added generation number.)
Thus if three processes were all trying to output to fred%u.%g.txt then they might end up using fred0.0.txt, fred1.0.txt, fred2.0.txt as the first file in their rotating sequences.
Note that the use of unique ids to avoid conflicts is only guaranteed to work reliably when using a local disk file system.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
The append flag which indicates at creation time to append to an existing file or to always create a new oneprivate int
The rolling file count.private File[]
The array of File instance representing the rolling filesprivate int
private String
The lockfile nameprivate FileOutputStream
The output stream that is used as a lockprivate static final int
private RollingFileStream.MeteredStream
The underlying stream being written to that keeps track of how much has been writtenprivate String
The filename pattern. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a default RollingFileStream.RollingFileStream
(String pattern, int limit, int count, boolean append) Initialize a RollingFileStream to write to a set of files with optional append. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Invoked by the metered OutputStreamvoid
close()
Close all the files.private void
fileDelete
(File file) Delete a file in a privilege blockprivate boolean
fileExists
(File file) Check to see if a file exists in a privilege blockprivate long
fileLength
(File file) Get the length of a file in a privilege blockprivate boolean
fileRename
(File file1, File file2) Rename a file in a privilege blockprivate File
Generates and returns File from a patternprivate String
getSystemProperty
(String property) Gets a system property in a privileged blockprivate void
Opens a new file that and delegates it to a MeteredStreamprivate FileOutputStream
Opens a file in the privileged blockprivate void
Opens the output files files based on the configured pattern, limit, count, and append mode.private void
rotate()
Rotates the log files.void
write
(int b) Implements the write method of the OutputStream.Methods inherited from class java.io.OutputStream
flush, nullOutputStream, write, write
-
Field Details
-
meter
The underlying stream being written to that keeps track of how much has been written -
append
private boolean appendThe append flag which indicates at creation time to append to an existing file or to always create a new one -
limit
private int limit -
count
private int countThe rolling file count. This many files will be created before the oldest is removed and the files rolled. -
pattern
The filename pattern. -
lockFileName
The lockfile name -
lockStream
The output stream that is used as a lock -
files
The array of File instance representing the rolling files -
MAX_LOCKS
private static final int MAX_LOCKS- See Also:
-
locks
-
-
Constructor Details
-
RollingFileStream
Construct a default RollingFileStream. This will be configured entirely with default values:- pattern - %d/derby-%g.log (DERBY_HOME/derby-0.log)
- limit - 0 (unlimited)
- count - 1 (one file)
- append - false (overwrite and not append)
- Throws:
IOException
- if there are IO problems opening the files.SecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control")).NullPointerException
- if pattern property is an empty String.
-
RollingFileStream
public RollingFileStream(String pattern, int limit, int count, boolean append) throws IOException, SecurityException Initialize a RollingFileStream to write to a set of files with optional append. When (approximately) the given limit has been written to one file, another file will be opened. The output will cycle through a set of count files.- Parameters:
pattern
- the pattern for naming the output filelimit
- the maximum number of bytes to write to any one filecount
- the number of files to useappend
- specifies append mode- Throws:
IOException
- if there are IO problems opening the files.SecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control").IllegalArgumentException
- if limit < 0, or count < 1.IllegalArgumentException
- if pattern is an empty string
-
-
Method Details
-
write
Implements the write method of the OutputStream. This writes the value to the metered stream.- Specified by:
write
in classOutputStream
- Parameters:
b
- The value to write- Throws:
IOException
-
openFiles
Opens the output files files based on the configured pattern, limit, count, and append mode.- Throws:
IOException
-
generate
Generates and returns File from a pattern- Parameters:
pattern
- The filename patterngeneration
- The generation number used if there is a conflictunique
- The unique number to append to the filename- Returns:
- The File
- Throws:
IOException
-
rotate
Rotates the log files. The metered OutputStream is closed,the log files are rotated and then a new metered OutputStream is created.- Throws:
IOException
-
close
Close all the files.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
- Throws:
SecurityException
- if a security manager exists and if the caller does not have LoggingPermission("control").
-
getSystemProperty
Gets a system property in a privileged block- Parameters:
property
- The propety to get- Returns:
- The property value
-
openFile
Opens a file in the privileged block- Parameters:
filename
- The name of the file to openappend
- iftrue
open the file in append mode- Returns:
- The FileOutputStream for the file
- Throws:
IOException
-
fileExists
Check to see if a file exists in a privilege block- Parameters:
file
- The file to check- Returns:
true
if the file exists orfalse
otherwise
-
fileDelete
Delete a file in a privilege block- Parameters:
file
- The file to delete
-
fileRename
Rename a file in a privilege block- Parameters:
file1
- The file to renamefile2
- The file to rename it to- Returns:
true
if the file was renamed or false otherwise
-
fileLength
Get the length of a file in a privilege block- Parameters:
file
- The file to get the length of- Returns:
- The length of the file
-
open
Opens a new file that and delegates it to a MeteredStream- Parameters:
fname
- The name of the fileappend
- Iftrue
append to the existing file- Throws:
IOException
-
checkMeter
Invoked by the metered OutputStream- Throws:
IOException
-