mlog: refactor to use Components instead of Contexts for storing Logger

This commit is contained in:
Brian Picciano 2019-06-15 17:35:17 -06:00
parent 467bcbe52d
commit 8bd0664ba0
4 changed files with 32 additions and 62 deletions

28
mlog/cmp.go Normal file
View File

@ -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
}

View File

@ -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...)
}

View File

@ -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)

View File

@ -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()