- Forward


Logging in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • Some Data:
    • About 25% of maintenance expenditures are corrective (i.e., a result of defects found in the production environment) and preventive (i.e., a result of defects found in the development/testing environment)
  • An Implication:
    • Design with maintenance (and support) in mind
Overview
Back SMYC Forward
  • What is Logging?
    • A mechanism for recording or transmiting problems that arise at run time
  • Users:
    • Developers
    • Field Service Engineers
    • Users
    • System Administrators
Key Elements of java.util.logging
Back SMYC Forward
  • Logger java.util.logging.Logger :
    • Used to log messages
  • LogRecord java.util.logging.LogRecord :
    • An encapsulation of the information to be logged
  • Handler java.util.logging.Handler :
    • Sends LogRecord objects to a variety of destinations including memory, output streams, consoles, files, and sockets
  • Filter java.util.logging.Filter :
    • Provides fine-grained control over what gets logged
  • Formatter java.util.logging.Formatter :
    • Formats LogRecord objects
The Process
Back SMYC Forward
  1. The application calls a method in a Logger object.
  2. The Logger creates a LogRecord.
  3. The Logger (perhaps after checking a log level and/or Filter) passes the LogRecord to a Handler.
  4. The Handler uses a Formatter is used to format the LogRecord.
Log Levels
Back SMYC Forward
  • Purpose:
    • Give a rough guide to the importance/urgency of an entry
  • The Level java.util.logging.Level Class:
    • Contains standard levels that range from SEVERE to FINEST
Logging Methods
Back SMYC Forward
  • Generic:
    • severe(String message)
    • warning(String message)
    • fine(String message)
    • finest(String message)
    • log(Level level, String message
  • Specific:
    • entering()
    • exiting()
    • throwing()
Output/Transmission
Back SMYC Forward
  • Handlers:
    • ConsoleHandler
    • FileHandler
    • SocketHandler
  • Formatters:
    • SimpleFormatter
    • XMLFormatter
An Example
Back SMYC Forward

Logging Basics

javaexamples/logging/Driver.java
 
There's Always More to Learn
Back -