Differences between revisions 3 and 4
Revision 3 as of 2008-04-17 17:14:07
Size: 2849
Editor: JacobAnawalt
Comment: Highlighted conversion pattern and fixed example output
Revision 4 as of 2009-09-20 23:45:57
Size: 2851
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
The [http://logging.apache.org/log4cxx/manual.html log4cxx] introduction gives a use case where you have a thread-per-client program. With that setup you could push onto the NDC stack some client identifier like it's IP or user id. Then every logging statement that goes through a layout requesting the NDC will be able to include the client's information. The [[http://logging.apache.org/log4cxx/manual.html|log4cxx]] introduction gives a use case where you have a thread-per-client program. With that setup you could push onto the NDC stack some client identifier like it's IP or user id. Then every logging statement that goes through a layout requesting the NDC will be able to include the client's information.

The Nested Diagnostic Context (NDC) is a per-thread dynamic stack-based context that you can add to the logging output. If you are using a PatternLayout the conversion character to access the stack is %x.

The log4cxx introduction gives a use case where you have a thread-per-client program. With that setup you could push onto the NDC stack some client identifier like it's IP or user id. Then every logging statement that goes through a layout requesting the NDC will be able to include the client's information.

Another use for the NDC would be to push the name of the function or other code block on at the top of a block that logs instead of creating a logger for that specific block. This is a way to add some context to the log message without getting the line or file information. It doesn't allow filtering by Appender or Logger code as you might with an appender threshold or a logger's level.

Either use is a lot cleaner and more flexible than having to add that information manually to each log output. You can choose to include that information or not and where to include it from your configuration file. If you need two or more non-stack thread-specific data items the MappedDiagnosticContext (MDC) may be what you need.

// Example without NDC
void MyClass::MyFn()
{
    std::stringstream buf;
    buf << clientInfo << " MyFn() Starting";
    LOG4CXX_DEBUG(logger,buf.str()); // %m output is "clientInfo MyFn() Starting"
}

// Example with NDC
void MyClass::MyNdcRaiiFn()
{
    log4cxx::NDC ndc("MyNdcRaiiFn"); // client info context was pushed on when thread was created
    LOG4CXX_DEBUG(logger,"Starting"); // output could include "clientInfo MyNdcRaiiFn()"
}

// Another example with NDC using javaesque push/pop.
void MyClass::MyNdcFn()
{
    log4cxx::NDC::push("MyNdcRaiiFn"); // client info context was pushed on when thread was created
    LOG4CXX_DEBUG(logger,"Starting"); // output could include "clientInfo MyNdcRaiiFn()"
    log4cxx::NDC::pop();
}

See Also

Mailing List Archives


CategoryApiDocs

NestedDiagnosticContexts (last edited 2009-09-20 23:45:57 by localhost)