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"
|
2023-12-26 15:08:03 +00:00
|
|
|
"testing"
|
2018-01-16 13:59:52 +00:00
|
|
|
. "testing"
|
|
|
|
"time"
|
|
|
|
|
2023-12-26 15:58:08 +00:00
|
|
|
"code.betamike.com/mediocregopher/mediocre-go-lib/internal/massert"
|
|
|
|
"code.betamike.com/mediocregopher/mediocre-go-lib/mctx"
|
2018-01-16 13:59:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTruncate(t *T) {
|
2023-12-26 15:08:03 +00:00
|
|
|
massert.Equal(t, "abc", Truncate("abc", 4))
|
|
|
|
massert.Equal(t, "abc", Truncate("abc", 3))
|
|
|
|
massert.Equal(t, "ab...", Truncate("abc", 2))
|
2018-01-16 13:59:52 +00:00
|
|
|
}
|
|
|
|
|
2023-12-26 15:08:03 +00:00
|
|
|
func TestJSONLogger(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{
|
2023-12-26 15:08:03 +00:00
|
|
|
MessageHandler: NewJSONMessageHandler(buf),
|
2021-02-07 23:04:41 +00:00
|
|
|
Now: func() time.Time { return now },
|
|
|
|
})
|
2018-01-16 13:59:52 +00:00
|
|
|
|
2023-12-26 15:08:03 +00:00
|
|
|
assertOut := func(t *testing.T, expected string) {
|
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')
|
2023-12-26 15:08:03 +00:00
|
|
|
massert.Equal(t, nil, err)
|
|
|
|
massert.Equal(t, expected, strings.TrimSpace(out))
|
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"))
|
2023-12-26 15:08:03 +00:00
|
|
|
assertOut(t, `{"td":"<TD>","ts":<TS>,"level":"INFO","descr":"bar","level_int":30}`)
|
|
|
|
assertOut(t, `{"td":"<TD>","ts":<TS>,"level":"WARN","descr":"baz","level_int":20,"annotations":{"errMsg":"ERR"}}`)
|
|
|
|
assertOut(t, `{"td":"<TD>","ts":<TS>,"level":"ERROR","descr":"buz","level_int":10,"annotations":{"errMsg":"ERR"}}`)
|
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")
|
2023-12-26 15:08:03 +00:00
|
|
|
assertOut(t, `{"td":"<TD>","ts":<TS>,"level":"INFO","descr":"bar","level_int":30,"annotations":{"foo":"bar"}}`)
|
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")
|
2023-12-26 15:08:03 +00:00
|
|
|
assertOut(t, `{"td":"<TD>","ts":<TS>,"level":"INFO","ns":["ns"],"descr":"bar","level_int":30,"annotations":{"foo":"bar"}}`)
|
2018-07-18 23:01:28 +00:00
|
|
|
}
|