如何在日志中识别线程或类似线程? [英] How can I identify threads or similar in log?

查看:74
本文介绍了如何在日志中识别线程或类似线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Go语言的echo框架进行开发.

I am developing in Go language, echo framework.

我正在设计一个记录器,我想将等效于线程ID的日志记录到日志中,但是我不知道一个好方法.

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.

检查后,Go语言中存在goroutine的ID,但我不应该得到它,最初,有人说goroutine在同一线程中是不同的.

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.

推荐答案

在GoLang中,go例程没有等效的线程ID.

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

您可能要做的是创建一个上下文并附加一个唯一的ID(最好是请求ID)让所有函数也接受上下文.让记录器库获取上下文并打印唯一的ID.通过以这种方式使用上下文,您甚至可以共享日志信息

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

这篇关于如何在日志中识别线程或类似线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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