2018-01-16 13:59:52 +00:00
|
|
|
package mlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2018-05-28 05:47:23 +00:00
|
|
|
"strings"
|
2018-01-16 13:59:52 +00:00
|
|
|
. "testing"
|
|
|
|
"time"
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
2018-07-18 23:01:28 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
2018-01-16 13:59:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTruncate(t *T) {
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Equal("abc", Truncate("abc", 4)),
|
|
|
|
massert.Equal("abc", Truncate("abc", 3)),
|
|
|
|
massert.Equal("ab...", Truncate("abc", 2)),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 18:19:26 +00:00
|
|
|
func TestLogger(t *T) {
|
2018-01-16 13:59:52 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2019-02-09 19:08:30 +00:00
|
|
|
h := defaultHandler(buf)
|
2018-11-30 21:27:18 +00:00
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
l := NewLogger()
|
|
|
|
l.SetHandler(h)
|
2018-05-28 05:47:23 +00:00
|
|
|
l.testMsgWrittenCh = make(chan struct{}, 10)
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2018-07-19 18:19:26 +00:00
|
|
|
assertOut := func(expected string) massert.Assertion {
|
2018-01-16 13:59:52 +00:00
|
|
|
select {
|
|
|
|
case <-l.testMsgWrittenCh:
|
|
|
|
case <-time.After(1 * time.Second):
|
2019-03-10 23:23:37 +00:00
|
|
|
return massert.Errorf("waited too long for msg to write")
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
out, err := buf.ReadString('\n')
|
2018-07-19 18:19:26 +00:00
|
|
|
return massert.All(
|
|
|
|
massert.Nil(err),
|
2019-02-09 19:08:30 +00:00
|
|
|
massert.Equal(expected, strings.TrimSpace(out)),
|
2018-07-19 18:19:26 +00:00
|
|
|
)
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default max level should be INFO
|
2019-02-09 19:08:30 +00:00
|
|
|
l.Debug("foo")
|
|
|
|
l.Info("bar")
|
|
|
|
l.Warn("baz")
|
|
|
|
l.Error("buz")
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-02-09 19:08:30 +00:00
|
|
|
assertOut(`{"level":"INFO","descr":"bar"}`),
|
|
|
|
assertOut(`{"level":"WARN","descr":"baz"}`),
|
|
|
|
assertOut(`{"level":"ERROR","descr":"buz"}`),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
l.SetMaxLevel(WarnLevel)
|
2019-02-09 19:08:30 +00:00
|
|
|
l.Debug("foo")
|
|
|
|
l.Info("bar")
|
|
|
|
l.Warn("baz")
|
|
|
|
l.Error("buz", mctx.Annotate(ctx, "a", "b", "c", "d"))
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-02-09 19:08:30 +00:00
|
|
|
assertOut(`{"level":"WARN","descr":"baz"}`),
|
2019-06-15 23:35:17 +00:00
|
|
|
assertOut(`{"level":"ERROR","descr":"buz","annotations":{"a":"b","c":"d"}}`),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2018-05-28 05:47:23 +00:00
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
l2 := l.Clone()
|
|
|
|
l2.SetMaxLevel(InfoLevel)
|
|
|
|
l2.SetHandler(func(msg Message) error {
|
2019-02-05 20:18:17 +00:00
|
|
|
msg.Description = strings.ToUpper(msg.Description)
|
2018-11-30 21:27:18 +00:00
|
|
|
return h(msg)
|
2018-10-28 19:09:42 +00:00
|
|
|
})
|
2019-02-09 19:08:30 +00:00
|
|
|
l2.Info("bar")
|
|
|
|
l2.Warn("baz")
|
|
|
|
l.Error("buz")
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-02-09 19:08:30 +00:00
|
|
|
assertOut(`{"level":"INFO","descr":"BAR"}`),
|
|
|
|
assertOut(`{"level":"WARN","descr":"BAZ"}`),
|
|
|
|
assertOut(`{"level":"ERROR","descr":"buz"}`),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2018-07-18 23:01:28 +00:00
|
|
|
}
|