Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the NDC logs and how we can use it in our application and what is the significance of that

Tags:

java

struts

what is the NDC logs and how we can use it in our application and what is the significance of that...

like image 533
Pedantic Avatar asked Jun 06 '10 14:06

Pedantic


People also ask

What is NDC logging?

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.

Why do we use logger in Java?

A Logger object is used to log messages for a specific system or application component. Loggers are normally named, using a hierarchical dot-separated namespace. Logger names can be arbitrary strings, but they should normally be based on the package name or class name of the logged component, such as java.net or javax.

What is a Java logging library?

A Java logging framework is a computer data logging package for the Java platform. This article covers general purpose logging frameworks. Logging refers to the recording of activity by an application and is a common issue for development teams.

How do you do logs in Java?

The process of creating a new Logger in Java is quite simple. You have to use Logger. getLogger() method. The getLogger() method identifies the name of the Logger and takes string as a parameter.


3 Answers

Nested Diagnostic Contexts are particular to a thread.

Common uses are for recording info per-session (if one thread is used for a session), so you can log the originating client, username etc. and other cross-cutting attributes without:

  1. passing these attributes through the layers of your application
  2. explicitly logging them in each log statement. Log4j will output the NDC if PatternLayout is suitably configured.

See also Log4j's Mapped Diagnostic Contexts.

like image 67
Brian Agnew Avatar answered Oct 31 '22 20:10

Brian Agnew


NDC stands for "Nested Diagnostic Contexts", it's a feature of log4j. The most common usage of log4j is just to log stuff without any indication of what client request it was part of, with the result that, when your application runs in production with concurrent requests, all the log messages for all the requests are jumbled together in the log file and telling who did what is impossible. NDC allows you to mark log messages as belonging to particular clients so that you can distinguish who is doing what, without having separate loggers for each client.

like image 28
Nathan Hughes Avatar answered Oct 31 '22 20:10

Nathan Hughes


Logger are usually statically defined in the code, which make log sometimes hard to understand.

The NDC allows the dynamically push a parameter that will be displayed in every subsequent log line issued by the thread, until it is popped.

Useful if you want a log like:

[request=x] a
[request=y] a
[request=x] b
[request=x] c
[request=y] b
[request=x] d
[request=y] c
[request=y] d

(Disclaimer: I don't remember the exact formatting)

With just a,b,c,d it's hard to understand which thread does what. If you push and pop the request id dynamically, then it's easier to follow. Can also be used for other kinds of contextual information.

like image 37
ewernli Avatar answered Oct 31 '22 20:10

ewernli