2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-05-08 17:12:37 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package glog_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2020-05-08 17:12:37 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Ctx(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
w := bytes.NewBuffer(nil)
|
|
|
|
l := glog.NewWithWriter(w)
|
|
|
|
l.SetCtxKeys("Trace-Id", "Span-Id", "Test")
|
|
|
|
ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890")
|
|
|
|
ctx = context.WithValue(ctx, "Span-Id", "abcdefg")
|
|
|
|
|
2021-09-27 21:27:24 +08:00
|
|
|
l.Print(ctx, 1, 2, 3)
|
2020-05-08 17:12:37 +08:00
|
|
|
t.Assert(gstr.Count(w.String(), "1234567890"), 1)
|
|
|
|
t.Assert(gstr.Count(w.String(), "abcdefg"), 1)
|
|
|
|
t.Assert(gstr.Count(w.String(), "1 2 3"), 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Ctx_Config(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
w := bytes.NewBuffer(nil)
|
|
|
|
l := glog.NewWithWriter(w)
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"CtxKeys": g.SliceStr{"Trace-Id", "Span-Id", "Test"},
|
|
|
|
}
|
|
|
|
err := l.SetConfigWithMap(m)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890")
|
|
|
|
ctx = context.WithValue(ctx, "Span-Id", "abcdefg")
|
|
|
|
|
2021-09-27 21:27:24 +08:00
|
|
|
l.Print(ctx, 1, 2, 3)
|
2020-05-08 17:12:37 +08:00
|
|
|
t.Assert(gstr.Count(w.String(), "1234567890"), 1)
|
|
|
|
t.Assert(gstr.Count(w.String(), "abcdefg"), 1)
|
|
|
|
t.Assert(gstr.Count(w.String(), "1 2 3"), 1)
|
|
|
|
})
|
|
|
|
}
|
2021-08-20 15:44:08 +08:00
|
|
|
|
|
|
|
func Test_Ctx_CtxKey(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
w := bytes.NewBuffer(nil)
|
|
|
|
l := glog.NewWithWriter(w)
|
2021-09-27 21:27:24 +08:00
|
|
|
l.Print(gctx.WithValue(context.TODO(), "abcdefg"), 1, 2, 3)
|
2021-08-20 15:44:08 +08:00
|
|
|
t.Assert(gstr.Count(w.String(), "abcdefg"), 1)
|
|
|
|
t.Assert(gstr.Count(w.String(), "1 2 3"), 1)
|
|
|
|
})
|
|
|
|
}
|