Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logging1. 2 Introduction Ships must keep a written log telling speed, direction, destination, etc. –A kind of diary of the ship. Large programs should.

Similar presentations


Presentation on theme: "Logging1. 2 Introduction Ships must keep a written log telling speed, direction, destination, etc. –A kind of diary of the ship. Large programs should."— Presentation transcript:

1 Logging1

2 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

3 Logging3 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

4 Logging4 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)LoggerString

5 Logging5 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)

6 Logging6 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.

7 Logging7 Architecture A logger object associates a set of handler objects. A handler object associates a filter and a formatter.

8 Logging8 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.

9 Logging9 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

10 Logging10 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.

11 Logging11 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.

12 Logging12 References Sun Microsystems: Java Logging Overview, 2001 –http://java.sun.com/j2se/1.5.0/docs/guide/logging/over view.htmlhttp://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/overview- summary.htmlhttp://java.sun.com/j2se/1.5.0/docs/api/overview- summary.html Mark Grand: Design Patterns in Java 2 nd edition, Volume 1, Wiley 2002 –Factory Method, page 103-117 –Observer, page 387-396 –Strategy, page 413-418


Download ppt "Logging1. 2 Introduction Ships must keep a written log telling speed, direction, destination, etc. –A kind of diary of the ship. Large programs should."

Similar presentations


Ads by Google