github.com/wangyougui/gf/v2@v2.6.5/os/glog/glog_z_unit_logger_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/wangyougui/gf. 6 7 package glog_test 8 9 import ( 10 "bytes" 11 "context" 12 "testing" 13 14 "github.com/wangyougui/gf/v2/container/garray" 15 "github.com/wangyougui/gf/v2/os/glog" 16 "github.com/wangyougui/gf/v2/test/gtest" 17 "github.com/wangyougui/gf/v2/text/gstr" 18 ) 19 20 var arrayForHandlerTest1 = garray.NewStrArray() 21 22 func customHandler1(ctx context.Context, input *glog.HandlerInput) { 23 arrayForHandlerTest1.Append(input.String(false)) 24 input.Next(ctx) 25 } 26 27 func TestLogger_SetHandlers1(t *testing.T) { 28 gtest.C(t, func(t *gtest.T) { 29 w := bytes.NewBuffer(nil) 30 l := glog.NewWithWriter(w) 31 l.SetHandlers(customHandler1) 32 l.SetCtxKeys("Trace-Id", "Span-Id", "Test") 33 ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") 34 ctx = context.WithValue(ctx, "Span-Id", "abcdefg") 35 36 l.Print(ctx, 1, 2, 3) 37 t.Assert(gstr.Count(w.String(), "1234567890"), 1) 38 t.Assert(gstr.Count(w.String(), "abcdefg"), 1) 39 t.Assert(gstr.Count(w.String(), "1 2 3"), 1) 40 41 t.Assert(arrayForHandlerTest1.Len(), 1) 42 t.Assert(gstr.Count(arrayForHandlerTest1.At(0), "1234567890"), 1) 43 t.Assert(gstr.Count(arrayForHandlerTest1.At(0), "abcdefg"), 1) 44 t.Assert(gstr.Count(arrayForHandlerTest1.At(0), "1 2 3"), 1) 45 }) 46 } 47 48 var arrayForHandlerTest2 = garray.NewStrArray() 49 50 func customHandler2(ctx context.Context, input *glog.HandlerInput) { 51 arrayForHandlerTest2.Append(input.String(false)) 52 } 53 54 func TestLogger_SetHandlers2(t *testing.T) { 55 gtest.C(t, func(t *gtest.T) { 56 w := bytes.NewBuffer(nil) 57 l := glog.NewWithWriter(w) 58 l.SetHandlers(customHandler2) 59 l.SetCtxKeys("Trace-Id", "Span-Id", "Test") 60 ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") 61 ctx = context.WithValue(ctx, "Span-Id", "abcdefg") 62 63 l.Print(ctx, 1, 2, 3) 64 t.Assert(gstr.Count(w.String(), "1234567890"), 0) 65 t.Assert(gstr.Count(w.String(), "abcdefg"), 0) 66 t.Assert(gstr.Count(w.String(), "1 2 3"), 0) 67 68 t.Assert(arrayForHandlerTest2.Len(), 1) 69 t.Assert(gstr.Count(arrayForHandlerTest2.At(0), "1234567890"), 1) 70 t.Assert(gstr.Count(arrayForHandlerTest2.At(0), "abcdefg"), 1) 71 t.Assert(gstr.Count(arrayForHandlerTest2.At(0), "1 2 3"), 1) 72 }) 73 } 74 75 func TestLogger_SetHandlers_HandlerJson(t *testing.T) { 76 gtest.C(t, func(t *gtest.T) { 77 w := bytes.NewBuffer(nil) 78 l := glog.NewWithWriter(w) 79 l.SetHandlers(glog.HandlerJson) 80 l.SetCtxKeys("Trace-Id", "Span-Id", "Test") 81 ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") 82 ctx = context.WithValue(ctx, "Span-Id", "abcdefg") 83 84 l.Debug(ctx, 1, 2, 3) 85 t.Assert(gstr.Count(w.String(), "1234567890"), 1) 86 t.Assert(gstr.Count(w.String(), "abcdefg"), 1) 87 t.Assert(gstr.Count(w.String(), `"1 2 3"`), 1) 88 t.Assert(gstr.Count(w.String(), `"DEBU"`), 1) 89 }) 90 } 91 92 func TestLogger_SetHandlers_HandlerStructure(t *testing.T) { 93 gtest.C(t, func(t *gtest.T) { 94 w := bytes.NewBuffer(nil) 95 l := glog.NewWithWriter(w) 96 l.SetHandlers(glog.HandlerStructure) 97 l.SetCtxKeys("Trace-Id", "Span-Id", "Test") 98 ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") 99 ctx = context.WithValue(ctx, "Span-Id", "abcdefg") 100 101 l.Debug(ctx, "debug", "uid", 1000) 102 l.Info(ctx, "info", "' '", `"\n`) 103 104 t.Assert(gstr.Count(w.String(), "uid=1000"), 1) 105 t.Assert(gstr.Count(w.String(), "Content=debug"), 1) 106 t.Assert(gstr.Count(w.String(), `"' '"="\"\\n"`), 1) 107 t.Assert(gstr.Count(w.String(), `CtxStr="1234567890, abcdefg"`), 2) 108 }) 109 } 110 111 func Test_SetDefaultHandler(t *testing.T) { 112 gtest.C(t, func(t *gtest.T) { 113 oldHandler := glog.GetDefaultHandler() 114 glog.SetDefaultHandler(func(ctx context.Context, in *glog.HandlerInput) { 115 glog.HandlerJson(ctx, in) 116 }) 117 defer glog.SetDefaultHandler(oldHandler) 118 119 w := bytes.NewBuffer(nil) 120 l := glog.NewWithWriter(w) 121 l.SetCtxKeys("Trace-Id", "Span-Id", "Test") 122 ctx := context.WithValue(context.Background(), "Trace-Id", "1234567890") 123 ctx = context.WithValue(ctx, "Span-Id", "abcdefg") 124 125 l.Debug(ctx, 1, 2, 3) 126 t.Assert(gstr.Count(w.String(), "1234567890"), 1) 127 t.Assert(gstr.Count(w.String(), "abcdefg"), 1) 128 t.Assert(gstr.Count(w.String(), `"1 2 3"`), 1) 129 t.Assert(gstr.Count(w.String(), `"DEBU"`), 1) 130 }) 131 }