Categorygithub.com/systemd/slog-journal
modulepackage
0.1.0
Repository: https://github.com/systemd/slog-journal.git
Documentation: pkg.go.dev

# README

slog: Systemd journal handler

Usage

[!NOTE] Journald only supports keys of the form ^[A-Z_][A-Z0-9_]*$. Any other keys will be silently dropped.

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

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:

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))
}

# Functions

NewHandler returns a new Handler that writes to the systemd journal.

# Constants

Names of levels corresponding to syslog.Priority values.
Names of levels corresponding to syslog.Priority values.
Names of levels corresponding to syslog.Priority values.
Names of levels corresponding to syslog.Priority values.

# Structs

Handler sends logs to the systemd journal.
LevelVar is similar to [slog.LevelVar] but also implements the service side of [RestartMode=debug].
Options configure the Journal handler.