logging

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// Level types
	LevelDebug = "debug"
	LevelInfo  = "info"
	LevelWarn  = "warn"
	LevelError = "error"

	// Format types
	FormatJSON   = "json"
	FormatText   = "text"
	FormatRaw    = "raw"
	FormatPretty = "pretty"
)

Variables

This section is empty.

Functions

func Init

func Init(opts *Opts) error

Init initializes the default slog logger based on the provided options.

func NewLogger

func NewLogger(opts *Opts) (*slog.Logger, error)

Types

type ContextHandler

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

ContextHandler wraps an slog.Handler and extracts values from context before logging.

func NewContextHandler

func NewContextHandler(handler slog.Handler, extract ExtractFromContextFn) *ContextHandler

NewContextHandler creates a new handler that extracts context values before delegating to the wrapped handler.

func (*ContextHandler) Enabled

func (h *ContextHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*ContextHandler) Handle

func (h *ContextHandler) Handle(ctx context.Context, r slog.Record) error

func (*ContextHandler) WithAttrs

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

func (*ContextHandler) WithGroup

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

type ExtractFromContextFn

type ExtractFromContextFn func(context.Context) []any

ExtractFromContextFn defines a function that extracts key-value pairs from context. It should return pairs of keys and values as []any, e.g., []any{"key1", "value1", "key2", "value2"}

type Opts

type Opts struct {
	Fields   []string `long:"field" env:"FIELD" description:"Inject fields at the topline level, using k:v"`
	Level    string   `long:"level" env:"LEVEL" description:"Log level: debug, info, warn, error" default:"info"`
	Format   string   `long:"format" env:"FORMAT" description:"Log format: json, text, raw, pretty" default:"json"`
	FilePath string   `long:"file" env:"FILE" description:"Log to file instead of stderr"`
}

Opts holds logging configuration options.

type RawHandler

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

RawHandler is a simple handler that outputs messages with key-value pairs in a simple format.

func NewRawHandler

func NewRawHandler(w io.Writer, opts *slog.HandlerOptions) *RawHandler

NewRawHandler creates a new RawHandler.

func (*RawHandler) Enabled

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

Enabled reports whether the handler handles records at the given level.

func (*RawHandler) Handle

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

Handle outputs the log message with key-value pairs in a simple format.

func (*RawHandler) WithAttrs

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

WithAttrs returns a new handler with additional attributes.

func (*RawHandler) WithGroup

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

WithGroup returns a new handler with a group name.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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