Search code examples
logginggogorilla

Go log thread id in Gorilla Handler


How do we get the thread id or any other unique Id of the http request being handled by the handler in logging inside Gorilla Handlers?
In Java, when Tomcat or other container handles multiple http requests, thread id helps to track all the log messages for respective http request handling.
What is the equivalent in Go? Given a Rest API developed using Gorilla library, how do I track all log statements of specific http request inside a handler processing?


Solution

  • Based on https://groups.google.com/forum/#!searchin/golang-nuts/Logging$20http$20thread/golang-nuts/vDNEH3_vMXQ/uyqGEwdchzgJ, ThreadLocal concept is not possible with Go.

    Every where you need logging, it requires to pass in http Request instance so that the context associated with the request can be retrieved and one can fetch the unique ID from this context for the request. But its not practical to pass Request instance to all the layers/methods.