package main import ( "fmt" "log/slog" "os" ) func main() { slog.Error("This is an ERROR message") slog.Debug("This is a DEBUG message") slog.Info("This is an INFO message") slog.Warn("This is a WARNING message") // Default log level logLevel := &slog.LevelVar{} fmt.Println("Log level:", logLevel) // Text Handler opts := &slog.HandlerOptions{ Level: logLevel, } handler := slog.NewTextHandler(os.Stdout, opts) logger := slog.New(handler) // Change log level of logger logLevel.Set(slog.LevelDebug) logger.Debug("This is a DEBUG message") // JSON Handler logJSON := slog.New(slog.NewJSONHandler(os.Stdout, nil)) logJSON.Error("ERROR message in JSON") }
zzh@ZZHPC:/zdata/Github/mastering-go-expertise$ go run main.go 2024/06/20 10:07:22 ERROR This is an ERROR message 2024/06/20 10:07:22 INFO This is an INFO message 2024/06/20 10:07:22 WARN This is a WARNING message Log level: LevelVar(INFO) time=2024-06-20T10:07:22.133+08:00 level=DEBUG msg="This is a DEBUG message" {"time":"2024-06-20T10:07:22.133359176+08:00","level":"ERROR","msg":"ERROR message in JSON"}
标签:06,log,level,slog,ERROR,Go,message From: https://www.cnblogs.com/zhangzhihui/p/18258151