Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
arianvp committed Jan 12, 2025
1 parent ff5b88e commit 2458a75
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
```


Expand Down

0 comments on commit 2458a75

Please sign in to comment.