1 / 12

Logging

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.

doria
Download Presentation

Logging

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 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 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 LoggergetLogger(String name) 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) 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. Logging

  7. Architecture • A logger object associates a set of handler objects. • A handler object associates a filter and a formatter. 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. Logging

  9. 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 Filters and formatters 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/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 Logging

More Related