github.com/gogf/gf/v2@v2.7.4/os/glog/glog_z_unit_logger_chaining_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 "fmt" 12 "testing" 13 "time" 14 15 "github.com/gogf/gf/v2/os/gfile" 16 "github.com/gogf/gf/v2/os/gtime" 17 "github.com/gogf/gf/v2/test/gtest" 18 "github.com/gogf/gf/v2/text/gstr" 19 ) 20 21 func Test_To(t *testing.T) { 22 gtest.C(t, func(t *gtest.T) { 23 w := bytes.NewBuffer(nil) 24 To(w).Error(ctx, 1, 2, 3) 25 To(w).Errorf(ctx, "%d %d %d", 1, 2, 3) 26 t.Assert(gstr.Count(w.String(), defaultLevelPrefixes[LEVEL_ERRO]), 2) 27 t.Assert(gstr.Count(w.String(), "1 2 3"), 2) 28 }) 29 } 30 31 func Test_Path(t *testing.T) { 32 gtest.C(t, func(t *gtest.T) { 33 path := gfile.Temp(gtime.TimestampNanoStr()) 34 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 35 36 err := gfile.Mkdir(path) 37 t.AssertNil(err) 38 defer gfile.Remove(path) 39 40 Path(path).File(file).Stdout(false).Error(ctx, 1, 2, 3) 41 Path(path).File(file).Stdout(false).Errorf(ctx, "%d %d %d", 1, 2, 3) 42 content := gfile.GetContents(gfile.Join(path, file)) 43 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 2) 44 t.Assert(gstr.Count(content, "1 2 3"), 2) 45 }) 46 } 47 48 func Test_Cat(t *testing.T) { 49 gtest.C(t, func(t *gtest.T) { 50 cat := "category" 51 path := gfile.Temp(gtime.TimestampNanoStr()) 52 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 53 54 err := gfile.Mkdir(path) 55 t.AssertNil(err) 56 defer gfile.Remove(path) 57 58 Path(path).File(file).Cat(cat).Stdout(false).Error(ctx, 1, 2, 3) 59 Path(path).File(file).Cat(cat).Stdout(false).Errorf(ctx, "%d %d %d", 1, 2, 3) 60 content := gfile.GetContents(gfile.Join(path, cat, file)) 61 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 2) 62 t.Assert(gstr.Count(content, "1 2 3"), 2) 63 }) 64 } 65 66 func Test_Level(t *testing.T) { 67 gtest.C(t, func(t *gtest.T) { 68 path := gfile.Temp(gtime.TimestampNanoStr()) 69 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 70 71 err := gfile.Mkdir(path) 72 t.AssertNil(err) 73 defer gfile.Remove(path) 74 75 Path(path).File(file).Level(LEVEL_PROD).Stdout(false).Debug(ctx, 1, 2, 3) 76 Path(path).File(file).Level(LEVEL_PROD).Stdout(false).Debug(ctx, "%d %d %d", 1, 2, 3) 77 content := gfile.GetContents(gfile.Join(path, file)) 78 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 0) 79 t.Assert(gstr.Count(content, "1 2 3"), 0) 80 }) 81 } 82 83 func Test_Skip(t *testing.T) { 84 gtest.C(t, func(t *gtest.T) { 85 path := gfile.Temp(gtime.TimestampNanoStr()) 86 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 87 88 err := gfile.Mkdir(path) 89 t.AssertNil(err) 90 defer gfile.Remove(path) 91 92 Path(path).File(file).Skip(10).Stdout(false).Error(ctx, 1, 2, 3) 93 Path(path).File(file).Stdout(false).Errorf(ctx, "%d %d %d", 1, 2, 3) 94 content := gfile.GetContents(gfile.Join(path, file)) 95 fmt.Println(content) 96 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 2) 97 t.Assert(gstr.Count(content, "1 2 3"), 2) 98 //t.Assert(gstr.Count(content, "Stack"), 1) 99 }) 100 } 101 102 func Test_Stack(t *testing.T) { 103 gtest.C(t, func(t *gtest.T) { 104 path := gfile.Temp(gtime.TimestampNanoStr()) 105 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 106 107 err := gfile.Mkdir(path) 108 t.AssertNil(err) 109 defer gfile.Remove(path) 110 111 Path(path).File(file).Stack(false).Stdout(false).Error(ctx, 1, 2, 3) 112 Path(path).File(file).Stdout(false).Errorf(ctx, "%d %d %d", 1, 2, 3) 113 content := gfile.GetContents(gfile.Join(path, file)) 114 fmt.Println(content) 115 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 2) 116 t.Assert(gstr.Count(content, "1 2 3"), 2) 117 //t.Assert(gstr.Count(content, "Stack"), 1) 118 }) 119 } 120 121 func Test_StackWithFilter(t *testing.T) { 122 gtest.C(t, func(t *gtest.T) { 123 path := gfile.Temp(gtime.TimestampNanoStr()) 124 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 125 126 err := gfile.Mkdir(path) 127 t.AssertNil(err) 128 defer gfile.Remove(path) 129 130 Path(path).File(file).StackWithFilter("none").Stdout(false).Error(ctx, 1, 2, 3) 131 content := gfile.GetContents(gfile.Join(path, file)) 132 fmt.Println(ctx, content) 133 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 1) 134 t.Assert(gstr.Count(content, "1 2 3"), 1) 135 //t.Assert(gstr.Count(content, "Stack"), 1) 136 137 }) 138 gtest.C(t, func(t *gtest.T) { 139 path := gfile.Temp(gtime.TimestampNanoStr()) 140 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 141 142 err := gfile.Mkdir(path) 143 t.AssertNil(err) 144 defer gfile.Remove(path) 145 146 Path(path).File(file).StackWithFilter("/gf/").Stdout(false).Error(ctx, 1, 2, 3) 147 content := gfile.GetContents(gfile.Join(path, file)) 148 fmt.Println(ctx, content) 149 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 1) 150 t.Assert(gstr.Count(content, "1 2 3"), 1) 151 //t.Assert(gstr.Count(content, "Stack"), 0) 152 }) 153 } 154 155 func Test_Header(t *testing.T) { 156 gtest.C(t, func(t *gtest.T) { 157 path := gfile.Temp(gtime.TimestampNanoStr()) 158 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 159 160 err := gfile.Mkdir(path) 161 t.AssertNil(err) 162 defer gfile.Remove(path) 163 164 Path(path).File(file).Header(true).Stdout(false).Error(ctx, 1, 2, 3) 165 content := gfile.GetContents(gfile.Join(path, file)) 166 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 1) 167 t.Assert(gstr.Count(content, "1 2 3"), 1) 168 }) 169 gtest.C(t, func(t *gtest.T) { 170 path := gfile.Temp(gtime.TimestampNanoStr()) 171 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 172 173 err := gfile.Mkdir(path) 174 t.AssertNil(err) 175 defer gfile.Remove(path) 176 177 Path(path).File(file).Header(false).Stdout(false).Error(ctx, 1, 2, 3) 178 content := gfile.GetContents(gfile.Join(path, file)) 179 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_ERRO]), 0) 180 t.Assert(gstr.Count(content, "1 2 3"), 1) 181 }) 182 } 183 184 func Test_Line(t *testing.T) { 185 gtest.C(t, func(t *gtest.T) { 186 path := gfile.Temp(gtime.TimestampNanoStr()) 187 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 188 189 err := gfile.Mkdir(path) 190 t.AssertNil(err) 191 defer gfile.Remove(path) 192 193 Path(path).File(file).Line(true).Stdout(false).Debug(ctx, 1, 2, 3) 194 content := gfile.GetContents(gfile.Join(path, file)) 195 fmt.Println(content) 196 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 1) 197 t.Assert(gstr.Count(content, "1 2 3"), 1) 198 //t.Assert(gstr.Count(content, ".go"), 1) 199 //t.Assert(gstr.Contains(content, gfile.Separator), true) 200 }) 201 gtest.C(t, func(t *gtest.T) { 202 path := gfile.Temp(gtime.TimestampNanoStr()) 203 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 204 205 err := gfile.Mkdir(path) 206 t.AssertNil(err) 207 defer gfile.Remove(path) 208 209 Path(path).File(file).Line(false).Stdout(false).Debug(ctx, 1, 2, 3) 210 content := gfile.GetContents(gfile.Join(path, file)) 211 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 1) 212 t.Assert(gstr.Count(content, "1 2 3"), 1) 213 //t.Assert(gstr.Count(content, ".go"), 1) 214 //t.Assert(gstr.Contains(content, gfile.Separator), false) 215 }) 216 } 217 218 func Test_Async(t *testing.T) { 219 gtest.C(t, func(t *gtest.T) { 220 path := gfile.Temp(gtime.TimestampNanoStr()) 221 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 222 223 err := gfile.Mkdir(path) 224 t.AssertNil(err) 225 defer gfile.Remove(path) 226 227 Path(path).File(file).Async().Stdout(false).Debug(ctx, 1, 2, 3) 228 time.Sleep(1000 * time.Millisecond) 229 230 content := gfile.GetContents(gfile.Join(path, file)) 231 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 1) 232 t.Assert(gstr.Count(content, "1 2 3"), 1) 233 }) 234 235 gtest.C(t, func(t *gtest.T) { 236 path := gfile.Temp(gtime.TimestampNanoStr()) 237 file := fmt.Sprintf(`%d.log`, gtime.TimestampNano()) 238 239 err := gfile.Mkdir(path) 240 t.AssertNil(err) 241 defer gfile.Remove(path) 242 243 Path(path).File(file).Async(false).Stdout(false).Debug(ctx, 1, 2, 3) 244 content := gfile.GetContents(gfile.Join(path, file)) 245 t.Assert(gstr.Count(content, defaultLevelPrefixes[LEVEL_DEBU]), 1) 246 t.Assert(gstr.Count(content, "1 2 3"), 1) 247 }) 248 }