Class FileHandler
public class FileHandler extends StreamHandler
FileHandler writes logging records into a specified file or a
rotating set of files.
When a set of files is used and a given amount of data has been written to one file, then this file is closed and another file is opened. The name of these files are generated by given name pattern, see below for details. When the files have all been filled the Handler returns to the first and goes through the set again.
By default, the I/O buffering mechanism is enabled, but when each log record is complete, it is flushed out.
XMLFormatter is the default formatter for FileHandler.
FileHandler reads the following LogManager properties for
initialization; if a property is not defined or has an invalid value, a
default value is used.
- java.util.logging.FileHandler.append specifies whether this
FileHandlershould append onto existing files, defaults tofalse. - java.util.logging.FileHandler.count specifies how many output files to rotate, defaults to 1.
- java.util.logging.FileHandler.filter specifies the
Filterclass name, defaults to noFilter. - java.util.logging.FileHandler.formatter specifies the
Formatterclass, defaults tojava.util.logging.XMLFormatter. - java.util.logging.FileHandler.encoding specifies the character set encoding name, defaults to the default platform encoding.
- java.util.logging.FileHandler.level specifies the level for this
Handler, defaults toLevel.ALL. - java.util.logging.FileHandler.limit specifies the maximum number of bytes to write to any one file, defaults to zero, which means no limit.
- java.util.logging.FileHandler.pattern specifies name pattern for the output files. See below for details. Defaults to "%h/java%u.log".
Name pattern is a string that may include some special substrings, which will be replaced to generate output files:
- "/" represents the local pathname separator
- "%g" represents the generation number to distinguish rotated logs
- "%h" represents the home directory of the current user, which is specified by "user.home" system property
- "%t" represents the system's temporary directory
- "%u" represents a unique number to resolve conflicts
- "%%" represents the percent sign character '%'
Normally, the generation numbers are not larger than the given file count and follow the sequence 0, 1, 2.... If the file count is larger than one, but the generation field("%g") has not been specified in the pattern, then the generation number after a dot will be added to the end of the file name.
The "%u" unique field is used to avoid conflicts and is set to 0 at first. If
one FileHandler tries to open the filename which is currently in use
by another process, it will repeatedly increment the unique number field and
try again. If the "%u" component has not been included in the file name
pattern and some contention on a file does occur, then a unique numerical
value will be added to the end of the filename in question immediately to the
right of a dot. The generation of unique IDs for avoiding conflicts is only
guaranteed to work reliably when using a local disk file system.
-
Constructor Summary
Constructors Constructor Description FileHandler()Construct aFileHandlerusingLogManagerproperties or their default value.FileHandler(String pattern)Constructs a newFileHandler.FileHandler(String pattern, boolean append)Construct a newFileHandler.FileHandler(String pattern, int limit, int count)Construct a newFileHandler.FileHandler(String pattern, int limit, int count, boolean append)Construct a newFileHandler. -
Method Summary
Methods inherited from class java.util.logging.StreamHandler
flush, isLoggable, setEncoding, setOutputStreamMethods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, reportError, setErrorManager, setFilter, setFormatter, setLevel
-
Constructor Details
-
FileHandler
Construct aFileHandlerusingLogManagerproperties or their default value.- Throws:
IOException- if any I/O error occurs.
-
FileHandler
Constructs a newFileHandler. The given name pattern is used as output filename, the file limit is set to zero (no limit), the file count is set to one; the remaining configuration is done usingLogManagerproperties or their default values. This handler writes to only one file with no size limit.- Parameters:
pattern- the name pattern for the output file.- Throws:
IOException- if any I/O error occurs.IllegalArgumentException- if the pattern is empty.NullPointerException- if the pattern isnull.
-
FileHandler
Construct a newFileHandler. The given name pattern is used as output filename, the file limit is set to zero (no limit), the file count is initialized to one and the value ofappendbecomes the new instance's append mode. The remaining configuration is done usingLogManagerproperties. This handler writes to only one file with no size limit.- Parameters:
pattern- the name pattern for the output file.append- the append mode.- Throws:
IOException- if any I/O error occurs.IllegalArgumentException- ifpatternis empty.NullPointerException- ifpatternisnull.
-
FileHandler
Construct a newFileHandler. The given name pattern is used as output filename, the maximum file size is set tolimitand the file count is initialized tocount. The remaining configuration is done usingLogManagerproperties. This handler is configured to write to a rotating set of count files, when the limit of bytes has been written to one output file, another file will be opened instead.- Parameters:
pattern- the name pattern for the output file.limit- the data amount limit in bytes of one output file, can not be negative.count- the maximum number of files to use, can not be less than one.- Throws:
IOException- if any I/O error occurs.IllegalArgumentException- ifpatternis empty,limit < 0orcount < 1.NullPointerException- ifpatternisnull.
-
FileHandler
Construct a newFileHandler. The given name pattern is used as output filename, the maximum file size is set tolimit, the file count is initialized tocountand the append mode is set toappend. The remaining configuration is done usingLogManagerproperties. This handler is configured to write to a rotating set of count files, when the limit of bytes has been written to one output file, another file will be opened instead.- Parameters:
pattern- the name pattern for the output file.limit- the data amount limit in bytes of one output file, can not be negative.count- the maximum number of files to use, can not be less than one.append- the append mode.- Throws:
IOException- if any I/O error occurs.IllegalArgumentException- ifpatternis empty,limit < 0orcount < 1.NullPointerException- ifpatternisnull.
-
-
Method Details
-
close
public void close()Flushes and closes all opened files.- Overrides:
closein classStreamHandler
-
publish
Publish aLogRecord.- Overrides:
publishin classStreamHandler- Parameters:
record- the log record to publish.
-