3

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
maxwell
  • 83
  • 1
  • 7
  • See also http://stackoverflow.com/questions/33947545/go-log-thread-id-in-gorilla-handler – nos Jan 30 '17 at 10:07

3 Answers3

2

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

I159
  • 29,741
  • 31
  • 97
  • 132
0

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.

shivendra pratap singh
  • 1,318
  • 1
  • 13
  • 18
0

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250