Java Logging API - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Java Logging API

Description:

Use System.out.println when in doubt. PROS: Easy to Implement. Effective. CONS: ... Create local 'level' variable and constants. Preface println statements with ... – PowerPoint PPT presentation

Number of Views:229
Avg rating:3.0/5.0
Slides: 14
Provided by: chrisc89
Category:
Tags: api | doubt | java | logging

less

Transcript and Presenter's Notes

Title: Java Logging API


1
Java Logging API
  • An alternative to Debug by println
  • Christopher J Coakley

2
Overview
  • Debug by println Server Example
  • Adding granularity
  • DIY Logging API
  • Logging API
  • Changing Logging Levels and Handlers
  • Apache log4j
  • Conclusion

3
Debug by println
  • Use System.out.println when in doubt
  • PROS
  • Easy to Implement
  • Effective
  • CONS
  • Annoying to remove (readd)
  • All or Nothing Approach
  • Example Server1.java

4
Adding Granularity
  • Create local level variable and constants
  • Preface println statements with if braces
  • if (level lt SERIOUS) System.out.println(Crash)
  • CONS more code to look at
  • Example Server2.java

5
DIY Logging API
  • Encapsulate the level
  • Allow alternative output
  • Example MyLogger.java
  • PROS Does everything we wanted
  • CONS What if we want more

6
Logging API
  • Encapsulates (almost) everything
  • java.util.logging.Logger
  • Create with Logger.getLogger() factory method
  • Logger log Logger.getLogger( this.class.getName(
    ) )
  • log.warning(bad news)
  • log.severe(fatal program error)
  • log.info(boring stuff)
  • Example Server3.java

7
Changing Logging Levels and Handlers
  • A handler is responsible for the level
  • Delegation allows multiple handlers
  • Output everything to file, critical to System.err
  • Supports inheritance
  • Logger pre-filters messages
  • Warning Suns info is wrong (incomplete)!

8
Handlers Continued
  • StreamHandler can wrap System.err
  • SocketHandler can write over TCP
  • FileHandler supports log file rotation
  • FileHandler defaults to XMLFormatter
  • FileHandler(pattern, limit, count, append)
  • Example Server4.java

9
Apache log4j
  • Replacement for Java logging
  • Cool Features
  • System Logger
  • GUI log file viewer
  • Universal and Generic Logging Interface
  • http//logging.apache.org/log4j/docs/

10
Conclusion
  • Logging is not Testing
  • Logging is a Debug by println replacement
  • Useful for long running server applications

11
Questions?
12
Factory Pattern - Encapsulate new
  • public class myclass
  • private static HashMap h_ new HashMap()
  • public static MyClass getMyClass(String name)
  • MyClass ret (MyClass)h_.get(name)
  • if (ret null)
  • ret new MyClass(name)
  • h.put(name,ret)
  • return ret

13
Java Properties
  • java.util.Properties
  • Just a hash table
  • System.getProperties()
  • Used by a variety of APIs and Extensions
  • Can be used by logging to change preferences.
  • Example PropLog.java
Write a Comment
User Comments (0)
About PowerShow.com