Created
September 7, 2023 15:39
-
-
Save terut/b6dc6a3051006b96c649b6ab1c8febe1 to your computer and use it in GitHub Desktop.
slog example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func logRun() { | |
//ctx := context.Background() | |
replace := func(groups []string, a slog.Attr) slog.Attr { | |
switch { | |
case a.Key == slog.MessageKey: | |
return slog.Attr{Key: "message", Value: a.Value} | |
case a.Key == slog.LevelKey: | |
if l := a.Value.Any().(slog.Level); l == slog.LevelWarn { | |
return slog.String("severity", "WARNING") | |
} | |
return slog.Attr{Key: "severity", Value: a.Value} | |
} | |
return a | |
} | |
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: replace})) | |
logger2 := logger.With("foo", 1) | |
logger.Info("w", slog.Group("request", "method", "GET", slog.Int("status", 200))) | |
logger2.Info("w2") | |
logger.Warn("w", slog.Group("request", "method", "GET", slog.Group("res", slog.Int("status", 200)))) | |
logger.Info("w3") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment