Skip to content

Commit

Permalink
Merge pull request #11 from systemd/ready-for-publish
Browse files Browse the repository at this point in the history
Ready for publish
  • Loading branch information
arianvp authored Jan 12, 2025
2 parents 03fca0d + 0889a21 commit 37c91c7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,45 @@
> Journald only supports keys of the form `^[A-Z_][A-Z0-9_]*$`. Any other keys will be silently dropped.
```go
h := slogjournal.NewHandler(nil)
h , err := slogjournal.NewHandler(nil)
log := slog.New(h)
log.Info("Hello, world!", "EXTRA_KEY", "5")
log.Info("Hello, world!", slog.Group("HTTP", "METHOD", "put", "URL", "http://example.com"))
```

### Make sure your logs are compatible with the journal

> [!CAUTION]
> This is pre-release software. No releases have been tagged yet.
When using third-party slog libraries, you do not have control over the attributes that are passed to the logger.
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.
For this you can use the `ReplaceGroup` and `ReplaceAttr` fields in `Options`:


```go
package main

import (
"log/slog"
sloghttp "github.com/samber/slog-http"
slogjournal "github.com/systemd/slog-journal"
)

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))
}
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/arianvp/slog-journal
module github.com/systemd/slog-journal

go 1.22.1

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

import (
Expand Down

0 comments on commit 37c91c7

Please sign in to comment.