slogutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const MessageKey = "message"

Variables

This section is empty.

Functions

func Attrs

func Attrs(ctx context.Context) []slog.Attr

Attrs returns the attributes in the context.

func Data

func Data(ctx context.Context) map[string]any

Data returns the attributes in the context as a map.

func GetLogFilePath

func GetLogFilePath(logConfig config.LogConfig) string

GetLogFilePath returns the resolved log file path. If logConfig.File is empty, it returns the default "activity.log".

func IterAttrs

func IterAttrs(ctx context.Context) func(func(attr slog.Attr) bool)

IterAttrs walks through the attributes in the context. The return value is compatible with iter.Seq[slog.Attr] to allow range func.

Example:

for attr := range slogutil.IterAttrs(ctx) {
	// DO SOMETHING
}

Feature description: https://tip.golang.org/wiki/RangefuncExperiment

func SetupLogRotation

func SetupLogRotation(logConfig config.LogConfig) *slog.Logger

SetupLogRotation configures slog with log rotation using lumberjack. Always logs to both stdout (text format) and a file (JSON format). The file path defaults to "activity.log" if logConfig.File is empty.

func SetupLogRotationWithFallback

func SetupLogRotationWithFallback(logConfig config.LogConfig, legacyLogLevel string) *slog.Logger

SetupLogRotationWithFallback sets up log rotation with backward compatibility It checks both new log config and legacy log_level setting

func With

func With(ctx context.Context, kvargs ...any) context.Context

With returns a new context with the given key-value pairs.

func WithAttrs

func WithAttrs(ctx context.Context, attrs ...slog.Attr) context.Context

WithAttrs returns a new context with the given attributes.

Types

type Config

type Config struct {
	Level       slog.Leveler
	ReplaceAttr ReplaceAttrFunc
	Hooks       []Hook
	AddSource   bool
	LogPath     string
}

type DynamicLeveler

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

func (*DynamicLeveler) Level

func (dl *DynamicLeveler) Level() slog.Level

Level returns the current logging level.

func (*DynamicLeveler) SetLevel

func (dl *DynamicLeveler) SetLevel(level slog.Level)

SetLevel updates the logging level.

type Format

type Format string

type Handler

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

Handler is a slog.Handler with hooks support.

func NewHandler

func NewHandler(config ...Config) Handler

NewHandler creates a new Handler with the given configuration.

func WrapHandler

func WrapHandler(h slog.Handler) Handler

WrapHandler creates a new Handler with the given slog.Handler. If the provided handler is nil, a default JSON handler is used.

func (Handler) Enabled

func (h Handler) Enabled(ctx context.Context, l slog.Level) bool

func (Handler) Handle

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

func (Handler) WithAttrs

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

func (Handler) WithGroup

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

func (Handler) WithHooks

func (h Handler) WithHooks(hooks ...Hook) Handler

type Hook

type Hook interface {
	Run(ctx context.Context, r *slog.Record)
}

Hook is called when a slog record is handled.

type ReplaceAttrFunc

type ReplaceAttrFunc func(groups []string, a slog.Attr) slog.Attr

Jump to

Keyboard shortcuts

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