mlog: clean up component code and add tests for it
This commit is contained in:
parent
752e266685
commit
1a6b8e3166
15
mlog/cmp.go
15
mlog/cmp.go
@ -7,6 +7,11 @@ import (
|
||||
|
||||
type cmpKey int
|
||||
|
||||
const (
|
||||
cmpKeyLogger cmpKey = iota
|
||||
cmpKeyCachedLogger
|
||||
)
|
||||
|
||||
// SetLogger sets the given logger onto the Component. The logger can later be
|
||||
// retrieved from the Component, or any of its children, using From.
|
||||
//
|
||||
@ -15,14 +20,14 @@ type cmpKey int
|
||||
// should still be called. This is due to some caching that From does for
|
||||
// performance.
|
||||
func SetLogger(cmp *mcmp.Component, l *Logger) {
|
||||
cmp.SetValue(cmpKey(0), l)
|
||||
cmp.SetValue(cmpKeyLogger, l)
|
||||
|
||||
// If the base Logger on this Component gets changed, then the cached Logger
|
||||
// from From on this Component, and all of its Children, ought to be reset,
|
||||
// so that any changes can be reflected in their loggers.
|
||||
var resetFromLogger func(*mcmp.Component)
|
||||
resetFromLogger = func(cmp *mcmp.Component) {
|
||||
cmp.SetValue(cmpKey(1), nil)
|
||||
cmp.SetValue(cmpKeyCachedLogger, nil)
|
||||
for _, childCmp := range cmp.Children() {
|
||||
resetFromLogger(childCmp)
|
||||
}
|
||||
@ -38,7 +43,7 @@ var DefaultLogger = NewLogger()
|
||||
// ancestors, using SetLogger. If no Logger was ever set then DefaultLogger is
|
||||
// returned.
|
||||
func GetLogger(cmp *mcmp.Component) *Logger {
|
||||
if l, ok := cmp.InheritedValue(cmpKey(0)); ok {
|
||||
if l, ok := mcmp.InheritedValue(cmp, cmpKeyLogger); ok {
|
||||
return l.(*Logger)
|
||||
}
|
||||
return DefaultLogger
|
||||
@ -48,7 +53,7 @@ func GetLogger(cmp *mcmp.Component) *Logger {
|
||||
// some annotations related to the Component itself to all Messages being
|
||||
// logged.
|
||||
func From(cmp *mcmp.Component) *Logger {
|
||||
if l, _ := cmp.Value(cmpKey(1)).(*Logger); l != nil {
|
||||
if l, _ := cmp.Value(cmpKeyCachedLogger).(*Logger); l != nil {
|
||||
return l
|
||||
}
|
||||
|
||||
@ -61,7 +66,7 @@ func From(cmp *mcmp.Component) *Logger {
|
||||
msg.Contexts = append(msg.Contexts[:0], ctx)
|
||||
return oldHandler(msg)
|
||||
})
|
||||
cmp.SetValue(cmpKey(1), l)
|
||||
cmp.SetValue(cmpKeyCachedLogger, l)
|
||||
|
||||
return l
|
||||
}
|
||||
|
82
mlog/cmp_test.go
Normal file
82
mlog/cmp_test.go
Normal file
@ -0,0 +1,82 @@
|
||||
package mlog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
. "testing"
|
||||
|
||||
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
||||
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
||||
)
|
||||
|
||||
func TestGetSetLogger(t *T) {
|
||||
cmp := new(mcmp.Component)
|
||||
cmpChild := cmp.Child("child")
|
||||
ctx := mctx.Annotated("foo", "bar")
|
||||
|
||||
var msgs []string
|
||||
l := NewLogger()
|
||||
l.SetHandler(func(msg Message) error {
|
||||
msgStr := fmt.Sprintf("%s %q", msg.Level, msg.Description)
|
||||
for _, ctx := range msg.Contexts {
|
||||
for _, kv := range mctx.Annotations(ctx).StringSlice(true) {
|
||||
msgStr += fmt.Sprintf(" %s=%s", kv[0], kv[1])
|
||||
}
|
||||
}
|
||||
msgs = append(msgs, msgStr)
|
||||
return nil
|
||||
})
|
||||
SetLogger(cmp, l)
|
||||
|
||||
msgs = msgs[:0]
|
||||
GetLogger(cmp).Info("get-cmp", ctx)
|
||||
GetLogger(cmpChild).Info("get-cmpChild", ctx)
|
||||
From(cmp).Info("from-cmp", ctx)
|
||||
From(cmpChild).Info("from-cmpChild", ctx)
|
||||
massert.Require(t,
|
||||
massert.Equal(`INFO "get-cmp" foo=bar`, msgs[0]),
|
||||
massert.Equal(`INFO "get-cmpChild" foo=bar`, msgs[1]),
|
||||
massert.Equal(`INFO "from-cmp" componentPath=/ foo=bar`, msgs[2]),
|
||||
massert.Equal(`INFO "from-cmpChild" componentPath=/child foo=bar`, msgs[3]),
|
||||
)
|
||||
|
||||
l2 := l.Clone()
|
||||
l2.SetHandler(func(msg Message) error {
|
||||
msg.Description += " (2)"
|
||||
return l.Handler()(msg)
|
||||
})
|
||||
SetLogger(cmp, l2)
|
||||
|
||||
msgs = msgs[:0]
|
||||
GetLogger(cmp).Info("get-cmp", ctx)
|
||||
GetLogger(cmpChild).Info("get-cmpChild", ctx)
|
||||
From(cmp).Info("from-cmp", ctx)
|
||||
From(cmpChild).Info("from-cmpChild", ctx)
|
||||
massert.Require(t,
|
||||
massert.Equal(`INFO "get-cmp (2)" foo=bar`, msgs[0]),
|
||||
massert.Equal(`INFO "get-cmpChild (2)" foo=bar`, msgs[1]),
|
||||
massert.Equal(`INFO "from-cmp (2)" componentPath=/ foo=bar`, msgs[2]),
|
||||
massert.Equal(`INFO "from-cmpChild (2)" componentPath=/child foo=bar`, msgs[3]),
|
||||
)
|
||||
|
||||
// If a Logger is set on the child, that shouldn't affect the parent
|
||||
l3 := l.Clone()
|
||||
l3.SetHandler(func(msg Message) error {
|
||||
msg.Description += " (3)"
|
||||
return l.Handler()(msg)
|
||||
})
|
||||
SetLogger(cmpChild, l3)
|
||||
|
||||
msgs = msgs[:0]
|
||||
GetLogger(cmp).Info("get-cmp", ctx)
|
||||
GetLogger(cmpChild).Info("get-cmpChild", ctx)
|
||||
From(cmp).Info("from-cmp", ctx)
|
||||
From(cmpChild).Info("from-cmpChild", ctx)
|
||||
massert.Require(t,
|
||||
massert.Equal(`INFO "get-cmp (2)" foo=bar`, msgs[0]),
|
||||
massert.Equal(`INFO "get-cmpChild (3)" foo=bar`, msgs[1]),
|
||||
massert.Equal(`INFO "from-cmp (2)" componentPath=/ foo=bar`, msgs[2]),
|
||||
massert.Equal(`INFO "from-cmpChild (3)" componentPath=/child foo=bar`, msgs[3]),
|
||||
)
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user