The NDC class implements nested diagnostic contexts as 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.
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::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
- 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
- When exiting a thread make sure to call
There is no penalty for forgetting to match each
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,
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.
Author: VxR <firstname.lastname@example.org>
Version: $Revision: 1.5 $
Located at libraries/log4php.debug/LoggerNDC.php