slogjournal

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2025 License: BSD-3-Clause Imports: 17 Imported by: 1

README

slog: Systemd journal handler

Go Reference

Usage

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

Documentation

Overview

Package slogjournal provides a handler for the systemd journal. The journal only accepts keys of the form ^[A-Z_][A-Z0-9_]*$.

Index

Constants

View Source
const (
	LevelNotice    slog.Level = slog.LevelInfo + 1
	LevelCritical  slog.Level = slog.LevelError + 1
	LevelAlert     slog.Level = slog.LevelError + 2
	LevelEmergency slog.Level = slog.LevelError + 3
)

Names of levels corresponding to syslog.Priority values.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler sends logs to the systemd journal. The journal only accepts keys of the form ^[A-Z_][A-Z0-9_]*$.

func NewHandler

func NewHandler(opts *Options) (*Handler, error)

NewHandler returns a new Handler that writes to the systemd journal. The journal only accepts keys of the form ^[A-Z_][A-Z0-9_]*$. If opts is nil, the default options are used. If opts.Level is nil, the default level is a LevelVar which is equivalent to slog.LevelInfo unless the environment variable DEBUG_INVOCATION is set, in which case it is slog.LevelDebug.

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower. It is called early, before any arguments are processed, to save effort if the log event should be discarded.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle handles the Record and formats it as a journal message. The Message field maps to the MESSAGE field in the journal. The Level field maps to the PRIORITY field in the journal. The PC field maps to the CODE_FILE, CODE_FUNC and CODE_LINE fields in the journal. The Time field maps to the SYSLOG_TIMESTAMP field in the journal. The Attrs field maps to the [KEY=VALUE] fields in the journal. The SYSLOG_IDENTIFIER field is set to the base name of the program. Journal only supports keys of the form ^[A-Z_][A-Z0-9_]*$. Keys starting with an underscore are reserved for internal use and will be dropped. Any other keys will be silently dropped.

Message keys may appear multiple times. Message values may contain arbitrary binary data. If the message does not fit in a single datagram, the message is sent as a file descriptor pointing to a tempfd. If the tempfd feature is not available, the message is sent as a file descriptor pointing to a temporary file in /dev/shm.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup returns a new Handler with the given group appended to the receiver's existing groups.

type LevelVar

type LevelVar struct {
	slog.LevelVar
}

LevelVar is similar to slog.LevelVar but also implements the service side of RestartMode=debug. It looks if the environment variable DEBUG_INVOCATION is set and if so, sets the level to slog.LevelDebug. The zero value of LevelVar is equivalent to slog.LevelInfo. In the future, we might extend the behaviour of LevelVar to implement org.freedesktop.LogControl1.

func (*LevelVar) Level

func (v *LevelVar) Level() slog.Level

Return v's level. 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.

type Options

type Options struct {
	Level slog.Leveler

	// ReplaceAttr is called on all non-builtin Attrs before they are written.
	// This can be useful for processing attributes to be in the correct format
	// for log statements outside of your own code as the journal only accepts
	// keys of the form ^[A-Z_][A-Z0-9_]*$.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr

	// ReplaceGroup is called on all group names before they are written.  This
	// can be useful for processing group names to be in the correct format for
	// log statements outside of your own code as the journal only accepts
	// keys of the form ^[A-Z_][A-Z0-9_]*$.
	ReplaceGroup func(group string) string
}

Options configure the Journal handler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL