寻找用于记录的呼叫或线程ID [英] looking for a call or thread id to use for logging

查看:137
本文介绍了寻找用于记录的呼叫或线程ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重新编写用golang编写的小型web应用程序的日志记录。由于外部要求日志记录已被隔离在一个位置,因此我们稍后可能会切换到日志记录服务器。 (不是我的想法 - 我承诺....)然而,我们现在可以记录常用的东西,如日期/时间,行号,用户和消息,主要使用标准库的一部分和我们传递的用户/会话结构周围。 但是 - 这里提出了这个问题 - 在较低级别的方法中,为了记录日志而将会话传递给用户名是很浪费的。所以我想找一些其他东西用来在日志文件中找到一个特定的请求。我确信有一些明显的我没有想到。



到目前为止的想法:


  • Java日志框架可以打印出线程ID在这种情况下,这也足够好。只是它在golang中被称为其他东西?

  • 使用户/会话struct事物以某种方式全局访问。 (仍然需要传递会话ID,除非有一个线程用作查找键)返回主题1)
  • 放弃并传递用户/会话结构。

  • 不会在最低级别上记录错误,而只会在用户/会话结构可用时记录错误。行号不会很好。



我们使用大猩猩的部分作为网页,除此之外大部分是标准



对此的建议和想法?

滥用的可能性很高,因此无法访问Go中当前goroutine的标识符。这可能看起来很严厉,但这实际上保留了Go包生态系统的一个重要属性:如果你启动一个新的goroutine来做某件事情并不重要。



也就是说,对于函数F的任何方法:

  F()
  


几乎完全等价于:完成:= make(chan struct {})
去func(){
延迟关闭(完成)
F()
}
<-done

(这个几乎来自事实:如果F恐慌,恐慌
将不会捕获的原始goroutine)。



这也适用于日志记录 - 如果您使用当前goroutine
来推断当前用户,则任何启动新的goroutine
将会破坏这个假设,而你的日志
将不包含预期的信息。



你需要传递一些信息种类的上下文。


I am reworking the logging of our little web application written in golang. Due to external requirements logging has been isolated in one place so we can potentially switch in a logging server later on. (Not my idea - I promise....) Nevertheless we are now able to log the common things like date/time, line number, user and the message mostly using parts of the standard library and a user/session struct we are passing around.

But - and here comes the question - in lower level methods it is a waste to pass in the session just to get to the user name for the sake of logging. So I would like to find something else to use to find one specific request in the log files. I am sure there are something obvious I haven't thought of.

Ideas so far:

  • Java logging frameworks can print out the thread id and that would be good enough in this case also. Just that it is called something else in golang?
  • make the user/session struct thing accesible globally somehow. (Still need to pass the session id around unless there are a thread to use as lookup key. Back to idea number 1.)
  • give up and pass the user/session struct around anyway.
  • don't log errors on the lowest level but only when the user/session struct is available. The line number won't be that good though.

We are using parts of gorilla for web things and apart from that mostly the standard library.

Suggestions and ideas about this?

解决方案

Because of the high potential for abuse, there is no way to access an identifier for the current goroutine in Go. This may seem draconian, but this actually preserves an important property of the Go package ecosystem: it does not matter if you start a new goroutine to do something.

That is, for any method of function F:

F()

is almost exactly equivalent to:

done := make(chan struct{})
go func() {
   defer close(done)
   F()
}
<-done

(The "almost" comes from the fact that if F panics, the panic will not be caught by the original goroutine).

This applies to logging too - if you were using the current goroutine to infer the current user, any code that started a new goroutine as above would break that assumption, and your logging would not contain the expected information.

You'll need to pass around some kind of context.

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

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