github.com/phuslu/log@v1.0.100/logger_std_test.go (about) 1 //go:build go1.21 2 // +build go1.21 3 4 package log 5 6 import ( 7 "fmt" 8 stdLog "log" 9 "log/slog" 10 "os" 11 "testing" 12 ) 13 14 func TestStdLogWriter(t *testing.T) { 15 w := &stdLogWriter{ 16 Logger: Logger{ 17 Level: InfoLevel, 18 Writer: IOWriter{os.Stderr}, 19 }, 20 } 21 22 fmt.Fprint(w, "hello from stdLog debug Print") 23 fmt.Fprintln(w, "hello from stdLog debug Println") 24 fmt.Fprintf(w, "hello from stdLog debug %s", "Printf") 25 } 26 27 func TestStdLogLogger(t *testing.T) { 28 logger := Logger{ 29 Level: DebugLevel, 30 Caller: -1, 31 Context: NewContext(nil).Str("tag", "std_log").Value(), 32 Writer: &ConsoleWriter{ColorOutput: true, EndWithMessage: true}, 33 } 34 35 stdLog := logger.Std("", stdLog.LstdFlags) 36 stdLog.Print("hello from stdLog Print") 37 stdLog.Println("hello from stdLog Println") 38 stdLog.Printf("hello from stdLog %s", "Printf") 39 40 stdLog = logger.Std("", 0) 41 stdLog.Print("hello from stdLog Print") 42 stdLog.Println("hello from stdLog Println") 43 stdLog.Printf("hello from stdLog %s", "Printf") 44 } 45 46 func TestStdSlogNormal(t *testing.T) { 47 var logger *slog.Logger = (&Logger{ 48 Level: InfoLevel, 49 TimeField: "date", 50 TimeFormat: "2006-01-02", 51 Caller: 1, 52 }).Slog() 53 54 logger.Info("hello from slog Info") 55 logger.Warn("hello from slog Warn") 56 logger.Error("hello from slog Error") 57 } 58 59 func TestStdSlogAttrs(t *testing.T) { 60 var logger *slog.Logger = (&Logger{ 61 Level: InfoLevel, 62 TimeField: "date", 63 TimeFormat: "2006-01-02", 64 Caller: -1, 65 }).Slog() 66 67 sublogger := logger.With("logger_name", "sub_logger").WithGroup("g").With("everything", 42) 68 sublogger.Info("hello from sub attr slog") 69 logger.Info("hello from origin slog") 70 } 71 72 func TestStdSlogGroup(t *testing.T) { 73 var logger *slog.Logger = (&Logger{ 74 Level: InfoLevel, 75 TimeField: "date", 76 TimeFormat: "2006-01-02", 77 Caller: 1, 78 }).Slog() 79 80 logger1 := logger.WithGroup("g").With("1", "2").With("3", "4") 81 logger1.Info("hello from group slog 1") 82 logger1.Info("hello from group slog 2") 83 84 logger2 := logger1.WithGroup("g1").With("a", "b").With("c", "d"). 85 WithGroup("g2").With("foo", "bar").With("bar", "foo"). 86 WithGroup("g3").With("x", 1).With("y", 2).With("z", 3) 87 logger2.Info("hello from group slog 3") 88 logger2.Info("hello from group slog 4") 89 90 logger1.Info("hello from group slog 1") 91 logger1.Info("hello from group slog 2") 92 }