Skip to content

Instantly share code, notes, and snippets.

@mdwhatcott
Created December 6, 2022 18:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdwhatcott/13be2d3bba3288af08b5ce0f446344dc to your computer and use it in GitHub Desktop.
Save mdwhatcott/13be2d3bba3288af08b5ce0f446344dc to your computer and use it in GitHub Desktop.
Achieving log.Lshortfile with the new slog package
// https://pkg.go.dev/golang.org/x/exp/slog
func main() {
options := slog.HandlerOptions{
AddSource: true,
ReplaceAttr: func(a slog.Attr) slog.Attr {
if a.Key != "source" {
return a
}
fields := strings.Split(a.Value.String(), ":")
path := filepath.Base(fields[0])
return slog.String("source", path+":"+fields[1])
},
}
slog.SetDefault(slog.New(options.NewTextHandler(os.Stderr)))
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment