A simple go log.
go get github.com/haormj/log
package main
import (
"context"
"github.com/haormj/log"
)
func main() {
l := log.Logger.Clone()
l.With("main", "I'm main")
ctx := log.NewContext(context.Background(), l)
hello(ctx)
world(ctx)
l.Info("main", "end")
}
func hello(ctx context.Context) {
l, _ := log.FromContext(ctx)
l.With("hello", "1")
l.Infow("this is hello function")
}
func world(ctx context.Context) {
l, _ := log.FromContext(ctx)
l.With("world", 2)
l.Infof("this is %s function", "world")
}
If pass Log
through Context
, pay attention to the life cycle of the Context
to prevent memory increase