github.com/gogf/gf@v1.16.9/os/glog/glog_z_unit_ctx_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package glog_test 8 9 import ( 10 "bytes" 11 "context" 12 "github.com/gogf/gf/frame/g" 13 "github.com/gogf/gf/os/gctx" 14 "github.com/gogf/gf/os/glog" 15 "github.com/gogf/gf/test/gtest" 16 "github.com/gogf/gf/text/gstr" 17 "testing" 18 ) 19 20 func Test_Ctx(t *testing.T) { 21 gtest.C(t, func(t *gtest.T) { 22 w := bytes.NewBuffer(nil) 23 l := glog.NewWithWriter(w) 24 l.SetCtxKeys("Trace-Id", "Span-Id", "Test") 25 ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") 26 ctx = context.WithValue(ctx, "Span-Id", "abcdefg") 27 28 l.Ctx(ctx).Print(1, 2, 3) 29 t.Assert(gstr.Count(w.String(), "1234567890"), 1) 30 t.Assert(gstr.Count(w.String(), "abcdefg"), 1) 31 t.Assert(gstr.Count(w.String(), "1 2 3"), 1) 32 }) 33 } 34 35 func Test_Ctx_Config(t *testing.T) { 36 gtest.C(t, func(t *gtest.T) { 37 w := bytes.NewBuffer(nil) 38 l := glog.NewWithWriter(w) 39 m := map[string]interface{}{ 40 "CtxKeys": g.SliceStr{"Trace-Id", "Span-Id", "Test"}, 41 } 42 err := l.SetConfigWithMap(m) 43 t.Assert(err, nil) 44 ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") 45 ctx = context.WithValue(ctx, "Span-Id", "abcdefg") 46 47 l.Ctx(ctx).Print(1, 2, 3) 48 t.Assert(gstr.Count(w.String(), "1234567890"), 1) 49 t.Assert(gstr.Count(w.String(), "abcdefg"), 1) 50 t.Assert(gstr.Count(w.String(), "1 2 3"), 1) 51 }) 52 } 53 54 func Test_Ctx_CtxKey(t *testing.T) { 55 gtest.C(t, func(t *gtest.T) { 56 w := bytes.NewBuffer(nil) 57 l := glog.NewWithWriter(w) 58 l.Ctx(gctx.WithValue(context.TODO(), "abcdefg")).Print(1, 2, 3) 59 t.Assert(gstr.Count(w.String(), "abcdefg"), 1) 60 t.Assert(gstr.Count(w.String(), "1 2 3"), 1) 61 }) 62 }