Logging - PowerPoint PPT Presentation

About This Presentation
Title:

Logging

Description:

Log messages can go to the console, to a file, or one of several other places (e. ... void exiting(String sourceClass, String sourceMethod) ... – PowerPoint PPT presentation

Number of Views:115
Avg rating:3.0/5.0
Slides: 18
Provided by: DaveMa56
Category:
Tags: exiting | goto | logging | seein

less

Transcript and Presenter's Notes

Title: Logging


1
Logging
2
What is logging?
  • Logging is producing messages that tell you
    what your program is doing
  • Its not much different than using
    System.out.println(...)
  • Log messages can go to the console, to a file, or
    one of several other places (e.g. sent over the
    Internet)
  • You can use logging to help you debug a program
  • You can use logging to produce a file when the
    user runs your program

3
Why use logging?
  • Reality Large programs always have bugs
  • JUnit testing can greatly reduce this problem,
    but its impossible to see in advance all
    potentially useful tests
  • Your program may go to customers (users)
  • Typical error report from customer It doesnt
    work.
  • We love it when we have a customer who tries
    assorted things and gives us a detailed scenario
    in which the program fails
  • Such customers are rare
  • Heres what you want to tell the typical
    customer
  • Send us the log its in such-and-such a place.

4
Basic use
  • import java.util.logging.
  • private static Logger myLogger
    Logger.getLogger("myPackage")
  • myLogger.log(Level.SEVERE, "Bad news!")
  • Aug 15, 2004 105109 AM myPackage.Sync
    mainSEVERE Bad news!

5
Logging levels and methods
  • myLogger.severe(String msg)
  • myLogger.warning(String msg)
  • myLogger.info(String msg)
  • myLogger.config(String msg)
  • myLogger.fine(String msg)
  • myLogger.finer(String msg)
  • myLogger.finest(String msg)
  • Level.SEVERE
  • Level.WARNING
  • Level.INFO
  • Level.CONFIG
  • Level.FINE
  • Level.FINER
  • Level.FINEST
  • These levels are ordered, so that you can set the
    level of severity that results in log messages
  • However, the levels have no inherent
    meaning--they are what you make of them

6
Controlling logging levels
  • public void Logger.setLevel(Level newLevel)
  • Sets the logger to log all messages at newLevel
    or above
  • Logger calls at lower levels dont do anything
  • Example logger.setLevel(Level.WARNING)
  • Additional settings
  • logger.setLevel(Level.ALL)
  • logger.setLevel(Level.OFF)
  • public Level getLevel()
  • Note that this returns a Level, not an int
  • Level has intValue() and toString() methods

7
Additional Logger methods
  • void entering(String sourceClass,
    String sourceMethod)
  • void entering(String sourceClass,
    String sourceMethod,
    Object param1)
  • void entering(String sourceClass,
    String sourceMethod,
    Object params)
  • void exiting(String sourceClass,
    String sourceMethod)
  • void exiting(String sourceClass,
    String sourceMethod,
    Object result)
  • These log messages at level FINER

8
Logging flow of control
  • You send your message to a Logger
  • The Logger checks a Filter to see whether to
    ignore the message
  • The Logger sends the message to a Handler to put
    the message somewhere
  • The Handler checks another Filter to see whether
    to ignore this kind of message sent to this
    destination
  • The Handler calls a Formatter to decide what kind
    of a text string to produce
  • The Handler sends the formatted message somewhere

9
Filters
  • Filter is an interface it defines the single
    methodboolean isLoggable(LogRecord record)
  • A LogRecord is another class in
    java.util.logging it provides numerous methods
    for examining the proposed logging message
  • We wont go into the details of LogRecord in this
    lecture (see the API if you need to use this)

10
Logging formatting and destinations
  • The JDK defines five Handlers
  • StreamHandler sends messages to an OutputStream
  • ConsoleHandler sends messages to System.err
    (default)
  • FileHandler sends messages to a file
  • SocketHandler sends messages to a TCP port
  • MemoryHandler buffers messages in memory
  • It also defines two ways to format messages
  • SimpleFormatter (default)
  • XMLFormatter
  • And, of course, you can define your own Handlers
    and Formatters
  • As you can tell from the Basic Use slide
    earlier, you can ignore all of this and just use
    the defaults

11
Using a FileHandler
  • try logger.addHandler(new
    FileHandler("myLogFile"))catch
    (SecurityException e) e.printStackTrace()
  • The default Formatter for a FileHandler is
    XMLFormatter

12
FileHandler results
  • lt?xml version"1.0" encoding"MacRoman"
    standalone"no"?gtlt!DOCTYPE log SYSTEM
    "logger.dtd"gtltloggtltrecordgt ltdategt2004-08-15T13
    2126lt/dategt ltmillisgt1092590486248lt/millisgt
    ltsequencegt0lt/sequencegt ltloggergtmyPackagelt/logger
    gt ltlevelgtSEVERElt/levelgt ltclassgtmyClasslt/classgt
    ltmethodgtmainlt/methodgt ltthreadgt10lt/threadgt
    ltmessagegtBad news!lt/messagegtlt/recordgtlt/loggt

13
Reading log files
  • Dont like XML? See the SimpleFormatter class
  • When you log, your log messages must be
    exceptionally easy to understand
  • Remember, you might see a log file months or even
    years after you wrote the program
  • Messages such as at1 12 37 5 may no longer make
    any sense to you

14
What to log
  • Heres what you dont want to log
  • Everything!
  • Specific methods only after an error has occurred
  • Heres what you do want to log
  • Critical events, such as updating a database
  • Results of a long sequence of operations
  • Interactions with other components of a large
    system
  • Actions known to be complex or error-prone

15
When logging goes bad
  • Heres an all-too-common error
  • Create log files on every run of the program
  • Never get rid of old log files
  • Solution
  • Use the FileHandler class
  • The FileHandler can either write to a specified
    file, or it can write to a rotating set of files.
  • For a rotating set of files, as each file reaches
    a given size limit, it is closed, rotated out,
    and a new file opened. Successively older files
    are named by adding "0", "1", "2", etc into the
    base filename.

16
The rest of the story
  • As usual, Ive only given you a few features
    beyond the most basic usage
  • If you want to know about Filters, other
    destinations than the console or a file, etc.,
    its all in the Java API
  • You should also know that there is an alternative
    open source logging package, log4j
  • log4j has been around longer than Suns logging
    package
  • Many people feel that log4j is better
  • I didnt cover it because it isnt installed in
    the labs here

17
The End
Write a Comment
User Comments (0)
About PowerShow.com