From 0889a216446fa240507c1d6b0cc4bf141afdc1f1 Mon Sep 17 00:00:00 2001 From: Arian van Putten Date: Sun, 12 Jan 2025 14:57:44 +0100 Subject: [PATCH] add example --- README.md | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 2567d1b..cb64dcf 100644 --- a/README.md +++ b/README.md @@ -20,38 +20,31 @@ For this you can use the `ReplaceGroup` and `ReplaceAttr` fields in `Options`: ```go -packag +package main + import ( "log/slog" sloghttp "github.com/samber/slog-http" slogjournal "github.com/systemd/slog-journal" ) -h , err := slogjournal.NewHandler(&slogjournal.Options{ - ReplaceGroup: func(k string) string { - return strings.ReplaceAll(strings.ToUpper(k), "-", "_") - }, - ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { - a.Key = strings.ReplaceAll(strings.ToUpper(a.Key), "-", "_") - return a - }, -}) - -log := slog.New(h) -mux := http.NewServeMux() -mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - log.Info("Hello world") - w.Write([]byte("Hello, world!")) -}) -http.ListenAndServe(":8080", sloghttp.New(log)(mux)) -``` - - - - - - - - - +func main() { + h , err := slogjournal.NewHandler(&slogjournal.Options{ + ReplaceGroup: func(k string) string { + return strings.ReplaceAll(strings.ToUpper(k), "-", "_") + }, + ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { + a.Key = strings.ReplaceAll(strings.ToUpper(a.Key), "-", "_") + return a + }, + }) + + log := slog.New(h) + mux := http.NewServeMux() + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + log.Info("Hello world") + w.Write([]byte("Hello, world!")) + }) + http.ListenAndServe(":8080", sloghttp.New(log)(mux)) +} ``` \ No newline at end of file