Skip to content

Commit 372cb9d

Browse files
authored
Better docs (#12)
1 parent 37c91c7 commit 372cb9d

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# slog: Systemd journal handler
22

3-
## Usage
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/systemd/slog-journal.svg)](https://pkg.go.dev/github.com/systemd/slog-journal)
44

5-
> [!NOTE]
6-
> Journald only supports keys of the form `^[A-Z_][A-Z0-9_]*$`. Any other keys will be silently dropped.
5+
## Usage
76

87
```go
98
h , err := slogjournal.NewHandler(nil)

journal.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type LevelVar struct {
3737
}
3838

3939
// Return v's level.
40+
// When invoked for the first time, checks if the environment variable DEBUG_INVOCATION is set and if so, sets the level to slog.LevelDebug before returning it.
4041
func (v *LevelVar) Level() slog.Level {
4142
sync.OnceFunc(func() {
4243
if os.Getenv("DEBUG_INVOCATION") != "" {
@@ -102,12 +103,14 @@ type Handler struct {
102103

103104
const sndBufSize = 8 * 1024 * 1024
104105

105-
// NewHandler returns a new Handler that writes to the systemd journal.
106+
// NewHandler returns a new Handler that writes to the [systemd journal].
106107
// The journal only accepts keys of the form ^[A-Z_][A-Z0-9_]*$.
107108
// If opts is nil, the default options are used.
108109
// If opts.Level is nil, the default level is a [LevelVar] which is equivalent to
109110
// slog.LevelInfo unless the environment variable DEBUG_INVOCATION is set, in
110111
// which case it is slog.LevelDebug.
112+
//
113+
// [systemd journal]: https://systemd.io/JOURNAL_NATIVE_PROTOCOL/
111114
func NewHandler(opts *Options) (*Handler, error) {
112115
h := &Handler{}
113116

@@ -140,9 +143,28 @@ func (h *Handler) Enabled(_ context.Context, level slog.Level) bool {
140143

141144
var identifier = []byte(path.Base(os.Args[0]))
142145

143-
// Handle handles the Record and formats it as a [journal message](https://systemd.io/JOURNAL_NATIVE_PROTOCOL/).
146+
// Handle handles the Record and formats it as a [journal message].
147+
// The Message field maps to the [MESSAGE] field in the journal.
148+
// The Level field maps to the [PRIORITY] field in the journal.
149+
// The PC field maps to the [CODE_FILE, CODE_FUNC and CODE_LINE] fields in the journal.
150+
// The Time field maps to the [SYSLOG_TIMESTAMP] field in the journal.
151+
// The Attrs field maps to the [KEY=VALUE] fields in the journal.
152+
// The [SYSLOG_IDENTIFIER] field is set to the base name of the program.
144153
// Journal only supports keys of the form ^[A-Z_][A-Z0-9_]*$.
154+
// Keys starting with an underscore are reserved for internal use and will be dropped.
145155
// Any other keys will be silently dropped.
156+
//
157+
// Message keys may appear multiple times.
158+
// Message values may contain arbitrary binary data.
159+
// If the message does not fit in a single datagram, the message is sent as a file descriptor pointing to a tempfd.
160+
// If the tempfd feature is not available, the message is sent as a file descriptor pointing to a temporary file in /dev/shm.
161+
//
162+
// [journal message]: https://www.freedesktop.org/software/systemd/man/latest/systemd.journal-fields.html
163+
// [MESSAGE]: https://www.freedesktop.org/software/systemd/man/latest/systemd.journal-fields.html#MESSAGE=
164+
// [PRIORITY]: https://www.freedesktop.org/software/systemd/man/latest/systemd.journal-fields.html#PRIORITY=
165+
// [CODE_FILE, CODE_FUNC and CODE_LINE]: https://www.freedesktop.org/software/systemd/man/latest/systemd.journal-fields.html#CODE_FILE
166+
// [SYSLOG_TIMESTAMP]: https://www.freedesktop.org/software/systemd/man/latest/systemd.journal-fields.html#SYSLOG_FACILITY=
167+
// [SYSLOG_IDENTIFIER]: https://www.freedesktop.org/software/systemd/man/latest/systemd.journal-fields.html#SYSLOG_FACILITY=
146168
func (h *Handler) Handle(ctx context.Context, r slog.Record) error {
147169
buf := make([]byte, 0, 1024)
148170
buf = h.appendKV(buf, "MESSAGE", []byte(r.Message))

0 commit comments

Comments
 (0)