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 0889a21
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
```

0 comments on commit 0889a21

Please sign in to comment.