Class LoggerNDC

Description

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 push(), pop(), clear(), getDepth() and 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 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 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:

  1. require_once dirname(__FILE__).'/../../main/php/Logger.php';
  2. Logger::configure(dirname(__FILE__).'/../resources/ndc.properties');
  3. $logger Logger::getRootLogger();
  4.  
  5. LoggerNDC::push('conn=1234');
  6. $logger->debug("just received a new connection");
  7. LoggerNDC::push('client=ab23');
  8. $logger->debug("some more messages that can");
  9. $logger->debug("now related to a client");
  10. $logger->debug("back and waiting for new connections");

With the properties file:

  1. log4php.appender.default = LoggerAppenderEcho
  2. log4php.appender.default.layout = LoggerLayoutPattern
  3. log4php.appender.default.layout.conversionPattern="%d{Y-m-d H:i:s} %-5p %c %x: %m in %F at %L%n"
  4. log4php.rootLogger = DEBUG, default

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

  • version: $Revision: 1166187 $
  • since: 0.3

Located in /LoggerNDC.php (line 96)


	
			
Variable Summary
static mixed $stack
Method Summary
static void clear ()
static array get ()
static integer getDepth ()
static string peek ()
static string pop ()
static void push (string $message)
static void remove ()
static void setMaxDepth (integer $maxDepth)
Variables
static mixed $stack = array() (line 99)

This is the repository of NDC stack

  • access: private
Methods
static clear (line 109)

Clear any nested diagnostic information if any. This method is useful in cases where the same thread can be potentially used over and over in different unrelated contexts.

This method is equivalent to calling the setMaxDepth() method with a zero maxDepth argument.

  • access: public
static void clear ()
static get (line 117)

Never use this method directly, use the LoggerLoggingEvent::getNDC() method instead.

  • access: public
static array get ()
static getDepth (line 127)

Get the current nesting depth of this diagnostic context.

static integer getDepth ()
static peek (line 156)

Looks at the last diagnostic context at the top of this NDC without removing it.

The returned value is the value that was pushed last. If no context is available, then the empty string "" is returned.

  • return: The innermost diagnostic context.
  • access: public
static string peek ()
static pop (line 140)

Clients should call this method before leaving a diagnostic context.

The returned value is the value that was pushed last. If no context is available, then the empty string "" is returned.

  • return: The innermost diagnostic context.
  • access: public
static string pop ()
static push (line 172)

Push new diagnostic context information for the current thread.

The contents of the message parameter is determined solely by the client.

  • access: public
static void push (string $message)
  • string $message: The new diagnostic context information.
static remove (line 179)

Remove the diagnostic context for this thread.

  • access: public
static void remove ()
static setMaxDepth (line 197)

Set maximum depth of this diagnostic context. If the current depth is smaller or equal to maxDepth, then no action is taken.

This method is a convenient alternative to multiple pop() calls. Moreover, it is often the case that at the end of complex call sequences, the depth of the NDC is unpredictable. The setMaxDepth() method circumvents this problem.

static void setMaxDepth (integer $maxDepth)
  • integer $maxDepth

Documentation generated on Sat, 18 Feb 2012 22:32:26 +0000 by phpDocumentor 1.4.3