Here we can start with a very simple java logger implementation example.
Above sample it used default java inbuilt formatter which is SimpleFormatter
also it become applied to Console handler which result output printed into the console output. Here it prints three types of logging messages info,warning and severe. The output of above become follows
Sep 03, 2014 8:09:52 PM com.behindjava.LoggerSample main INFO: This is an info message Sep 03, 2014 8:09:52 PM com.behindjava.LoggerSample main WARNING: This is a warning message Sep 03, 2014 8:09:52 PM com.behindjava.LoggerSample main SEVERE: This is a severe message
Logging Levels in Java Logger
In java consist of there are 7 logging levels which are FINEST (lowest) -> FINER -> FINE -> CONFIG -> INFO -> WARNING -> SEVERE (highest)
. Also it is possible to configure the logging levels in Logger object like
logger.setLevel(Level.WARNING);
If you add this line of code in above sample program then it will print the warning log and above. So the result become
Sep 03, 2014 8:22:51 PM com.behindjava.LoggerSample main WARNING: This is a warning message Sep 03, 2014 8:22:51 PM com.behindjava.LoggerSample main SEVERE: This is a severe message
Configuring Handler For logger
A Handler typically uses a Formatter to format the message before logging it. You can create your own Handler if you want, but Java comes with 4 built-in Handler's already which are follows.
ConsoleHandler - The ConsoleHandler logs all messages to System.err
. By default the ConsoleHandler uses a SimpleFormatter to format the messages before writing them to System.err
. The console handler can writern like follows.
ConsoleHandler handler = new ConsoleHandler();
FileHandler - The FileHandler writes all messages to file. This can either be a single file, or a set of rotated files. If rotated files are used, each file is filled to a certain size limit, after which a new file is created. Below follows the FileHandler with different constructors.
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);
Here 'pattern' means the predefind name of generating file. 'append' is for whether the FileHandler should append to any existing files or not. 'limit' for logging file limit. When the log files reach the given file size limit a new file is created, until the maximum of the file 'count' is reached. Below shows an example of FileHandler
FileHandler handler = new FileHandler("sample-log.%u.%g.txt", 1024 * 1024, 10, true);
In the above code you can findout the file pattern which contain some special code. Below shows the explanation of these special codes.
/ 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 that distinguishes the rotated log files from each other. %u A unique number to avoid naming conflicts. %% A single percent sign, in case you want to use that in your file name
StreamHandler - The StreamHandler
writes the log messages to an OutputStream
. Here is an example of how you create a StreamHandler:
StreamHandler handler = new StreamHandler(); StreamHandler handler = new StreamHandler(outputStream, formatter);
SocketHandler - A SocketHandler writes the log messages to some network address via a socket. The log messages are sent across the network raw (as text). They are not wrapped in an HTTP request or anything like that. Here is how you create a SocketHandler:
SocketHandler socketHandler = new SocketHandler(host, port);
MemoryHandler - A MemoryHandler is a handler that keeps the LogRecords internally in a buffer. When the internal buffer is full, new LogRecords start overwriting the oldest ones in the buffer.
MemoryHandler handler = MemoryHandler(); MemoryHandler handler = MemoryHandler(targetHandler, bufferSize, pushLevel);
No comments:
Post a Comment