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?
相关问题
- Golang mongodb aggregation
- I want to trace logs using a Macro multi parameter
- Error message 'No handlers could be found for
- How to flatten out a nested json structure in go
- convert logback.xml to log4j.properties
相关文章
- how do I log requests and responses for debugging
- Can I run a single test in a suite?
- How to check if a request was cancelled
- Is it possible to implement an interface with unex
- How to access value of first index of array in Go
- Android Studio doesn't display logs by package
- Embedded Interface
- Stacktrace does not print in Glassfish 4.1 Cluster
The gorilla/handlers library doesn't provide a way to do this by default: the logging functions there log in Apache formats, which don't provide for this.
Also keep in mind that a "thread ID" doesn't make sense here - you want a request ID that is associated with a
*http.Request
.You could write your own RequestID middleware that creates an ID and stores in the request context for other middleware/handlers to retrieve as-needed:
Keep in mind that the code above is not tested. Written off the top of my head in the Playground, so please let me know if there's a bug.
Improvements you could consider beyond this basic example:
Note that under extremely high loads (e.g. tens of thousands of req/s - tens of millions of hits per day), this may not be performant, but is unlikely to be a bottleneck for > 99% of users.
PS: I may look at providing a
handlers.RequestID
implementation in the gorilla/handlers library at some point - if you'd like to see it, raise an issue on the repo and I'll see if I can find time to implement a more complete take on the above.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.