logging

12
Logging 1 Logging

Upload: doria

Post on 04-Jan-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Logging. 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Logging

Logging 1

Logging

Page 2: Logging

Logging 2

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

Page 3: Logging

Logging 3

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

Page 4: Logging

Logging 4

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)

Page 5: Logging

Logging 5

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)

Page 6: Logging

Logging 6

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.

Page 7: Logging

Logging 7

Architecture

• A logger object associates a set of handler objects.

• A handler object associates a filter and a formatter.

Page 8: Logging

Logging 8

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.

Page 9: Logging

Logging 9

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

Page 10: Logging

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.

Page 11: Logging

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.

Page 12: Logging

Logging 12

References

• Sun Microsystems: Java Logging Overview, 2001– http://java.sun.com/j2se/1.5.0/docs/guide/logging/

overview.html• JavaDoc of package java.util.logging

– http://java.sun.com/j2se/1.5.0/docs/api/overview-summary.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