slogjournal

package module
v0.1.0 Latest Latest
Warning

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

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

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

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](https://systemd.io/JOURNAL_NATIVE_PROTOCOL/). Journal only supports keys of the form ^[A-Z_][A-Z0-9_]*$. Any other keys will be silently dropped.

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.

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