logging

package
v0.1.3 Latest Latest
Warning

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

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

Documentation

Overview

Package logging provides log/slog handlers with an established human-readable line format:

2006/01/02 15:04:05 [LEVEL] [service] file.go:42 - message | key=value

It is stdlib-only, and it is a handler package rather than a logger API: applications use the standard *slog.Logger on top, so nothing here is a dependency they cannot walk away from. New wires a logger from the conventional environment variables (LOG_LEVEL, LOG_FORMAT=json for machine-readable output, NO_COLOR / LOG_NO_COLOR).

The printf-style helpers -- InitializeGlobalLogger plus Debug/Info/Warn/Error/Fatal -- are a thin, stateless convenience over slog's default logger for code that has not moved to structured attributes. They hold no state and return nothing; ignoring them costs nothing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...any)

Debug logs a printf-formatted message at debug level.

func Error

func Error(msg string, args ...any)

Error logs a printf-formatted message at error level.

func Fatal

func Fatal(msg string, args ...any)

Fatal logs a printf-formatted message at error level and exits.

func Info

func Info(msg string, args ...any)

Info logs a printf-formatted message at info level.

func InitializeGlobalLogger

func InitializeGlobalLogger(serviceName string)

InitializeGlobalLogger installs the service's logger as slog's default. Configuration comes from LOG_LEVEL, LOG_FORMAT and NO_COLOR/LOG_NO_COLOR.

func LevelFromEnv

func LevelFromEnv() slog.Level

LevelFromEnv parses LOG_LEVEL into a slog.Level, defaulting to Info.

func New

func New(service string) *slog.Logger

New returns a *slog.Logger configured from the environment: LOG_LEVEL (debug/info/warn/error), LOG_FORMAT=json for slog's JSON handler, and NO_COLOR / LOG_NO_COLOR to disable colors.

func Warn

func Warn(msg string, args ...any)

Warn logs a printf-formatted message at warn level.

Types

type Handler

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

Handler is a slog.Handler emitting the platform line format.

func NewHandler

func NewHandler(opts Options) *Handler

NewHandler creates a Handler with the given options.

func (*Handler) Enabled

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

Enabled implements slog.Handler.

func (*Handler) Handle

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

Handle implements slog.Handler.

func (*Handler) WithAttrs

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

WithAttrs implements slog.Handler.

func (*Handler) WithGroup

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

WithGroup implements slog.Handler; groups are flattened into key prefixes.

type Options

type Options struct {
	// Service names the emitting service in every line.
	Service string
	// Level is the minimum level to emit.
	Level slog.Leveler
	// Colors enables ANSI level colors.
	Colors bool
	// Writer receives the output; defaults to os.Stdout.
	Writer io.Writer
}

Options configures a Handler.

Jump to

Keyboard shortcuts

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