Diagnostic Flashcards
Logging
automatically recording diagnostic output from a program
logging output can be useful during development and testing, but also in production code. A web server could log IP address of incoming http requests. A mail server could log basic info about each email recieved
Logging using standard error output, or ordinary file output, can be done
It can be better to make use of a logging framework, which provides lots of useful functionality
Logging Framework or API (application programmer inferface)
- specificies origin of log entry (which application, which class, which method…)
- specify content of log entry
- specify level of importance of log entry
- control what importance levels are actually being logged
- control where log entries are recorded
- analyze resulting log files
Classes in java.util.logging
important classes of java.util.logging package:
- Logger
- Handler (subclasses - consoleHandler, FileHandler, SocketHandler)
- Formatter (subclasses- simlpeformater, XML formatter)
- Level
Level class (java.util.logging.Level)
contains public static names constants used to specify the importance level of log messages, and to control which log records are actually logged
(from high to low): Level.SEVERE Level.WARNING Level.INFO Level.CONFIG Level.FINE Level.FINER Level.FINEST
Levels are used in two (2) ways
when logging a message a level must be specified for that message
each logger and handler has a level set for it; log messages with a level less than that are ignored
Other named constants
Level.ALL
Level.OFF
Handler
Handles the log message
Each logger object must have one or more handler object associated with it, to actually log any log messagges
Existing Handler Classes:
- ConsoleHandler: log to stderr, Each logger has this handler by default
- FileHandler: log to a file, with controllable log file rotation
- SocketHandler: connect to a logging server over a TCP/IP socket
Formater
Each Handler object must have a formatter object which it uses to format logging messages
Existing Formatter Classes
SimpleFormatter: the default for ConsoleHandler
XMLFOrmatter: the default FileHandler and SocketFormater
Each log message has an associated log Level. The Level specifies the importance and urgency of a log message. Levels are really integers, with higher values indicating higher priorities.
The Level class defines seven standard log levels as named constants, ranging from FINEST (the lowest priority, with the lowest value) to SEVERE (the highest priority, with the highest value).
The Level class defines a set of standard logging levels that can be used to control logging output. The logging Level objects are ordered and are specified by ordered integers. Enabling logging at a given level also enables logging at all higher levels.
The levels in descending order are:
SEVERE (highest value) WARNING INFO CONFIG FINE FINER FINEST (lowest value) In addition there is a level OFF that can be used to turn off logging, and a level ALL that can be used to enable logging of all messages.
Lab9: Question 2: What level would I use to turn off logging? What level would I use to enable the logging of all messages? (The Level class link used for Question 1 might be useful)
In addition there is a level OFF that can be used to turn off logging, and a level ALL that can be used to enable logging of all messages.
Tracing involves logging messages which report the state of the application at different stages of execution. In the Java logging framework, there are three levels that should be used for tracing:
FINE, FINER and FINEST.
FINE: Use this level for significant events that explain the flow or state of the system when trying to understand or debug a problem. These are usually at the level of object creation, catch clauses for exceptions that do not constitute errors, and so on.
FINER: There is a set of Logger methods that generate messages with a FINER level. These are entry(…), exit(..), finer(..) (This is a method named finer; different from a logging level called FINER) and throwing(..). Entering and exiting should be used for tracing method entry and exit; arguments and return values can be specified. Finer can be used for any tracing that is more verbose than desired for fine, but is not method entry/exit or used before throwing an exception. Throwing should be used when you are about to throw or re-throw an exception.
FINEST: Finest is usually thought of as developer or debug tracing. This is used during development, when attempting to learn the behavior of a system at a fine level of detail and when trying to diagnose difficult problems once the code is released. This level of tracing can be used to track the state of instance variables in a class e.g. before a method call, inside a method or a loop and after a method call.