slogutil

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 With

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

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

Types

type DynamicLeveler

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

func SetupLogRotation

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

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. Returns the logger and a DynamicLeveler for runtime level changes.

func SetupLogRotationWithFallback

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

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

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 Handler

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

Handler is a slog.Handler with hooks support.

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.

Jump to

Keyboard shortcuts

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