Skip to content

Commit 37c91c7

Browse files
authored
Merge pull request #11 from systemd/ready-for-publish
Ready for publish
2 parents 03fca0d + 0889a21 commit 37c91c7

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

README.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,45 @@
66
> Journald only supports keys of the form `^[A-Z_][A-Z0-9_]*$`. Any other keys will be silently dropped.
77
88
```go
9-
h := slogjournal.NewHandler(nil)
9+
h , err := slogjournal.NewHandler(nil)
1010
log := slog.New(h)
1111
log.Info("Hello, world!", "EXTRA_KEY", "5")
1212
log.Info("Hello, world!", slog.Group("HTTP", "METHOD", "put", "URL", "http://example.com"))
1313
```
1414

15+
### Make sure your logs are compatible with the journal
1516

16-
> [!CAUTION]
17-
> This is pre-release software. No releases have been tagged yet.
17+
When using third-party slog libraries, you do not have control over the attributes that are passed to the logger.
18+
Because the journal only supports keys of the form `^[A-Z_][A-Z0-9_]*$`, you may need to transform keys that don't match this pattern.
19+
For this you can use the `ReplaceGroup` and `ReplaceAttr` fields in `Options`:
20+
21+
22+
```go
23+
package main
24+
25+
import (
26+
"log/slog"
27+
sloghttp "github.com/samber/slog-http"
28+
slogjournal "github.com/systemd/slog-journal"
29+
)
30+
31+
func main() {
32+
h , err := slogjournal.NewHandler(&slogjournal.Options{
33+
ReplaceGroup: func(k string) string {
34+
return strings.ReplaceAll(strings.ToUpper(k), "-", "_")
35+
},
36+
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
37+
a.Key = strings.ReplaceAll(strings.ToUpper(a.Key), "-", "_")
38+
return a
39+
},
40+
})
41+
42+
log := slog.New(h)
43+
mux := http.NewServeMux()
44+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
45+
log.Info("Hello world")
46+
w.Write([]byte("Hello, world!"))
47+
})
48+
http.ListenAndServe(":8080", sloghttp.New(log)(mux))
49+
}
50+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/arianvp/slog-journal
1+
module github.com/systemd/slog-journal
22

33
go 1.22.1
44

journal.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package slogjournal provides a handler for the systemd journal.
2+
// The journal only accepts keys of the form ^[A-Z_][A-Z0-9_]*$.
13
package slogjournal
24

35
import (

0 commit comments

Comments
 (0)