在Gorilla Handler中记录线程ID [英] Go log thread id in Gorilla Handler

查看:129
本文介绍了在Gorilla Handler中记录线程ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何获取线程标识符或处理程序在 Gorilla Handlers 中记录的http请求的任何其他唯一标识符


在Java中,当Tomcat或其他容器处理多个http请求时,线程ID有助于跟踪各个http请求处理的所有日志消息。


Go 中的等价物是什么?鉴于使用 Gorilla 库开发的Rest API,我如何跟踪处理程序中特定http请求的所有日志语句?

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?

推荐答案

基于 https: //groups.google.com/forum/#!searchin/golang-nuts/Logging $ 20http $ 20thread / golang-nuts / vDNEH3_vMXQ / uyqGEwdchzgJ, ThreadLocal Go的概念是不可能的。

每个需要日志记录的地方,都需要传入http Request 实例,以便可以检索与请求关联的上下文,并可以获取唯一该请求的上下文ID。
但是将Request实例传递给所有图层/方法并不现实。

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.

这篇关于在Gorilla Handler中记录线程ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆