advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Looking into the JDK 1.4 Logging API :

Looking into the JDK 1.4 Logging API

by Keld H. Hansen

Introduction

In JDK 1.4, which was made public in February 2002, Sun introduced a logging API. Until then developers could use one of the many open source logging systems, like Log4J, or implement their own logging mechanism. You'll find many fine articles on the web describing the new Sun Logging API--see the Resources section at the end of my article--so I'll only give you the most important details as a start. We'll then dig down into a few topics which I haven't seen described yet on the web.

But first:

Why is logging important?

The answer is really quite simple: it's a way for your application to tell IT people (developers, administrators, supporters, maintainers, the performance staff, etc.) what's going on inside the Java code. The developers need information during development and debugging. The support staff needs data when the application hits an error. Production people need performance data in order to make best use of computer resources and improve performance. Here are some of the things that could be logged:

  • initialization and shutdown of the application
  • every time a new user logs on and off to the system
  • every time a database report is requested from the system (report name, parameters, cpu usage, )
  • response times for critical requests
  • entry and exit for key modules
  • system errors (it's part of the application's exception handling)
  • every activation of key functions in the application (e.g. from a menu)
  • every call to external systems
  • data that can tell how the application is being used

For most applications you don't have to worry about any negative performance impact caused by the logging code. In production you'd typically switch most logging off, and the JDK 1.4 logging API--and most other popular logging APIs--are designed to be cheap if you don't send much data to the logs. The day you need more logging data--e.g. when a bug has appeared or when you need to measure performance--you can switch logging on for the modules you need.

I hope this'll motivate you for our journey into the design and usage of the JDK 1.4 Logging API.

Loggers, Handlers and Formatters

When using the JDK 1.4 Logging API in your applications you work with Logger objects when sending a message to a log. It's wonderfully simple to work with a logger:

// Define the logger
    Logger logger = Logger.getLogger("hansen.Game");
    . . .
    // Use the logger
    logger.info("Game started");

Couldn't be much simpler.

But where does the message end? This is where the Handlers come into the picture. A handler defines the target for logging. It could be the console, a file, or the Windows NT event log, to name a few possibilities. So every logger needs at least one handler in order for the messages to have a place to go. The JDK defines 5 handlers:

  • StreamHandler : sends messages to an OutputStream
  • ConsoleHandler: sends messages to System.err
  • FileHandler: sends messages to a file
  • SocketHandler: sends messages to a TCP port
  • MemoryHandler: buffers messages in memory

The ConsoleHandler and the FileHandler are probably the most popular ones, covering most applications basic needs.

Finally we have the Formatters. As their name imply they determine the layout of the messages. Sun has defined two formatters:

  • SimpleFormatter: defines a simple message format containing among other things a timestamp
  • XMLFormatter: a message in XML format

Every handler has an associated formatter.

In this example we have defined two loggers. Logger1 has a single ConsoleHandler that uses the SimpleFormatter. Logger2 also has a ConsoleHandler (maybe with other properties) but also has a FileHandler that writes messages in XML format.

It's possible to define your own handlers and formatters.

The default set-up which you get if you don't do anything special is a ConsoleHandler using the SimpleFormatter. If you place the code fragment found at the beginning of this article in a main method and run it, you'll get this written to System.err:

11-08-2002 18:57:37 hansen.playground.logging.LogDef main
    INFO: Game started 
   

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Red Hat Takes on HPC Market, Microsoft
Python's New Release Bridges the Gap
No Flash Seen on iPhone Horizon
Apple Yields to Complaints Over iPhone NDA
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC
Oracle Keeps Building on Spoils From BEA
Intel, Oracle Head For 'The Cloud'
Oracle Focuses on Grid, Developer Tools

eCryptfs: Single-File Encryption in Linux
CCXML in Action: A CCXML Auto Attendant
Ballmer: Current Woes Won't Halt Tech, Microsoft
Microsoft Uses VMworld to Hype Its Hypervisor
Microsoft Charges Ahead in Virtualization
Microsoft Shows Some Ankle With Visual Studio
Top 5 Reasons to Adopt SQL Server 2008
Make Application Development Easy With Software Plus Services
SharePoint Applied: 10 Things You Wish They Had Told You?Part 2
Introducing Zend_Search_Lucene

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES