0
votes

I have a java web service app. The web method has one parameter is requestId from the client. My web service app has to process multiple requests concurrent, so it must have some way to trace the log because request's log are mixed. I plan to rename the thread name to requestId and print out the thread name for getting easy to trace the log. My question is: would it be safe if I rename the thread name?

2
It's safe, but it's kind of dumb. What if more than one thread is working on the request? What if a thread is doing a little bit of work for each of a large number of requests in quick succession? Logging this way locks you into a "thread per request" design. - David Schwartz

2 Answers

2
votes

If you're using log4j, checkout their Nested Diagnostic Context (NDC) and Mapped Diagnostic Context (MDC) concepts which allows you to put unstructured data like that into all log outputs.

Other libraries probably have similar concepts.

0
votes

Yes. You can rename the thread name anytime.