From 2458a758bd504a0bd93188c7cd7877f6007b2b49 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 | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2567d1b..00d54f3 100644 --- a/README.md +++ b/README.md @@ -20,30 +20,32 @@ 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)) +} ```