How to work with concurrent logs in golang?
There's no goroutine ID available from the runtime. Goroutines are used more liberally than threads in other languages, so the idea of which goroutine is handling a particular request or item may be less well-defined in a larger program.
So somehow you need to pass through an ID yourself for logging. You could just pass through an int
yourself, but the context
module is handy for this: apart from allowing user-defined values, it can handle item cancellation and deadlines which are also likely to be useful to you.
Here's a rough example of how it might be used. I've added a simple logging object that can be used as a context-aware logger. It logs the ID the context was created with. Probably this logger would go in its own package in a real program and support more of the log package's functions rather than just Printf
and Println
.
package main
import (
"fmt"
"log"
"sync"
"context"
)
type logger int
const loggerID = "logger_id"
func (l logger) Printf(s string, args ...interface{}) {
log.Printf("[id=%d] %s", l, fmt.Sprintf(s, args...))
}
func (l logger) Println(s string) {
log.Printf("[id=%d] %s\n", l, s)
}
func ctxWithLoggerID(ctx context.Context, id int) context.Context {
return context.WithValue(ctx, loggerID, id)
}
func getLogger(ctx context.Context) logger {
return logger(ctx.Value(loggerID).(int))
}
func main() {
ctx := context.Background()
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go hello(ctxWithLoggerID(ctx, i), &wg)
}
wg.Wait()
}
func hello(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
log := getLogger(ctx)
log.Printf("hello begin")
log.Println("hello, processing the hello message")
log.Printf("hello, end")
}
It's incidental to your question, but I added a sync.WaitGroup
to your program so that main
will wait until all of the workers are finished before exiting. That allows the code to be tested with a smaller number of workers (10 rather than the original 10000).