journal_handler

package
v1.28.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package journal_handler adapts slog to the systemd journal, recording each record as one journal entry whose priority follows the record's level.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler sits on both sides of an slog handler: it is the slog.Handler a logger writes records to, and the io.Writer the wrapped encoder writes bytes to. The detour exists because a record's level has to become a journal priority, which is known when the record arrives but not when its encoded bytes come back. The level is therefore recorded on the way in and read on the way out, with a lock held across the pair so concurrent records cannot take each other's priority.

func New added in v1.26.0

func New(newEncoder func(writer io.Writer) slog.Handler, fields map[string]string) *Handler

New returns a handler recording each slog record as one journal entry, encoded by the handler newEncoder builds. The encoder is built around the returned handler, because the handler is the io.Writer the encoder writes through: that detour is what lets a record's level become the entry's priority.

fields are journal fields added to every entry. SYSLOG_IDENTIFIER is worth setting: without it journald falls back to the process name, which the kernel truncates to 15 characters, so a service with a longer name cannot be selected with "journalctl -t" at all. fields may be nil.

func (*Handler) Enabled

func (handler *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle

func (handler *Handler) Handle(ctx context.Context, record slog.Record) error

func (*Handler) WithAttrs

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

func (*Handler) WithGroup

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

func (*Handler) Write

func (handler *Handler) Write(data []byte) (int, error)

Write sends the encoded record to the journal. It is called by the wrapped encoder, from inside Handle, so the priority it reads is the one Handle set.

Jump to

Keyboard shortcuts

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