2018-01-16 13:59:52 +00:00
|
|
|
package mlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2021-04-09 23:30:59 +00:00
|
|
|
"errors"
|
2021-02-07 23:04:41 +00:00
|
|
|
"fmt"
|
2018-05-28 05:47:23 +00:00
|
|
|
"strings"
|
2018-01-16 13:59:52 +00:00
|
|
|
. "testing"
|
|
|
|
"time"
|
|
|
|
|
2021-04-09 23:30:59 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mctx"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/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)
|
2021-02-07 23:04:41 +00:00
|
|
|
now := time.Now().UTC()
|
|
|
|
td, ts := now.Format(msgTimeFormat), fmt.Sprint(now.UnixNano())
|
2018-11-30 21:27:18 +00:00
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
l := NewLogger(&LoggerOpts{
|
|
|
|
MessageHandler: NewMessageHandler(buf),
|
|
|
|
Now: func() time.Time { return now },
|
|
|
|
})
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2018-07-19 18:19:26 +00:00
|
|
|
assertOut := func(expected string) massert.Assertion {
|
2021-02-07 23:04:41 +00:00
|
|
|
expected = strings.ReplaceAll(expected, "<TD>", td)
|
|
|
|
expected = strings.ReplaceAll(expected, "<TS>", ts)
|
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
|
|
|
}
|
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-01-16 13:59:52 +00:00
|
|
|
// Default max level should be INFO
|
2021-02-07 23:04:41 +00:00
|
|
|
l.Debug(ctx, "foo")
|
|
|
|
l.Info(ctx, "bar")
|
2021-04-09 23:30:59 +00:00
|
|
|
l.Warn(ctx, "baz", errors.New("ERR"))
|
|
|
|
l.Error(ctx, "buz", errors.New("ERR"))
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2021-02-07 23:04:41 +00:00
|
|
|
assertOut(`{"td":"<TD>","ts":<TS>,"level":"INFO","descr":"bar","level_int":30}`),
|
2021-04-10 00:05:07 +00:00
|
|
|
assertOut(`{"td":"<TD>","ts":<TS>,"level":"WARN","descr":"baz","level_int":20,"annotations":{"errMsg":"ERR"}}`),
|
|
|
|
assertOut(`{"td":"<TD>","ts":<TS>,"level":"ERROR","descr":"buz","level_int":10,"annotations":{"errMsg":"ERR"}}`),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// annotate context
|
|
|
|
ctx = mctx.Annotate(ctx, "foo", "bar")
|
|
|
|
l.Info(ctx, "bar")
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2021-02-07 23:04:41 +00:00
|
|
|
assertOut(`{"td":"<TD>","ts":<TS>,"level":"INFO","descr":"bar","level_int":30,"annotations":{"foo":"bar"}}`),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2018-05-28 05:47:23 +00:00
|
|
|
|
2021-02-07 23:04:41 +00:00
|
|
|
// add namespace
|
|
|
|
l = l.WithNamespace("ns")
|
|
|
|
l.Info(ctx, "bar")
|
|
|
|
massert.Require(t,
|
|
|
|
assertOut(`{"td":"<TD>","ts":<TS>,"level":"INFO","ns":["ns"],"descr":"bar","level_int":30,"annotations":{"foo":"bar"}}`),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2018-07-18 23:01:28 +00:00
|
|
|
}
|