What are NDC logs and how can we use it in our application, and what is the significance of this

what are NDC logs and how can we use it in our application and what is the meaning of this ...

+4
source share
3 answers

Nested diagnostic contexts refer to the stream.

Common uses are for recording information per session (if one stream is used for the session), so you can register the original client, username, etc. and other pass-through attributes without:

  • passing these attributes across your application layers
  • explicitly write them to each log statement. Log4j will output NDC if PatternLayout is configured accordingly.

See also Log4j Mapped Diagnostic Contexts.

+4
source

NDC stands for Nested Diagnostic Contexts, a feature of log4j. The most common use of log4j is simply to record a log without any indication of which client request it was part of, so that when your application starts with simultaneous requests, all the log messages for all requests are mixed together in the log file and tell, who did what is impossible. NDC allows you to mark journal messages as belonging to specific clients so that you can distinguish who does what without having separate logs for each client.

+3
source

A logger is usually statically defined in code, which makes the log sometimes difficult to understand.

NDC allows you to dynamically push parameter that will be displayed in each subsequent line of the log issued by the stream, as long as it is a pop ped.

Useful if you want to use the log:

 [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 do not remember the exact formatting)

With just a,b,c,d it's hard to figure out which thread is doing something. If you push and pop the request identifier dynamically, then it is easier to follow. It can also be used for other types of contextual information.

+2
source

Source: https://habr.com/ru/post/1312003/


All Articles