org.apache.log4j
Class NDC

java.lang.Object
  extended by org.apache.log4j.NDC

public final class NDC
extends Object


Method Summary
static void clear()
          Clear any nested diagnostic information if any.
static Stack cloneStack()
          Clone the diagnostic context for the current thread.
static String get()
          Never use this method directly.
static int getDepth()
          Get the current nesting depth of this diagnostic context.
static void inherit(Stack<String> stack)
          Inherit the diagnostic context of another thread.
static String peek()
          Looks at the last diagnostic context at the top of this NDC without removing it.
static String pop()
          Clients should call this method before leaving a diagnostic context.
static void push(String message)
          Push new diagnostic context information for the current thread.
static void remove()
          Remove the diagnostic context for this thread.
static void setMaxDepth(int maxDepth)
          Set maximum depth of this diagnostic context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

clear

public static void clear()
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(int) method with a zero maxDepth argument.


cloneStack

public static Stack cloneStack()
Clone the diagnostic context for the current thread.

Internally a diagnostic context is represented as a stack. A given thread can supply the stack (i.e. diagnostic context) to a child thread so that the child can inherit the parent thread's diagnostic context.

The child thread uses the inherit method to inherit the parent's diagnostic context.

Returns:
Stack A clone of the current thread's diagnostic context.

inherit

public static void inherit(Stack<String> stack)
Inherit the diagnostic context of another thread.

The parent thread can obtain a reference to its diagnostic context using the cloneStack() method. It should communicate this information to its child so that it may inherit the parent's diagnostic context.

The parent's diagnostic context is cloned before being inherited. In other words, once inherited, the two diagnostic contexts can be managed independently.

In java, a child thread cannot obtain a reference to its parent, unless it is directly handed the reference. Consequently, there is no client-transparent way of inheriting diagnostic contexts. Do you know any solution to this problem?

Parameters:
stack - The diagnostic context of the parent thread.

get

public static String get()
Never use this method directly.

Returns:
The string value of the specified key.

getDepth

public static int getDepth()
Get the current nesting depth of this diagnostic context.

Returns:
int The number of elements in the call stack.
See Also:
setMaxDepth(int)

pop

public static String pop()
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.

Returns:
String The innermost diagnostic context.

peek

public static String peek()
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.

Returns:
String The innermost diagnostic context.

push

public static void push(String message)
Push new diagnostic context information for the current thread.

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

Parameters:
message - The new diagnostic context information.

remove

public static void remove()
Remove the diagnostic context for this thread.

Each thread that created a diagnostic context by calling push(java.lang.String) should call this method before exiting. Otherwise, the memory used by the thread cannot be reclaimed by the VM.

As this is such an important problem in heavy duty systems and because it is difficult to always guarantee that the remove method is called before exiting a thread, this method has been augmented to lazily remove references to dead threads. In practice, this means that you can be a little sloppy and occasionally forget to call remove before exiting a thread. However, you must call remove sometime. If you never call it, then your application is sure to run out of memory.


setMaxDepth

public static void setMaxDepth(int maxDepth)
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.

For example, the combination

 void foo() {
    int depth = NDC.getDepth();

    ... complex sequence of calls

    NDC.setMaxDepth(depth);
 }
 

ensures that between the entry and exit of foo the depth of the diagnostic stack is conserved.

Parameters:
maxDepth - The maximum depth of the stack.
See Also:
getDepth()


Copyright © 1999-2014 Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.