2018-01-16 13:59:52 +00:00
|
|
|
package mlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2018-01-16 13:59:52 +00:00
|
|
|
"regexp"
|
2018-05-28 05:47:23 +00:00
|
|
|
"strings"
|
2018-01-16 13:59:52 +00:00
|
|
|
. "testing"
|
|
|
|
"time"
|
|
|
|
|
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) {
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Equal("abc", Truncate("abc", 4)),
|
|
|
|
massert.Equal("abc", Truncate("abc", 3)),
|
|
|
|
massert.Equal("ab...", Truncate("abc", 2)),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKV(t *T) {
|
|
|
|
var kv KV
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
2019-01-15 03:47:16 +00:00
|
|
|
massert.Nil(kv.KV()),
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Len(kv.KV(), 0),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2019-01-15 03:32:26 +00:00
|
|
|
// test that the Set method returns a copy
|
2018-01-16 13:59:52 +00:00
|
|
|
kv = KV{"foo": "a"}
|
2019-01-15 03:47:16 +00:00
|
|
|
kv2 := kv.Set("bar", "wat")
|
2018-07-19 18:19:26 +00:00
|
|
|
kv["bur"] = "ok"
|
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Equal(KV{"foo": "a", "bur": "ok"}, kv),
|
2019-01-15 03:47:16 +00:00
|
|
|
massert.Equal(KV{"foo": "a", "bar": "wat"}, kv2),
|
2018-07-19 18:19:26 +00:00
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 18:19:26 +00:00
|
|
|
func TestLogger(t *T) {
|
2019-02-05 20:18:17 +00:00
|
|
|
ctx := context.Background()
|
2018-01-16 13:59:52 +00:00
|
|
|
buf := new(bytes.Buffer)
|
2018-11-30 21:27:18 +00:00
|
|
|
h := func(msg Message) error {
|
|
|
|
return DefaultFormat(buf, msg)
|
|
|
|
}
|
|
|
|
|
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):
|
2018-10-28 19:09:42 +00:00
|
|
|
return massert.Errf("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),
|
|
|
|
massert.Equal(expected, out),
|
|
|
|
)
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default max level should be INFO
|
2019-02-05 20:18:17 +00:00
|
|
|
l.Debug(ctx, "foo")
|
|
|
|
l.Info(ctx, "bar")
|
|
|
|
l.Warn(ctx, "baz")
|
|
|
|
l.Error(ctx, "buz")
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
assertOut("~ INFO -- bar\n"),
|
|
|
|
assertOut("~ WARN -- baz\n"),
|
|
|
|
assertOut("~ ERROR -- buz\n"),
|
|
|
|
))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
l.SetMaxLevel(WarnLevel)
|
2019-02-05 20:18:17 +00:00
|
|
|
l.Debug(ctx, "foo")
|
|
|
|
l.Info(ctx, "bar")
|
|
|
|
l.Warn(ctx, "baz")
|
|
|
|
l.Error(ctx, "buz", KV{"a": "b"})
|
2018-10-28 19:09:42 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
assertOut("~ WARN -- baz\n"),
|
|
|
|
assertOut("~ ERROR -- buz -- a=\"b\"\n"),
|
|
|
|
))
|
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-05 20:18:17 +00:00
|
|
|
l2.Info(ctx, "bar")
|
|
|
|
l2.Warn(ctx, "baz")
|
|
|
|
l.Error(ctx, "buz")
|
2018-10-28 19:09:42 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
assertOut("~ INFO -- BAR\n"),
|
|
|
|
assertOut("~ WARN -- BAZ\n"),
|
|
|
|
assertOut("~ ERROR -- buz\n"),
|
|
|
|
))
|
2019-01-13 02:01:00 +00:00
|
|
|
|
2019-01-30 21:06:24 +00:00
|
|
|
l3 := l2.Clone()
|
|
|
|
l3.SetKV(KV{"a": 1})
|
2019-02-05 20:18:17 +00:00
|
|
|
l3.Info(ctx, "foo", KV{"b": 2})
|
|
|
|
l3.Info(ctx, "bar", KV{"a": 2, "b": 3})
|
2019-01-13 02:01:00 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
assertOut("~ INFO -- FOO -- a=\"1\" b=\"2\"\n"),
|
|
|
|
assertOut("~ INFO -- BAR -- a=\"2\" b=\"3\"\n"),
|
|
|
|
))
|
|
|
|
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-30 21:27:18 +00:00
|
|
|
func TestDefaultFormat(t *T) {
|
2018-07-19 18:19:26 +00:00
|
|
|
assertFormat := func(postfix string, msg Message) massert.Assertion {
|
2018-01-16 13:59:52 +00:00
|
|
|
expectedRegex := regexp.MustCompile(`^~ ` + postfix + `\n$`)
|
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, 128))
|
2018-11-30 21:27:18 +00:00
|
|
|
writeErr := DefaultFormat(buf, msg)
|
2018-01-16 13:59:52 +00:00
|
|
|
line, err := buf.ReadString('\n')
|
2018-07-19 18:19:26 +00:00
|
|
|
return massert.Comment(
|
|
|
|
massert.All(
|
|
|
|
massert.Nil(writeErr),
|
|
|
|
massert.Nil(err),
|
|
|
|
massert.Equal(true, expectedRegex.MatchString(line)),
|
|
|
|
),
|
|
|
|
"line:%q", line,
|
|
|
|
)
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
msg := Message{
|
|
|
|
Context: context.Background(),
|
|
|
|
Level: InfoLevel,
|
|
|
|
Description: "this is a test",
|
|
|
|
}
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, assertFormat("INFO -- this is a test", msg))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2019-01-13 02:01:00 +00:00
|
|
|
msg.KVer = KV{}
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, assertFormat("INFO -- this is a test", msg))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2019-01-13 02:01:00 +00:00
|
|
|
msg.KVer = KV{"foo": "a"}
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, assertFormat("INFO -- this is a test -- foo=\"a\"", msg))
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2019-01-13 02:01:00 +00:00
|
|
|
msg.KVer = KV{"foo": "a", "bar": "b"}
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t,
|
|
|
|
assertFormat("INFO -- this is a test -- bar=\"b\" foo=\"a\"", msg))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
2018-05-28 05:47:23 +00:00
|
|
|
|
|
|
|
func TestMerge(t *T) {
|
2018-07-19 18:19:26 +00:00
|
|
|
assertMerge := func(exp KV, kvs ...KVer) massert.Assertion {
|
2019-01-15 03:32:26 +00:00
|
|
|
return massert.Equal(exp.KV(), Merge(kvs...).KV())
|
2018-05-28 05:47:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 18:19:26 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
assertMerge(KV{}),
|
|
|
|
assertMerge(KV{}, nil),
|
|
|
|
assertMerge(KV{}, nil, nil),
|
|
|
|
|
|
|
|
assertMerge(KV{"a": "a"}, KV{"a": "a"}),
|
|
|
|
assertMerge(KV{"a": "a"}, nil, KV{"a": "a"}),
|
|
|
|
assertMerge(KV{"a": "a"}, KV{"a": "a"}, nil),
|
|
|
|
|
|
|
|
assertMerge(
|
|
|
|
KV{"a": "a", "b": "b"},
|
|
|
|
KV{"a": "a"}, KV{"b": "b"},
|
|
|
|
),
|
|
|
|
assertMerge(
|
|
|
|
KV{"a": "a", "b": "b"},
|
|
|
|
KV{"a": "a"}, KV{"b": "b"},
|
|
|
|
),
|
|
|
|
assertMerge(
|
|
|
|
KV{"a": "b"},
|
|
|
|
KV{"a": "a"}, KV{"a": "b"},
|
|
|
|
),
|
|
|
|
))
|
2018-07-19 18:43:17 +00:00
|
|
|
|
|
|
|
// Merge should _not_ call KV() on the inner KVers until the outer one is
|
|
|
|
// called.
|
|
|
|
{
|
|
|
|
kv := KV{"a": "a"}
|
|
|
|
mergedKV := Merge(kv)
|
|
|
|
kv["a"] = "b"
|
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Equal(KV{"a": "b"}, kv),
|
2019-01-15 03:32:26 +00:00
|
|
|
massert.Equal(map[string]interface{}{"a": "b"}, kv.KV()),
|
|
|
|
massert.Equal(map[string]interface{}{"a": "b"}, mergedKV.KV()),
|
2018-07-19 18:43:17 +00:00
|
|
|
))
|
|
|
|
}
|
2018-05-28 05:47:23 +00:00
|
|
|
}
|
2018-07-18 23:01:28 +00:00
|
|
|
|
|
|
|
func TestPrefix(t *T) {
|
|
|
|
kv := KV{"foo": "bar"}
|
|
|
|
prefixKV := Prefix(kv, "aa")
|
|
|
|
|
|
|
|
massert.Fatal(t, massert.All(
|
2019-01-15 03:32:26 +00:00
|
|
|
massert.Equal(map[string]interface{}{"foo": "bar"}, kv.KV()),
|
|
|
|
massert.Equal(map[string]interface{}{"aafoo": "bar"}, prefixKV.KV()),
|
|
|
|
massert.Equal(map[string]interface{}{"foo": "bar"}, kv.KV()),
|
2018-07-18 23:01:28 +00:00
|
|
|
))
|
|
|
|
}
|