From 931f841d408de444ae99f50d99cc0a72e0f87c22 Mon Sep 17 00:00:00 2001 From: Arian van Putten Date: Sun, 12 Jan 2025 14:55:04 +0100 Subject: [PATCH 1/6] update docs --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- journal.go | 7 +++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 91b3759..81caee0 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ > 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")) @@ -14,4 +14,48 @@ log.Info("Hello, world!", slog.Group("HTTP", "METHOD", "put", "URL", "http://exa > [!CAUTION] -> This is pre-release software. No releases have been tagged yet. \ No newline at end of file +> This is pre-release software. No releases have been tagged yet. + + +### Make sure your logs are compatible with the journal + +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 +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)) +``` + + + + + + + + + +``` \ No newline at end of file diff --git a/journal.go b/journal.go index e05e381..d3e9371 100644 --- a/journal.go +++ b/journal.go @@ -1,3 +1,10 @@ +/* +Package slogjournal provides a handler for the systemd journal. + +The journal only accepts keys of the form ^[A-Z_][A-Z0-9_]*$. + +Example: +*/ package slogjournal import ( From c3de3b26cf0ed43e18d1ff5a58ee2b2463d9b79b Mon Sep 17 00:00:00 2001 From: Arian van Putten Date: Sun, 12 Jan 2025 14:55:33 +0100 Subject: [PATCH 2/6] rename to github.com/systemd/slog-journal --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 58c5316..24d3a46 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/arianvp/slog-journal +module github.com/systemd/slog-journal go 1.22.1 From a108f7d773ff22ecabaf3971d6a12f44ddb2c66e Mon Sep 17 00:00:00 2001 From: Arian van Putten Date: Sun, 12 Jan 2025 14:56:08 +0100 Subject: [PATCH 3/6] more docs --- journal.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/journal.go b/journal.go index d3e9371..7110347 100644 --- a/journal.go +++ b/journal.go @@ -1,9 +1,6 @@ /* Package slogjournal provides a handler for the systemd journal. - The journal only accepts keys of the form ^[A-Z_][A-Z0-9_]*$. - -Example: */ package slogjournal From 2ecdaae4e1fc1a3a97a60127caab8fefe2514ff8 Mon Sep 17 00:00:00 2001 From: Arian van Putten Date: Sun, 12 Jan 2025 14:56:30 +0100 Subject: [PATCH 4/6] Remove pre-release note --- README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/README.md b/README.md index 81caee0..2567d1b 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,6 @@ log.Info("Hello, world!", "EXTRA_KEY", "5") log.Info("Hello, world!", slog.Group("HTTP", "METHOD", "put", "URL", "http://example.com")) ``` - -> [!CAUTION] -> This is pre-release software. No releases have been tagged yet. - - ### Make sure your logs are compatible with the journal When using third-party slog libraries, you do not have control over the attributes that are passed to the logger. @@ -25,6 +20,7 @@ For this you can use the `ReplaceGroup` and `ReplaceAttr` fields in `Options`: ```go +packag import ( "log/slog" sloghttp "github.com/samber/slog-http" From ff5b88e8d53f68274c7840cfc0800c265d435aaa Mon Sep 17 00:00:00 2001 From: Arian van Putten Date: Sun, 12 Jan 2025 14:56:56 +0100 Subject: [PATCH 5/6] change header --- journal.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/journal.go b/journal.go index 7110347..dc52a33 100644 --- a/journal.go +++ b/journal.go @@ -1,7 +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 provides a handler for the systemd journal. +// The journal only accepts keys of the form ^[A-Z_][A-Z0-9_]*$. package slogjournal import ( From 0889a216446fa240507c1d6b0cc4bf141afdc1f1 Mon Sep 17 00:00:00 2001 From: Arian van Putten Date: Sun, 12 Jan 2025 14:57:44 +0100 Subject: [PATCH 6/6] add example --- README.md | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 2567d1b..cb64dcf 100644 --- a/README.md +++ b/README.md @@ -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)) +} ``` \ No newline at end of file