github.com/gogf/gf@v1.16.9/os/glog/glog_z_unit_handler_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/container/garray" 13 "github.com/gogf/gf/os/glog" 14 "github.com/gogf/gf/test/gtest" 15 "github.com/gogf/gf/text/gstr" 16 "testing" 17 ) 18 19 var arrayForHandlerTest1 = garray.NewStrArray() 20 21 func customHandler1(ctx context.Context, input *glog.HandlerInput) { 22 arrayForHandlerTest1.Append(input.String(false)) 23 input.Next() 24 } 25 26 func TestLogger_SetHandlers1(t *testing.T) { 27 gtest.C(t, func(t *gtest.T) { 28 w := bytes.NewBuffer(nil) 29 l := glog.NewWithWriter(w) 30 l.SetHandlers(customHandler1) 31 l.SetCtxKeys("Trace-Id", "Span-Id", "Test") 32 ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") 33 ctx = context.WithValue(ctx, "Span-Id", "abcdefg") 34 35 l.Ctx(ctx).Print(1, 2, 3) 36 t.Assert(gstr.Count(w.String(), "1234567890"), 1) 37 t.Assert(gstr.Count(w.String(), "abcdefg"), 1) 38 t.Assert(gstr.Count(w.String(), "1 2 3"), 1) 39 40 t.Assert(arrayForHandlerTest1.Len(), 1) 41 t.Assert(gstr.Count(arrayForHandlerTest1.At(0), "1234567890"), 1) 42 t.Assert(gstr.Count(arrayForHandlerTest1.At(0), "abcdefg"), 1) 43 t.Assert(gstr.Count(arrayForHandlerTest1.At(0), "1 2 3"), 1) 44 }) 45 } 46 47 var arrayForHandlerTest2 = garray.NewStrArray() 48 49 func customHandler2(ctx context.Context, input *glog.HandlerInput) { 50 arrayForHandlerTest2.Append(input.String(false)) 51 } 52 53 func TestLogger_SetHandlers2(t *testing.T) { 54 gtest.C(t, func(t *gtest.T) { 55 w := bytes.NewBuffer(nil) 56 l := glog.NewWithWriter(w) 57 l.SetHandlers(customHandler2) 58 l.SetCtxKeys("Trace-Id", "Span-Id", "Test") 59 ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") 60 ctx = context.WithValue(ctx, "Span-Id", "abcdefg") 61 62 l.Ctx(ctx).Print(1, 2, 3) 63 t.Assert(gstr.Count(w.String(), "1234567890"), 0) 64 t.Assert(gstr.Count(w.String(), "abcdefg"), 0) 65 t.Assert(gstr.Count(w.String(), "1 2 3"), 0) 66 67 t.Assert(arrayForHandlerTest2.Len(), 1) 68 t.Assert(gstr.Count(arrayForHandlerTest2.At(0), "1234567890"), 1) 69 t.Assert(gstr.Count(arrayForHandlerTest2.At(0), "abcdefg"), 1) 70 t.Assert(gstr.Count(arrayForHandlerTest2.At(0), "1 2 3"), 1) 71 }) 72 }