journal_handler

package
v1.25.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: 6 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 NewJsonHandler

func NewJsonHandler(handlerOptions *slog.HandlerOptions) *Handler

NewJsonHandler returns a handler recording each record as a JSON object in the journal. handlerOptions may be nil.

func NewJsonHandlerWithFields added in v1.25.0

func NewJsonHandlerWithFields(handlerOptions *slog.HandlerOptions, fields map[string]string) *Handler

NewJsonHandlerWithFields is NewJsonHandler with 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 longer name cannot be selected with "journalctl -t". 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