Java Logging API
From TracingWiki
TracingBook : Source Level Instrumentation : Manually Inserted Instrumentation : High Level Languages Logging APIs : Java Logging API
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, recording events only 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, a simple file logger,
- MemoryHandler, a handler that buffers messages in a circular buffer in memory,
- ConsoleHandler, which publishes log records to System.err,
- SocketHandler, a 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, 2279 ms;
- the same loop with a logging statement within the loop but at a severity level not currently logged, 2401 ms;
- the logging level of the statement is raised such that logging takes place in a memory-based circular buffer, MemoryHandler, 10054 ms;
- 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 µs per event.
[edit] References
- Java 1.4.2 Logging Overview
- Java Logging Frameworks on Wikipedia
