github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zlog/zlog_test.go (about) 1 package zlog 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/sohaha/zlsgo" 8 ) 9 10 func TestLogTrack(T *testing.T) { 11 Track("log with Track") 12 Stack("log with Stack") 13 } 14 15 func TestLogs(T *testing.T) { 16 t := zlsgo.NewTest(T) 17 text := "Text" 18 19 log.SetIgnoreLog("test") 20 SetLogLevel(LogDump) 21 Debug("test") 22 Debug("debug") 23 Debug("log with Debug") 24 Debugf("%s\n", "log with Debug") 25 Info("log with Info") 26 Infof("%s\n", "log with Info") 27 Success("log with Success") 28 Successf("%s\n", "log with Success") 29 Tips("log with Tips") 30 Tipsf("%s\n", "log with Tips") 31 Warn("log with Warn") 32 Warnf("%s\n", "log with Warn") 33 Error("log with Error") 34 Errorf("%s\n", "log with Error") 35 Println("log with Println") 36 Printf("%s\n", "log with Printf") 37 Dump("log with Dump", t, T, nil) 38 39 SetLogLevel(LogFatal) 40 level := GetLogLevel() 41 t.Equal(LogFatal, level) 42 ResetFlags(BitLevel | BitShortFile | BitTime) 43 flage := GetFlags() 44 t.Equal(BitDefault, flage) 45 DisableConsoleColor() 46 GetFlags() 47 ResetFlags(BitDate) 48 AddFlag(BitLevel) 49 SetPrefix(text) 50 ForceConsoleColor() 51 ColorBackgroundWrap(ColorBlack, ColorLightGreen, text) 52 SetFile("tmp/Log.log") 53 CleanLog(log) 54 log := New(text) 55 log.SetPrefix(text) 56 t.EqualExit(log.GetPrefix(), text) 57 log.GetLogLevel() 58 log.SetSaveFile("tmp/Log.log") 59 log.ColorBackgroundWrap(ColorBlack, ColorLightGreen, text) 60 log.OpTextWrap(OpBold, text) 61 log.Dump(struct { 62 S struct { 63 N *string 64 n string 65 } 66 M map[string]interface{} 67 N string 68 I int 69 U uint 70 F float32 71 B bool 72 }{N: "test\nyes", M: map[string]interface{}{"s": 1243}, S: struct { 73 N *string 74 n string 75 }{n: ""}}) 76 CleanLog(log) 77 e := os.RemoveAll("tmp/") 78 t.Log(e) 79 } 80 81 func TestLogFatal(T *testing.T) { 82 ResetFlags(0) 83 oldOsExit := osExit 84 defer func() { osExit = oldOsExit }() 85 myExit := func(code int) { 86 } 87 osExit = myExit 88 Fatal("TestLogFatal") 89 Fatalf("%s\n", "Fatal") 90 } 91 92 func TestLogPanic(T *testing.T) { 93 defer func() { 94 if err := recover(); err != nil { 95 T.Log(err) 96 } 97 }() 98 Panic("log with Panicf") 99 } 100 101 func TestLogPanicf(T *testing.T) { 102 defer func() { 103 if err := recover(); err != nil { 104 T.Log(err) 105 } 106 }() 107 Panicf("%s", "log with Panicf") 108 }