Network Logging

Problem

Your class is running inside a server container, and its debugging output is hard to obtain.

Solution

Use a network-based logger like the one shown here.

Discussion

Getting the debug output from a desktop client is fairly easy on most operating systems. But if the program you want to debug is running in a “container” like a servlet engine or EJB server, it can be difficult to obtain debugging output, particularly if the container is running on a remote computer. It would be convenient if you could have your program send messages back to a program on your desktop machine for immediate display. Needless to say, it’s not that hard to do this with Java’s socket mechanism. I have written a small, simple API to handle this type of logging function. The program being debugged is the “client” from a socket point of view -- even though it may be running in a server-side container such as a web server or application server -- since the “network client” is the program that initiates the connection. The program that runs on your desktop machine is the “server” program for sockets, since it waits for a connection to come along.

Example 16-8 is a simple client program called NetLogSimple.

Example 16-8. NetLogSimple.java

/* A simple example of using the NetLog program. * Unrealistic in that it's standalone; this API is * intended for use inside another program, possibly * a servlet or EJB. */ public class NetLogSimple { public static void main(String[] args) throws java.io.IOException ...

Get Java Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.