|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 5
Most real-world systems must deal with multiple clients simultaneously. In a typical multithreaded implementation of such a system, different threads will handle different clients. Considering that, logging is especially well suited to tracing and debugging complex distributed applications. A common approach to differentiate the logging output of one client from another is to instantiate a new separate category for each client. However, that approach promotes category proliferation and increases the management overhead of logging.
In a lighter technique, you can uniquely stamp each log request initiated from the same client interaction, an approach described
by Neil Harrison (see Resources). To uniquely stamp each request, the user pushes contextual information into the Nested Diagnostic Context (NDC). The NDC class is shown below:
public class NDC {
// Used when printing the diagnostic
public static String get();
// Remove the top of the context from the NDC.
public static String pop();
// Add diagnostic context for the current thread.
public static void push(String message);
// Remove the diagnostic context for this thread.
public static void remove();
}
The NDC is managed per thread as a stack of contextual information. Note that all of org.log4j.NDC's methods are static. Assuming that NDC printing is turned on, every time a log request is made, the appropriate log4j component
will include the entire NDC stack for the current thread in the log output. That is done without the intervention of the user, who is responsible
only for placing the correct information in the NDC by using the push() and pop() methods at a few well-defined points in the code. In contrast, the per-client category approach commands extensive changes
in the code.
To illustrate that point, let us take the example of a servlet delivering content to numerous clients. The servlet can build the NDC at the very beginning of the request before executing other code. The contextual information can be the client's host name and other information inherent to the request, typically information contained in cookies. Hence, even if the servlet is serving multiple clients simultaneously, the logs initiated by the same code (that is, belonging to the same category) can still be distinguished because each client request will have a different NDC stack. Contrast that with the complexity of passing a freshly instantiated category to all code exercised during the client's request.
Nevertheless, some sophisticated applications, such as virtual hosting Web servers, must log differently, depending on the virtual host context and the software component issuing the request. The most recent log4j release supports multiple hierarchy trees, an enhancement that allows each virtual host to possess its own copy of the category hierarchy.
One of the often-cited arguments against logging stems from its computational cost. That is a legitimate concern, as even moderately sized applications can generate thousands of log requests. Much effort was spent measuring and tweaking logging performance. Log4j claims to be fast and flexible: speed first, flexibility second.