如何在日志中识别线程或类似的内容?

huangapple go评论100阅读模式
英文:

How can I identify threads or similar in log?

问题

我正在使用Go语言和Echo框架进行开发。

我正在设计一个日志记录器,我想要记录类似于线程ID的信息到日志中,但是我不知道一个好的方法。

例如,如果同时进行多个访问的登录处理,其中一个成功了,另一个失败了,通过查看日志无法确定是哪个失败了。

经过调查,Go语言中存在goroutine的ID,但是我不应该获取它,
原本说goroutine在同一个线程中是不同的。

我应该如何标识线程?
或者是否有一个可以将它们放入日志中的包?

如果我使用了错误的表达方式,对不起,因为英语不是我的母语。
谢谢。

英文:

I am developing in Go language, echo framework.

I am designing a logger and I want to log the equivalent of thread ID to the log, but I do not know a good method.

For example, if login processing is performed at the same time with multiple accesses, it succeeds on one side and fails on one side
It can not be determined by looking at the log which failed.

After examining, in Go language goroutine's id exists, but I should not get it,
Originally it was said that goroutine was not the same in the same thread.

How can I identify threads?
Or is there a package to put them in the log?

I'm sorry if I used the wrong expression as English is not my mother tongue.
Thanks.

答案1

得分: 2

在Go语言中,没有与goroutine对应的线程ID的等价物。

你可以创建一个context并将一个唯一的ID(最好是请求ID)附加到它上面。让所有的函数都接受一个context参数。让日志库接收这个context并打印出唯一的ID。通过这种方式使用context,你可以在日志中共享更多的信息。

英文:

There is no equivalent of thread id in GoLang for go routines.

What you may do is create a context and attach a unique id (request id preferably) to it.Let all the functions accept a context aswell. Let the logger library take the context and print the unique id. By using context in this way you can share even for info for the log

答案2

得分: 0

我个人更喜欢在Go语言中使用log4j Log4J in Go

它非常容易使用,而且具有基于时间和大小进行文件轮转的所有功能。此外,您还可以存储包含文件名和行号的完整错误堆栈。

英文:

I personally prefer log4j in go Log4J in Go.

It's quite easy to use and also it has all the facilities such as file rotation on the basis of time and size. Also, you can store complete error stack which contains the file name and line number.

答案3

得分: 0

在Go语言中,没有与go协程对应的线程ID的等价物。最近(2022年),TimAndy提出了自己的库timandy/routine

package main

import (
	"fmt"
	"time"

	"github.com/timandy/routine"
)

func main() {
	goid := routine.Goid()
	fmt.Printf("当前 goid: %v\n", goid)
	go func() {
		goid := routine.Goid()
		fmt.Printf("子协程 goid: %v\n", goid)
	}()

	// 等待子协程执行完毕。
	time.Sleep(time.Second)
}

在这个例子中,主函数启动了一个新的goroutine,所以Goid()返回的是主协程的ID为1,子协程的ID为6

英文:

> There is no equivalent of thread id in GoLang for go routines.

More recently (2022) TimAndy has proposed their own library timandy/routine

package main

import (
	"fmt"
	"time"

	"github.com/timandy/routine"
)

func main() {
	goid := routine.Goid()
	fmt.Printf("cur goid: %v\n", goid)
	go func() {
		goid := routine.Goid()
		fmt.Printf("sub goid: %v\n", goid)
	}()

	// Wait for the sub-coroutine to finish executing.
	time.Sleep(time.Second)
}

> In this example, the main function starts a new goroutine, so Goid() returns the main coroutine 1 and the child coroutine 6.

huangapple
  • 本文由 发表于 2017年1月30日 18:05:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/41933194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定