The Apache Software Foundation's log4j logging library is one of the better logging systems around. It's both easier to use and more flexible than Java's built-in logging system.
Tutorial Details:
This article shows you how to extend log4j with a custom "appender," the part of the system that actually writes the logging messages. This article also provides a simple example of Java's socket APIs and multithreading capabilities.
Logging is the simple process of printing messages of various types to known places. Logging messages can go to a console, to a file, to a remote monitor, or anywhere else that you find convenient. Think of logging as a sophisticated sibling of:
if( debug )
System.out.println("Debugging diagnostic");
Logging has several advantages over simple println() statements, however. The logging system can add contextual information—filename, line number, and date, for example—to the message automatically. You can redirect the messages to different destinations, or change the formatting, without recompiling your program. (In log4j, you just modify a simple properties file.) You can easily turn categories of messages on and off so you can see debug messages when you're debugging, but easily turn them off when you're not, for example.
Logging is central to all of my programs. I use it to monitor my program's progress as it works. I use it to log error messages from library methods that might be used in a server-side context (where there's no console on which to print a stack trace). Most importantly, logging is one of my main debugging tools. Though visual debuggers are handy on occasion, I've noticed that I can find more bugs faster by combining a careful reading of the code with a few well-placed logging messages. (I'm not quite sure why reading/logging seems more effective than visual debugging, but my current theory is that a visual debugger narrows your focus to a single thread of control through the program, so you tend to miss bugs that aren't on that thread.)
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Write custom appenders for log4j
View Tutorial: Write custom appenders for log4j
Related
Tutorials:
JavaWorld
December 1999
JavaWorld
December 1999 |
Create a custom Java 1.2-style ClassLoader - JavaWorld March 2000
Create a custom Java 1.2-style ClassLoader - JavaWorld March 2000 |
Log4j delivers
control over logging - JavaWorld November 2000
Log4j delivers
control over logging - JavaWorld November 2000 |
Add the power of asynchronous processing to your JSPs - JavaWorld February 2001
Create custom JSP tags to use with JMS ost JavaServer Pages (JSP) developers that |
JSP best practices
Follow these tips for reusable and easily maintainable JavaServer Pages |
Master the Jxta shell,
Part 1
Master the Jxta shell,
Part 1 |
Rumble in the
jungle: J2EE versus .Net, Part
2
Rumble in the
jungle: J2EE versus .Net, Part
2 |
A first look at JavaServer Faces, Part I
A first look at JavaServer Faces, Part Learn how to implement Web-based user interfaces with JSF |
Interesting
...
Interesting
... |
Tracing in a multithreaded, multiplatform environment
Tracing in a multithreaded, multiplatform environment
In \"Use a consistent trace system for easier debugging,\" Scott Clee showed you how to trace and log from a custom class to provide a consistent tracing approach across your applications. This approa |
Ganymede
A log4j plugin to Eclipse that works similar to chainsaw (SocketServer). Includes color, filtering, detailed information, and saves settings. |
JSP 2.0: The New Deal, Part 4
JSP 2.0: The New Deal, Part 4
In this final part of the "JSP 2.0: The New Deal" series, we look at two new features that make it much easier to develop custom tag libraries: tag files and the new simplified tag-handler Java API. |
FindBugs, Part 2: Writing custom detectors
FindBugs, Part 2: Writing custom detectors
How to write custom detectors to find application-specific problems
In the first article in this series, I showed you how to set up and execute FindBugs. Now we'll take a look at FindBugs' most powerful fea |
Extend JavaSound to Play MP3, Ogg Vorbis, and More
This article deals with this plugin architecture and API, how to write and use a custom SPI implementation, how metadata such as title, artist, and copyright are exposed, and how multiple SPI implementations could be integrated in an application such as p |
Annotations in Tiger, Part 2: Custom annotations
Write your own annotations in Java 5
Part 1 of this series introduced annotations, the new metadata facility in J2SE 5.0, and focused on Tiger's basic built-in annotations. A more powerful related feature is support for writing your own annotations. In t |
Reporting Application Errors by Email
Reporting Application Errors by Email
It is common practice for server-side applications to log messages to files on the server's file system. These logs are a vital source of information for system administrators and the application development team. If |
Java Application Instrumentation with Log4J
Java Application Instrumentation with Log4J
Application metrics, such as performance metrics, are key to understanding and improving application efficiency. Profiling and monitoring tools yield valuable information on CPU and resource usage, including OS |
Develop MIDlets using the J2ME MIDP Development for NetBeans IDE 4.0
This release integrates with the J2ME Wireless Toolkit 2.2 to create a powerful environment for developing MIDP 2.0 applications. |
Write custom appenders for log4j
The Apache Software Foundation's log4j logging library is one of the better logging systems around. It's both easier to use and more flexible than Java's built-in logging system. |
Writing a Custom Counter Component
MIDP 2.0 introduced the javax.microedition.lcdui.CustomItem class. As the name implies, a developer can now create custom UI components by extending the CustomItem class. This tech tip designs and implements a UI component for displaying a numeric counter |
|
|
|