Add a global value to all LogLevel only once which will affect everywhere

243 Views Asked by At

I have added Logger in my code at serveral places (Log4j). Like Logger.info(" some thing "); My log is something like this at so many places,

Timestamp LogLevel SometText SomeMoreText

Whenever any new request comes, I want to add some value like ID:CurrentValuetime in every logLevel which will be same till the request is there to all LogLevel so my logs should be like

Timestamp LogLevel ID:CurrentValuetime SometText SomeMoreText

ID:CurrentValuetime Should be same at all places it should be global and final which should get appended automatically to all loglevel

How can I achieve this ?

1

There are 1 best solutions below

0
On

Achieved it with the help of MDC

Created a servlet filter and with the help of

 MDC.put("UniqueID", UIDValue);

I was able to add unique value into log at once.

try {
             chain.doFilter(request, response);
            } finally {           
                MDC.remove("UniqueID");
              }

And remove value once request is complete