From 8bd0664ba002d832f104f5207ef59da490669475 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sat, 15 Jun 2019 17:35:17 -0600 Subject: [PATCH] mlog: refactor to use Components instead of Contexts for storing Logger --- mlog/cmp.go | 28 +++++++++++++++++++++++ mlog/ctx.go | 58 ----------------------------------------------- mlog/mlog.go | 6 ++--- mlog/mlog_test.go | 2 +- 4 files changed, 32 insertions(+), 62 deletions(-) create mode 100644 mlog/cmp.go delete mode 100644 mlog/ctx.go diff --git a/mlog/cmp.go b/mlog/cmp.go new file mode 100644 index 0000000..03d3e4f --- /dev/null +++ b/mlog/cmp.go @@ -0,0 +1,28 @@ +package mlog + +import ( + "github.com/mediocregopher/mediocre-go-lib/mcmp" +) + +type cmpKey int + +// SetLogger sets the given logger onto the Component. The logger can later be +// retrieved from the Component, or any of its children, using From. +func SetLogger(cmp *mcmp.Component, l *Logger) { + cmp.SetValue(cmpKey(0), l) +} + +// DefaultLogger is an instance of Logger which is returned by From when a +// Logger hasn't been previously set with SetLogger on the passed in Component. +var DefaultLogger = NewLogger() + +// From returns the Logger which was set on the Component, or one of its +// ancestors, using SetLogger. If no Logger was ever set then DefaultLogger is +// returned. +func From(cmp *mcmp.Component) *Logger { + l, _ := cmp.Value(cmpKey(0)).(*Logger) + if l == nil { + l = DefaultLogger + } + return l +} diff --git a/mlog/ctx.go b/mlog/ctx.go deleted file mode 100644 index 8d07a80..0000000 --- a/mlog/ctx.go +++ /dev/null @@ -1,58 +0,0 @@ -package mlog - -import ( - "context" -) - -type ctxKey int - -// WithLogger returns the Context with the Logger carried by it. -func WithLogger(ctx context.Context, l *Logger) context.Context { - return context.WithValue(ctx, ctxKey(0), l) -} - -// DefaultLogger is an instance of Logger which is returned by From when a -// Logger hasn't been previously WithLogger on the Contexts passed in. -var DefaultLogger = NewLogger() - -// From looks at each context and returns the Logger from the first Context -// which carries one via a WithLogger call. If none carry a Logger than -// DefaultLogger is returned. -func From(ctxs ...context.Context) *Logger { - for _, ctx := range ctxs { - if l, _ := ctx.Value(ctxKey(0)).(*Logger); l != nil { - return l - } - } - return DefaultLogger -} - -// Debug is a shortcut for -// mlog.From(ctxs...).Debug(desc, ctxs...) -func Debug(descr string, ctxs ...context.Context) { - From(ctxs...).Debug(descr, ctxs...) -} - -// Info is a shortcut for -// mlog.From(ctxs...).Info(desc, ctxs...) -func Info(descr string, ctxs ...context.Context) { - From(ctxs...).Info(descr, ctxs...) -} - -// Warn is a shortcut for -// mlog.From(ctxs...).Warn(desc, ctxs...) -func Warn(descr string, ctxs ...context.Context) { - From(ctxs...).Warn(descr, ctxs...) -} - -// Error is a shortcut for -// mlog.From(ctxs...).Error(desc, ctxs...) -func Error(descr string, ctxs ...context.Context) { - From(ctxs...).Error(descr, ctxs...) -} - -// Fatal is a shortcut for -// mlog.From(ctxs...).Fatal(desc, ctxs...) -func Fatal(descr string, ctxs ...context.Context) { - From(ctxs...).Fatal(descr, ctxs...) -} diff --git a/mlog/mlog.go b/mlog/mlog.go index f5141e2..5eb9275 100644 --- a/mlog/mlog.go +++ b/mlog/mlog.go @@ -107,8 +107,8 @@ type MessageJSON struct { Level string `json:"level"` Description string `json:"descr"` - // path -> key -> value - Annotations map[string]map[string]string `json:"annotations,omitempty"` + // key -> value + Annotations map[string]string `json:"annotations,omitempty"` } // DefaultHandler initializes and returns a Handler which will write all @@ -131,7 +131,7 @@ func defaultHandler(out io.Writer) Handler { } if len(msg.Contexts) > 0 { ctx := mctx.MergeAnnotations(msg.Contexts...) - msgJSON.Annotations = mctx.Annotations(ctx).StringMapByPath() + msgJSON.Annotations = mctx.Annotations(ctx).StringMap() } return enc.Encode(msgJSON) diff --git a/mlog/mlog_test.go b/mlog/mlog_test.go index 86aa8bd..cc23be0 100644 --- a/mlog/mlog_test.go +++ b/mlog/mlog_test.go @@ -60,7 +60,7 @@ func TestLogger(t *T) { l.Error("buz", mctx.Annotate(ctx, "a", "b", "c", "d")) massert.Require(t, assertOut(`{"level":"WARN","descr":"baz"}`), - assertOut(`{"level":"ERROR","descr":"buz","annotations":{"/":{"a":"b","c":"d"}}}`), + assertOut(`{"level":"ERROR","descr":"buz","annotations":{"a":"b","c":"d"}}`), ) l2 := l.Clone()