Handler.
The FileHandler can either write to a specified file,
or it can write to a rotating set of files.
For a rotating set of files, as each file reaches a given size limit, it is closed, rotated out, and a new file opened. Successively older files are named by adding "0", "1", "2", etc. into the base filename.
By default buffering is enabled in the IO libraries but each log record is flushed out when it is complete.
By default the XMLFormatter class is used for formatting.
Configuration:
By default each FileHandler is initialized using the following
LogManager configuration properties where <handler-name>
refers to the fully-qualified class name of the handler.
If properties are not defined
(or have invalid values) then the specified default values are used.
Handler
(defaults to Level.ALL). Filter class to use
(defaults to no Filter). Formatter class to use
(defaults to java.util.logging.XMLFormatter)
For example, the properties for FileHandler would be:
For a custom handler, e.g. com.foo.MyHandler, the properties would be:
A pattern consists of a string that includes the following special components that will be replaced at runtime:
Thus for example a pattern of "%t/java%g.log" with a count of 2 would typically cause log 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 log 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.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class | FileHandler.
A metered stream is a subclass of OutputStream that (a) forwards all its output to a target stream (b) keeps track of how many bytes have been written |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private int | |
| private File[] | |
| private long | |
| private FileChannel | |
| private String | |
| private static final Set | |
| private static final int | |
| private int | |
| private FileHandler. | |
| private String |
| Access | Constructor and Description |
|---|---|
| public | |
| public | FileHandler(String
the name of the output file pattern)Initialize a |
| public | FileHandler(String
the name of the output file pattern, boolean specifies append mode append)Initialize a |
| public | FileHandler(String
the pattern for naming the output file pattern, int the maximum number of bytes to write to any one file limit, int the number of files to use count)Initialize a |
| public | FileHandler(String
the pattern for naming the output file pattern, int the maximum number of bytes to write to any one file limit, int the number of files to use count, boolean specifies append mode append)Initialize a |
| public | FileHandler(String
the pattern for naming the output file pattern, long the maximum number of bytes to write to any one file limit, int the number of files to use count, boolean specifies append mode append)Initialize a |
| Modifier and Type | Method and Description |
|---|---|
| public synchronized void | |
| private void | configure()
Configure a FileHandler from LogManager properties and/or default values as specified in the class javadoc. |
| private File | Returns: the generated Filethe pattern for naming the output file pattern, int the generation number to distinguish rotated logs generation, int a unique number to resolve conflicts unique)Generate a file based on a user-supplied pattern, generation number, and an integer uniqueness suffix |
| pack-priv static File | |
| private boolean | |
| private void | |
| private void | |
| public void | publish(LogRecord
description of the log event. A null record is
silently ignored and is not published record)Overrides java. Format and publish a |
| private synchronized void | |
| pack-priv void | synchronousPostWriteHook()
Overrides java. Overridden by other handlers in this package to facilitate synchronous post-write behaviour. |
| append | back to summary |
|---|---|
| private boolean append | |
| count | back to summary |
|---|---|
| private int count | |
| files | back to summary |
|---|---|
| private File[] files | |
| limit | back to summary |
|---|---|
| private long limit | |
| lockFileChannel | back to summary |
|---|---|
| private FileChannel lockFileChannel | |
| lockFileName | back to summary |
|---|---|
| private String lockFileName | |
| locks | back to summary |
|---|---|
| private static final Set<String> locks | |
| MAX_LOCKS | back to summary |
|---|---|
| private static final int MAX_LOCKS | |
| maxLocks | back to summary |
|---|---|
| private int maxLocks | |
| meter | back to summary |
|---|---|
| private FileHandler. | |
| pattern | back to summary |
|---|---|
| private String pattern | |
| FileHandler | back to summary |
|---|---|
| public FileHandler() throws IOException Construct a default
| |
| FileHandler | back to summary |
|---|---|
| public FileHandler(String pattern) throws IOException Initialize a
The There is no limit on the amount of data that may be written, so use this with care.
| |
| FileHandler | back to summary |
|---|---|
| public FileHandler(String pattern, boolean append) throws IOException Initialize a
The There is no limit on the amount of data that may be written, so use this with care.
| |
| FileHandler | back to summary |
|---|---|
| public FileHandler(String pattern, int limit, int count) throws IOException Initialize a
The The count must be at least 1.
| |
| FileHandler | back to summary |
|---|---|
| public FileHandler(String pattern, int limit, int count, boolean append) throws IOException Initialize a
The The count must be at least 1.
| |
| FileHandler | back to summary |
|---|---|
| public FileHandler(String pattern, long limit, int count, boolean append) throws IOException Initialize a
The The count must be at least 1.
| |
| close | back to summary |
|---|---|
| public synchronized void close() Overrides java. Close all the files.
| |
| configure | back to summary |
|---|---|
| private void configure() Configure a FileHandler from LogManager properties and/or default values as specified in the class javadoc. | |
| generate | back to summary |
|---|---|
| private File generate(String pattern, int generation, int unique) throws IOException Generate a file based on a user-supplied pattern, generation number, and an integer uniqueness suffix
| |
| generate | back to summary |
|---|---|
| pack-priv static File generate(String pat, int count, int generation, int unique) throws IOException | |
| isParentWritable | back to summary |
|---|---|
| private boolean isParentWritable(Path path) | |
| open | back to summary |
|---|---|
| private void open(File fname, boolean append) throws IOException | |
| openFiles | back to summary |
|---|---|
| private void openFiles() throws IOException Open the set of output files, based on the configured instance variables. | |
| publish | back to summary |
|---|---|
| public void publish(LogRecord record) Overrides java. Format and publish a | |
| rotate | back to summary |
|---|---|
| private synchronized void rotate() Rotate the set of output files | |
| synchronousPostWriteHook | back to summary |
|---|---|
| pack-priv void synchronousPostWriteHook() Overrides java. Doc from java. Overridden by other handlers in this package to facilitate synchronous post-write behaviour. If other handlers need similar functionality, it might be feasible to make this method protected (see JDK-8349206), but please find a better name if you do ;).
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv Exception |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public void | error(String msg, Exception ex, int code)
Overrides java. The error method is called when a Handler failure occurs. |
| lastException | back to summary |
|---|---|
| pack-priv Exception lastException | |
| InitializationErrorManager | back to summary |
|---|---|
| private InitializationErrorManager() | |
| error | back to summary |
|---|---|
| public void error(String msg, Exception ex, int code) Overrides java. Doc from java. The error method is called when a Handler failure occurs. This method may be overridden in subclasses. The default behavior in this base class is that the first call is reported to System.err, and subsequent calls are ignored.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final OutputStream | |
| pack-priv long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Overrides java. Implements java. Closes this output stream and releases any system resources associated with this stream. |
| public void | flush()
Overrides java. Implements java. Flushes this output stream and forces any buffered output bytes to be written out. |
| public void | write(int b)
Implements abstract java. Writes the specified byte to this output stream. |
| public void | write(byte[] buff)
Overrides java. Writes |
| public void | write(byte[] buff, int off, int len)
Overrides java. Writes |
| out | back to summary |
|---|---|
| pack-priv final OutputStream out | |
| written | back to summary |
|---|---|
| pack-priv long written | |
| MeteredStream | back to summary |
|---|---|
| pack-priv MeteredStream(OutputStream out, long written) | |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Doc from java. Closes this output stream and releases any system resources
associated with this stream. The general contract of
| |
| flush | back to summary |
|---|---|
| public void flush() throws IOException Overrides java. Implements java. Doc from java. Flushes this output stream and forces any buffered output bytes
to be written out. The general contract of If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.
| |
| write | back to summary |
|---|---|
| public void write(int b) throws IOException Implements abstract java. Doc from java. Writes the specified byte to this output stream. The general
contract for
| |
| write | back to summary |
|---|---|
| public void write(byte[] buff) throws IOException Overrides java. Doc from java. Writes
| |
| write | back to summary |
|---|---|
| public void write(byte[] buff, int off, int len) throws IOException Overrides java. Doc from java. Writes
If
If
| |