Class LoggerNDC
The NDC class implements nested diagnostic contexts.
NDC was defined by Neil Harrison in the article "Patterns for Logging Diagnostic Messages" part of the book "Pattern Languages of Program Design 3" edited by Martin et al.
A Nested Diagnostic Context, or NDC in short, is an instrument to distinguish interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.
This class is similar to the LoggerMDC
class except that it is based
on a stack instead of a map.
Interleaved log output can still be meaningful if each log entry from different contexts had a distinctive stamp. This is where NDCs come into play.
Note that NDCs are managed on a per thread basis.
NDC operations such as LoggerNDC::push()
, LoggerNDC::pop()
, LoggerNDC::clear()
, LoggerNDC::getDepth()
and LoggerNDC::setMaxDepth()
affect the NDC of the current
thread only. NDCs of other threads remain unaffected.
For example, a servlet can build a per client request NDC consisting the
clients host name and other information contained in the the request.
Cookies are another source of distinctive information. To build an NDC
one uses the LoggerNDC::push()
operation.
Simply put,
- Contexts can be nested.
- When entering a context, call LoggerNDC::push() As a side effect, if there is no nested diagnostic context for the current thread, this method will create it.
- When leaving a context, call LoggerNDC::pop()
- When exiting a thread make sure to call
LoggerNDC::remove()
There is no penalty for forgetting to match each push operation with a corresponding pop, except the obvious mismatch between the real application context and the context set in the NDC.
If configured to do so, LoggerPatternLayout and LoggerLayoutTTCC instances automatically retrieve the nested diagnostic context for the current thread without any user intervention. Hence, even if a servlet is serving multiple clients simultaneously, the logs emanating from the same code (belonging to the same category) can still be distinguished because each client request will have a different NDC tag.
Example:
{@example ../../examples/php/ndc.php 19}
With the properties file:
{@example ../../examples/resources/ndc.properties 18}
Will result in the following (notice the conn and client ids):
2009-09-13 19:04:27 DEBUG root conn=1234: just received a new connection in src/examples/php/ndc.php at 23 2009-09-13 19:04:27 DEBUG root conn=1234 client=ab23: some more messages that can in src/examples/php/ndc.php at 25 2009-09-13 19:04:27 DEBUG root conn=1234 client=ab23: now related to a client in src/examples/php/ndc.php at 26 2009-09-13 19:04:27 DEBUG root : back and waiting for new connections in src/examples/php/ndc.php at 29
public static
|
|
public static
array
|
|
public static
integer
|
|
public static
string
|
|
public static
string
|
|
public static
|
|
public static
|
|
public static
|
#
setMaxDepth( integer $maxDepth )
Set maximum depth of this diagnostic context. If the current depth is smaller or equal to maxDepth, then no action is taken. |