diff --git a/m/m.go b/m/m.go new file mode 100644 index 0000000..e982a7b --- /dev/null +++ b/m/m.go @@ -0,0 +1,28 @@ +// Package m is the glue which holds all the other packages in this project +// together. While other packages in this project are intended to be able to be +// used separately and largely independently, this package combines them in ways +// which I specifically like. +package m + +import ( + "io" + + "github.com/mediocregopher/mediocre-go-lib/mcfg" + "github.com/mediocregopher/mediocre-go-lib/mlog" +) + +// Log returns a Logger which will automatically include with the log extra +// contextual information based on the Cfg and the given KVs +// +// If the cfg is nil then mlog.DefaultLogger is returned. +func Log(cfg *mcfg.Cfg, kvs ...mlog.KVer) *mlog.Logger { + fn := cfg.FullName() + l := mlog.DefaultLogger.WithWriteFn(func(w io.Writer, msg mlog.Message) error { + msg.Msg = fn + " " + msg.Msg + return mlog.DefaultWriteFn(w, msg) + }) + if len(kvs) > 0 { + l = l.WithKV(kvs...) + } + return l +} diff --git a/mdb/ps.go b/mdb/ps.go index 8235521..87992cf 100644 --- a/mdb/ps.go +++ b/mdb/ps.go @@ -7,6 +7,7 @@ import ( "time" "cloud.google.com/go/pubsub" + "github.com/mediocregopher/mediocre-go-lib/m" "github.com/mediocregopher/mediocre-go-lib/mcfg" "github.com/mediocregopher/mediocre-go-lib/mlog" oldctx "golang.org/x/net/context" @@ -31,6 +32,8 @@ type Message = pubsub.Message type PubSub struct { proj, credFile string *pubsub.Client + + log *mlog.Logger } // NewPubSub initializes and returns PubSub instance for the given projectID and @@ -40,9 +43,10 @@ func NewPubSub(ctx context.Context, projectID, credFile string) (*PubSub, error) proj: projectID, credFile: credFile, } + ps.log = mlog.DefaultLogger.WithKV(ps.KV()) var err error ps.Client, err = pubsub.NewClient(ctx, ps.proj, ps.clientOpts()...) - return ps, err + return ps, mlog.ErrWithKV(err, ps) } // CfgPubSub configures and returns a PubSub instance which will be usable once @@ -59,12 +63,14 @@ func CfgPubSub(cfg *mcfg.Cfg, defaultProject string) *PubSub { cfg.Start.Then(func(ctx context.Context) error { ps.proj = *proj ps.credFile = *credFile - mlog.Info("connecting to pubsub", &ps) + log := m.Log(cfg, ps.KV()) + log.Info("connecting to pubsub") psInner, err := NewPubSub(ctx, *proj, *credFile) if err != nil { return mlog.ErrWithKV(err, &ps) } ps = *psInner + ps.log = log return nil }) return &ps @@ -235,7 +241,7 @@ func (s *Subscription) Consume(ctx context.Context, fn ConsumerFunc, opts Consum ok, err := fn(context.Context(innerCtx), msg) if err != nil { - mlog.Warn("error consuming pubsub message", s, mlog.ErrKV(err)) + s.topic.ps.log.Warn("error consuming pubsub message", s, mlog.ErrKV(err)) } if ok { @@ -247,7 +253,7 @@ func (s *Subscription) Consume(ctx context.Context, fn ConsumerFunc, opts Consum if octx.Err() == context.Canceled || err == nil { return } else if err != nil { - mlog.Warn("error consuming from pubsub", s, mlog.ErrKV(err)) + s.topic.ps.log.Warn("error consuming from pubsub", s, mlog.ErrKV(err)) } } } @@ -339,7 +345,7 @@ func (s *Subscription) BatchConsume( } ret, err := fn(thisCtx, msgs) if err != nil { - mlog.Warn("error consuming pubsub batch messages", s, mlog.ErrKV(err)) + s.topic.ps.log.Warn("error consuming pubsub batch messages", s, mlog.ErrKV(err)) } for i := range thisGroup { thisGroup[i].retCh <- ret // retCh is buffered diff --git a/mhttp/mhttp.go b/mhttp/mhttp.go index 8cf530c..f281e50 100644 --- a/mhttp/mhttp.go +++ b/mhttp/mhttp.go @@ -6,6 +6,7 @@ import ( "context" "net/http" + "github.com/mediocregopher/mediocre-go-lib/m" "github.com/mediocregopher/mediocre-go-lib/mcfg" "github.com/mediocregopher/mediocre-go-lib/mlog" ) @@ -21,12 +22,13 @@ func CfgServer(cfg *mcfg.Cfg, h http.Handler) *http.Server { cfg.Start.Then(func(ctx context.Context) error { srv.Addr = *addr kv := mlog.KV{"addr": *addr} - mlog.Info("HTTP server listening", kv) + log := m.Log(cfg, kv) + log.Info("listening") go func() { err := srv.ListenAndServe() // TODO the listening log should happen here, somehow, now that we // know the actual address being listened on - mlog.Fatal("http server fataled", kv, mlog.ErrKV(err)) + log.Fatal("http server fataled", mlog.ErrKV(err)) }() return nil })