slogger

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

README

slogger

codecov GoDoc

This is a helper package for log/slog to set up a *slog.Logger based on some defaults for text, JSON or output suitable for consumption by sd_journal_stream_fd (i.e., when your application is run as a systemd unit).

Usage

package main

import (
	"log/slog"
	"os"

	"github.com/andrewheberle/slogger"
)

func main() {
	// set up our logLevel variable to allow changing levels
	logLevel := new(slog.LevelVar)

	// create a logger with default settings
	logger, err := slogger.NewLogger(logLevel)
	if err != nil {
		panic(err)
	}

	// level is set to info by default
	logger.Debug("this will not be shown")
	logger.Info("this is an info level message", "withstring", "foo")

	// raise the log level to debug
	logLevel.Set(slog.LevelDebug)

	// output a debug message
	logger.Debug("this message is only shown if the log level is changed", "withnumber", 10)
}

Output Formats

By default the *slog.Logger is created with a handler as follows:

  1. If it is detected that the application is run by systemd (ie systemd is the parent process) logs are output as text to os.Stderr in the following format:

    <LEVEL> MESSAGE [KEY=VALUE KEY=VALUE ...]
    
  2. Otherwise the log is output using slog.NewTextHandler to os.Stderr

It is possible override or force a particular output format by passing slogger.WithLoggerType to slogger.NewLogger with one of the following options:

  • slogger.LoggerTypeAuto - chooses systemd or text output (default)
  • slogger.LoggerTypeDiscard - no output via slog.DiscardHandler
  • slogger.LoggerTypeJson - JSON output via slog.NewJSONHandler
  • slogger.LoggerTypeSystemd - custom text output for by sd_journal_stream_fd
  • slogger.LoggerTypeText - Text output using slog.NewTextHandler

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnhandledLoggerType = errors.New("unhandled logger type")
)

Functions

func NewLogger

func NewLogger(leveler slog.Leveler, opts ...LoggerOption) (*slog.Logger, error)

Types

type LoggerOption

type LoggerOption func(*logger)

func WithLoggerType

func WithLoggerType(lt LoggerType) LoggerOption

func WithWriter

func WithWriter(w io.Writer) LoggerOption

type LoggerType

type LoggerType string
const (
	LoggerTypeAuto    LoggerType = "auto"
	LoggerTypeDiscard LoggerType = "discard"
	LoggerTypeJson    LoggerType = "json"
	LoggerTypeSystemd LoggerType = "systemd"
	LoggerTypeText    LoggerType = "text"
)

type LoggerTypeVar

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

func (*LoggerTypeVar) Set

func (lt *LoggerTypeVar) Set(s string) error

func (*LoggerTypeVar) String

func (lt *LoggerTypeVar) String() string

func (LoggerTypeVar) Type

func (lt LoggerTypeVar) Type() string

type SystemdHandler

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

func NewSystemdHandler

func NewSystemdHandler(w io.Writer, opts *slog.HandlerOptions) *SystemdHandler

func (*SystemdHandler) Enabled

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

func (*SystemdHandler) Handle

func (h *SystemdHandler) Handle(_ context.Context, r slog.Record) error

func (*SystemdHandler) WithAttrs

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

func (*SystemdHandler) WithGroup

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

Jump to

Keyboard shortcuts

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