github.com/gogf/gf/v2@v2.7.4/os/glog/glog_z_unit_internal_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 8 9 import ( 10 "bytes" 11 "context" 12 "testing" 13 14 "github.com/gogf/gf/v2/test/gtest" 15 "github.com/gogf/gf/v2/text/gstr" 16 ) 17 18 var ( 19 ctx = context.TODO() 20 ) 21 22 func Test_Print(t *testing.T) { 23 gtest.C(t, func(t *gtest.T) { 24 w := bytes.NewBuffer(nil) 25 l := NewWithWriter(w) 26 l.Print(ctx, 1, 2, 3) 27 l.Printf(ctx, "%d %d %d", 1, 2, 3) 28 t.Assert(gstr.Count(w.String(), "["), 0) 29 t.Assert(gstr.Count(w.String(), "1 2 3"), 2) 30 }) 31 } 32 33 func Test_Debug(t *testing.T) { 34 gtest.C(t, func(t *gtest.T) { 35 w := bytes.NewBuffer(nil) 36 l := NewWithWriter(w) 37 l.Debug(ctx, 1, 2, 3) 38 l.Debugf(ctx, "%d %d %d", 1, 2, 3) 39 t.Assert(gstr.Count(w.String(), defaultLevelPrefixes[LEVEL_DEBU]), 2) 40 t.Assert(gstr.Count(w.String(), "1 2 3"), 2) 41 }) 42 } 43 44 func Test_Info(t *testing.T) { 45 gtest.C(t, func(t *gtest.T) { 46 w := bytes.NewBuffer(nil) 47 l := NewWithWriter(w) 48 l.Info(ctx, 1, 2, 3) 49 l.Infof(ctx, "%d %d %d", 1, 2, 3) 50 t.Assert(gstr.Count(w.String(), defaultLevelPrefixes[LEVEL_INFO]), 2) 51 t.Assert(gstr.Count(w.String(), "1 2 3"), 2) 52 }) 53 } 54 55 func Test_Notice(t *testing.T) { 56 gtest.C(t, func(t *gtest.T) { 57 w := bytes.NewBuffer(nil) 58 l := NewWithWriter(w) 59 l.Notice(ctx, 1, 2, 3) 60 l.Noticef(ctx, "%d %d %d", 1, 2, 3) 61 t.Assert(gstr.Count(w.String(), defaultLevelPrefixes[LEVEL_NOTI]), 2) 62 t.Assert(gstr.Count(w.String(), "1 2 3"), 2) 63 }) 64 } 65 66 func Test_Warning(t *testing.T) { 67 gtest.C(t, func(t *gtest.T) { 68 w := bytes.NewBuffer(nil) 69 l := NewWithWriter(w) 70 l.Warning(ctx, 1, 2, 3) 71 l.Warningf(ctx, "%d %d %d", 1, 2, 3) 72 t.Assert(gstr.Count(w.String(), defaultLevelPrefixes[LEVEL_WARN]), 2) 73 t.Assert(gstr.Count(w.String(), "1 2 3"), 2) 74 }) 75 } 76 77 func Test_Error(t *testing.T) { 78 gtest.C(t, func(t *gtest.T) { 79 w := bytes.NewBuffer(nil) 80 l := NewWithWriter(w) 81 l.Error(ctx, 1, 2, 3) 82 l.Errorf(ctx, "%d %d %d", 1, 2, 3) 83 t.Assert(gstr.Count(w.String(), defaultLevelPrefixes[LEVEL_ERRO]), 2) 84 t.Assert(gstr.Count(w.String(), "1 2 3"), 2) 85 }) 86 } 87 88 func Test_LevelPrefix(t *testing.T) { 89 gtest.C(t, func(t *gtest.T) { 90 l := New() 91 t.Assert(l.GetLevelPrefix(LEVEL_DEBU), defaultLevelPrefixes[LEVEL_DEBU]) 92 t.Assert(l.GetLevelPrefix(LEVEL_INFO), defaultLevelPrefixes[LEVEL_INFO]) 93 t.Assert(l.GetLevelPrefix(LEVEL_NOTI), defaultLevelPrefixes[LEVEL_NOTI]) 94 t.Assert(l.GetLevelPrefix(LEVEL_WARN), defaultLevelPrefixes[LEVEL_WARN]) 95 t.Assert(l.GetLevelPrefix(LEVEL_ERRO), defaultLevelPrefixes[LEVEL_ERRO]) 96 t.Assert(l.GetLevelPrefix(LEVEL_CRIT), defaultLevelPrefixes[LEVEL_CRIT]) 97 l.SetLevelPrefix(LEVEL_DEBU, "debug") 98 t.Assert(l.GetLevelPrefix(LEVEL_DEBU), "debug") 99 l.SetLevelPrefixes(map[int]string{ 100 LEVEL_CRIT: "critical", 101 }) 102 t.Assert(l.GetLevelPrefix(LEVEL_DEBU), "debug") 103 t.Assert(l.GetLevelPrefix(LEVEL_INFO), defaultLevelPrefixes[LEVEL_INFO]) 104 t.Assert(l.GetLevelPrefix(LEVEL_NOTI), defaultLevelPrefixes[LEVEL_NOTI]) 105 t.Assert(l.GetLevelPrefix(LEVEL_WARN), defaultLevelPrefixes[LEVEL_WARN]) 106 t.Assert(l.GetLevelPrefix(LEVEL_ERRO), defaultLevelPrefixes[LEVEL_ERRO]) 107 t.Assert(l.GetLevelPrefix(LEVEL_CRIT), "critical") 108 }) 109 gtest.C(t, func(t *gtest.T) { 110 buffer := bytes.NewBuffer(nil) 111 l := New() 112 l.SetWriter(buffer) 113 l.Debug(ctx, "test1") 114 t.Assert(gstr.Contains(buffer.String(), defaultLevelPrefixes[LEVEL_DEBU]), true) 115 116 buffer.Reset() 117 118 l.SetLevelPrefix(LEVEL_DEBU, "debug") 119 l.Debug(ctx, "test2") 120 t.Assert(gstr.Contains(buffer.String(), defaultLevelPrefixes[LEVEL_DEBU]), false) 121 t.Assert(gstr.Contains(buffer.String(), "debug"), true) 122 123 buffer.Reset() 124 l.SetLevelPrefixes(map[int]string{ 125 LEVEL_ERRO: "error", 126 }) 127 l.Error(ctx, "test3") 128 t.Assert(gstr.Contains(buffer.String(), defaultLevelPrefixes[LEVEL_ERRO]), false) 129 t.Assert(gstr.Contains(buffer.String(), "error"), true) 130 }) 131 }