Table of Contents
- Handlers and formatters
- Built-in handlers
- Consolehandler
- Filehandler
- Streamhandler
- Sockethandler
- Memoryhandler
A is a component this takes care of the Handler actual logging to the outside world.
You can add one or more's to Handler a Logger . When messages is logged via Logger the, the messages is eventually forwarded Handler to the ' s and if not rejected by a Filter o R the minimum log level of the Logger .
Here's how do you add a to Handler a Logger :
Logger.addhandler (New Consolehandler ());
Handlers and formatters
A Handler typically uses a to Formatter format the message before logging it. You can either create your own or use one of the Formatter built-in Formatter ' s. To learn more about Formatter ' s see the text onformatters.
Here's how to get and set a formatter on a Handler :
Consolehandler handler = new Consolehandler (); Handler.setformatter (new Simpleformatter ()); Formatter Formatter = Handler.getformatter ();
For some built-in the Handler‘s formatter can also is set via the configuration file. See the text in configurationfor more details.
Built-in handlers
You can create the your own Handler if you want, but the Java comes with 4 built-in Handler‘s already:
- Consolehandler
- Filehandler
- Streamhandler
- Sockethandler
- Memoryhandler
You'll most often use FileHandler the, but each of these built-in's is Handler explained briefly in the following sections.
Consolehandler
The ConsoleHandler logs all messages to System.err . By default, the ConsoleHandler uses a to SimpleFormatter format the messages before writing them to System.err . Here's how do you create a ConsoleHandler :
Consolehandler handler = new Consolehandler ();
Filehandler
The FileHandler writes all messages to file. This can either is a single file, or a set of rotated files. If rotated files is used, each file is filled to a certain size limit, after which a new file is created. Each file name is composed of a base name and a sequence number. For instance mylog.0.txt , mylog.1.txt etc.
By default, the uses the to FileHandler XMLFormatter format is messages before writing them to a file.
Here is the various constructors you can use to create a FileHandler :
Filehandler handler = new Filehandler (); Filehandler handler = new Filehandler (String pattern); Filehandler handler = new Filehandler (String pattern, boolean append); Filehandler handler = new Filehandler (String pattern, int limit, int count); Filehandler handler = new Filehandler (String pattern, int limit, int count, boolean append);
The first constructor creates a default FileHandler . This is FileHandler fully configured via the configuration file.
The second constructor creates a with FileHandler a predefined pattern for generating file names for the log files.
The third constructor creates a FileHandler with a file name pattern, and a Boolean telling whether the FileHandler should append to an Y existing files or not. There is no file size limit, and file count are set to 1.
The fourth constructor creates a FileHandler with a file name pattern, a file size limit, and a file count. When the log files reach the given file size limit a new file was created, until the maximum of the file count is reached. Then FileHandler the starts-to-the-first file again, deleting it and logging to it from scratch.
The fifth constructor creates a with FileHandler a file name pattern, a file size limit, a file count, and a Boolean telling whe Ther the FileHandler should append to any existing files or not.
Here is a few examples:
Filehandler handler = new Filehandler ("Myapp-log.%u.%g.txt"); Filehandler handler = new Filehandler ("Myapp-log.%u.%g.txt", true); Filehandler handler = new Filehandler ("Myapp-log.%u.%g.txt", 1024 * 1024, 10); Filehandler handler = new Filehandler ("Myapp-log.%u.%g.txt", 1024x768 * 1024x768, ten, true);
File Name Pattern
The file name pattern is a string containing a file name plus one or more special codes, telling the what to FileHandler generate The file names. The special codes you can use is:
| Code |
Meaning |
| / |
The file name separator of the system. Typically either \ or/. |
| %t |
The temp directory of the system. |
| %h |
The user home directory of the system. |
| %g |
The generation number is distinguishes the rotated log files from each of the other. |
| %u |
A unique number to avoid naming conflicts. |
| %% |
A single percent sign, in the case you want the use of that in your file name. |
If no%g code has been specified FileHandler and the file count of the was greater than 1, then the generation number (file Seque NCE number) would be appended to the end of the file name, after a dot (.).
The%u code is usually set to 0. If a file already exists with that name which are in use by another process, the%u code are set to 1, 2 etc. until an unuse D base file name is found. If No%u code is used. The file name pattern, and a conflict over the file name are found with another process, the Uniqu E number is appended to the end of the file name and after any automatically added generation number. Note:the use of%u to generate the unique numbers for base file names are only guaranteed to work on a local file system.
Here is a few examples:
| file Name Pattern |
meaning |
| logfile.txt |
the file is called logfile, and are located in the current directory of the Application. |
logfile%g.txt |
the file is called logfile, and is Locat Ed in the current directory of the application. A generation number is inserted after the text "logfile" in the file name. For instance, logfile0.txt , logfile1.txt etc. |
logfile%u.%g.txt |
the file is called logfile, and is lo cated in the current directory of the application. A unique number and a generation number is inserted after the text "logfile" in the file name. for instance, logfile0.0.txt , logfile0.1.txt etc. |
Streamhandler
The StreamHandler writes the log messages to an OutputStream . Here's an example on how do you create a StreamHandler :
Streamhandler handler = new Streamhandler (); Streamhandler handler = new Streamhandler (OutputStream, formatter);
The first constructor creates a empty with StreamHandler no OutputStream . You must set one using the method setOutputStream() before using the handler.
The second constructor creates a with StreamHandler OutputStream A and a the Formatter outputStream parameter should is some you OutputStream either creat E or obtain from some and other object. For instance, it could is a fileoutputstream.
The Formatter should is whatever formatter (built-in or custom implementation) you want to use. The StreamHandler uses a by SimpleFormatter default (if you use the Zero-arg constructor).
Sockethandler
A SocketHandler writes the log messages to some network address via a socket. The log messages is sent across the network raw (as text). They is not wrapped in an HTTP request or anything.
Here's how do you create a SocketHandler :
Sockethandler Sockethandler = new Sockethandler (host, Port);
The host parameter should is a string pointing to a domain name (host). The port parameter should is the TCP port to connect to.
Here's an example, uses concrete values in the constructor:
Sockethandler Sockethandler = new Sockethandler ("jenkov.com", 80);
Memoryhandler
A is MemoryHandler a handler this keeps the LogRecords internally in a buffer. When the internal buffer was full, new LogRecords start overwriting the oldest ones in the buffer.
When a certain trigger event occurs, the LogRecord "s in the internal buffer is flushed to a target Handler which would write the To an LogRecords external system. For instance, when a of LogRecord some minimum logs level is logged, the whole buffer of LogRecord ' s could get pushed.
You can also call the method to force the's in the buffer to be flushed to the push() LogRecord target Handler .
Here is the examples creating a MemoryHandler :
Memoryhandler handler = Memoryhandler (); Memoryhandler handler = Memoryhandler ( targethandler, buffersize, pushlevel);
The first constructor creates a that MemoryHandler uses the configuration obtained from the configuration file.
The second constructor creates with MemoryHandler a target Handler , a specific internal buffer size, and a minimum push level that G ETS LogRecord ' s pushed to the target Handler .
Here is a example using concrete values for the buffer size and push level parameters in second constructor:
Memoryhandler handler = Memoryhandler ( Targethandler, ten, level.warning);
Java logging:handlers