MDC and NDC in log4j

Source: Internet
Author: User

NDC and MDC

NDC (nested diagnostic context) and MDC (mapped diagnostic context) are two very useful classes of log4j, which are used to store context infomation of applications ), this makes it easy to use the context information in the log.

 

NDC uses a stack-like mechanism to push and pop context information. Each thread stores context information independently. For example, a servlet can create a corresponding NDC for each request, store the client address and other information.

When using this function, we should try to make sure that the relevant information is used in NDC when entering a context. push (Message); NDC is used when the context is left. pop () deletes the information. In addition, due to some design problems, we also need to ensure that NDC. Remove () is used to clear the memory at the end of the current thread. Otherwise, memory leakage will occur.

After the context information is stored, We can output the information during the log. Use "% x" in the corresponding patternlayout to output the stored context information. The following is an example of patternlayout:

% R [% T] %-5 p % c {2} % x-% m % N

The most important advantage of using NDC is that when we want to output some context information, we do not need logger to look for this information, but store it in a proper location, then modify patternlayout in the configuration file. In the latest log4j 1.3 version, an org. Apache. log4j. Filters. ndcmatchfilter is added

Accept or reject a log based on the information stored in NDC.

 

MDC and NDC are very similar. The difference is that MDC uses a map-like mechanism internally to store information. The context information is also stored independently by each thread, the difference is that information is stored in "map" based on their key values. Corresponding Method, MDC. put (Key, value); MDC. remove (key); MDC. get (key); When configuring patternlayout, use: % x {key} to output the corresponding value. Similarly, MDC also has an org. Apache. log4j. Filters. mdcmatchfilter. Note that MDC is independent of threads, but a sub-thread will automatically obtain the copy of a parent thread MDC.

Whether NDC or MDC is selected depends on whether the context information to be stored is in the stack or key/value format.

 

Dynamically modify log configurations

During the development process, we often encounter the situation of modifying the log4j configuration. In this case, frequent application restart is obviously unacceptable. Fortunately, log4j provides the ability to automatically reload the configuration file. After the configuration file is modified, it will re-load the configuration. In versions 1.2 and earlier, domconfigurator and propertyconfigurator both provide the configureandwatch method to monitor the specified configuration file and set the check interval.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.