Java Logging API
From TracingWiki
The official release of JDK 1.4 came out in 2002 and included the JAVA logging API. This API provides a way to manually instrument java source code for debugging purposes. The target application needs to create instances of Logger objects, and may assign for each a different Handler object. The logger object can set the Level of any log message, and this can be used to control logging output. Handler objects also have their own Level. Enabling logging at a given level filters out events at lower levels and records events at that level and higher. The levels in descending order are: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST.
Logging example:
Logger logger = Logger.getLogger(“xyz.abc”);
logger.log(Level.WARNING,“This is an example”);
Some of the already implemented handlers that can be assigned to a logger are: FileHandler, simple file logger, MemoryHandler, handler that buffers messages in a circular buffer in memory, ConsoleHandler publishes log records to System.err, SocketHandler, simple network logging handler. Other non-implemented Handlers can be developed by subclassing one of the provided Handlers. Typically every Handler will have a Formatter associated with it. The two standard Formatters included in J2SE are: the SimpleFormatter which formats events into human-readable strings and the XMLFormatter which formats events into XML structured records. The XMLFormatter is used by default.
A default configuration file (lib/logging.properties) contains properties for use by Handlers and loggers (logger levels, size of the memory ring buffer...). This file is automatically read at startup by a LogManager object. Further modifications are possible by referring to the LogManager object.
The performance impact of logging on the execution time was studied with 4 scenarios:
- a simple loop without logging, 2279ms;
- the same loop with a logging statement within the loop but at a severity level not currently logged, 2401ms;
- the logging level of the statement is raised such that logging takes place in a memory based circular buffer, MemoryHandler, 10054ms;
- the memory based circular buffer is replaced by a FileHandler and events are written to disk, 210442 ms.
The XMLFormatter was used by the FileHandler and gave better results than the SimpleFormatter. When using a FileHandler to log events directly to a file, an overhead of 0.208 ms per event is observed. On the other hand, the MemoryHandler induces a cost of 7.77 us per event.
[edit] References
http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html
http://en.wikipedia.org/wiki/Java_Logging_Frameworks
