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 programs 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 simultaneously 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
Filters and formatters
• Each handler has a filter.
• The filter decides whether
the log record is loggable
or not
– Whether is should be
written by this handler
• 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
9
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
10
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
11
References
• Sun Microsystems: Java Logging Overview,
2001
– http://java.sun.com/j2se/1.5.0/docs/guide/logging/over
view.html
• JavaDoc of package java.util.logging
– http://java.sun.com/j2se/1.5.0/docs/api/overviewsummary.html
• Mark Grand: Design Patterns in Java 2nd edition,
Volume 1, Wiley 2002
– Factory Method, page 103-117
– Observer, page 387-396
– Strategy, page 413-418
Logging
12