Transcript Logging
Logging
Logging
1
Introduction
• Ships must keep a written log telling speed,
direction, destination, etc.
– A kind of diary of the ship.
• Large program should keep a written log telling
about all the major events in the “life” of the
program.
• Server programs usually keeps a log telling
which clients requested what service from the
server – and when – and how the server
responded to the request
– Ordinary response
– Exception(al) response
Logging
2
Java logging API
• J2SE has included a logging API since
version 1.4
– Package: java.util.logging
– The logging API is highly configurable
• You chose which media the log is written to
• You choose which level of messages to write to the
log.
– Only severe problems, or all kinds of information
messages
Logging
3
Class Logger
• Class Logger is the most central class in
the logging API.
• Class Logger has no public constructor
– You must use a static get method to obtain an
object of class Logger.
• public static Logger getLogger(String name)
Logging
4
Log levels
• A log record has an associated log level telling
how “important” the log record is.
• The class Level defines 7 log levels
• The levels in descending order are:
–
–
–
–
–
–
–
SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value)
Logging
5
Log methods
• Class Logger has a lot of log methods including
– a general log method
• Log(Level level, String message)
– a method for each log level
•
•
•
•
Severe(String message)
Info(String message)
Etc.
These methods are convenience methods
– They are not really necessary.
– All log methods are thread safe (synchronized)
• Can be used simultanously by many threads.
Logging
6
Architecture
• A logger object associates a set of handler
objects.
• A handler object associates a filter and a
formatter.
Logging
7
Handlers
• A logger object is associated with a set of
handler objects.
• A handler writes log records to a specific media.
• Some concrete handlers
– ConsoleHandler
• Writes log records to the console (i.e. the screen).
– FileHandler
• Writes log records to a file.
– SocketHandler
• Writes log records to a network connection.
Logging
8
Filter
• Each handler has a filter.
• The filter decides whether the log record is
loggable or not
– Whether is should be written by this handler
Logging
9
Formatter
• Each handler has a formatter.
• The formatter decides the format of the log
record written by this handler.
• Different kinds of formatters
– SimpleFormatter
– XMLFormatter
– Make your own formatter by sub-classing
Formatter
Logging
10
Logger names
• Each logger has a name
– Used to create the logger
• The names form a hierarchy
– Dk, dk.rhs, dk.rhs.myproject
• Often you use package names as logger
names.
Logging
11
Design patterns in the logging API
• To achieve flexibility the logging API used a
number of design patterns
– Factory method
• getLogger in Logger
– Observer – observable
• Handler observes the Logger
• Whenever a log “event” happens in Logger all associated
handlers are “notified”.
• You can attach and detach handlers at runtime.
– Strategy
• Handler.filter and Handler.formatter
– Encapsulates part of the algorithm of Handler in separate
classes. In this case: How to filter and how to format.
Logging
12
References
• Sun Microsystems: Java Logging
Overview, 2001
– http://java.sun.com/j2se/1.5.0/docs/guide/logg
ing/overview.html
• Javadoc of package java.util.logging
– http://java.sun.com/j2se/1.5.0/docs/api/overvi
ew-summary.html
• Mark Grand: Design Patterns in Java,
Volume 1, Wiley 1998
Logging
13