logutil

package
v0.0.0-...-0ba627c Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const (
	LevelTrace slog.Level = -8
	LevelFatal slog.Level = 12
)
View Source
const ErrKey = "error"
View Source
const StackKey = "stack"
View Source
const UserKey = "usr"

Variables

View Source
var DefaultConfig = Config{
	Level:  Level(slog.LevelInfo),
	Format: FormatText,
}
View Source
var LevelNames = map[slog.Level]string{
	LevelTrace: "TRACE",
	LevelFatal: "FATAL",
}
View Source
var NameLevels = func() map[string]slog.Level {
	m := make(map[string]slog.Level, len(LevelNames))
	for l, n := range LevelNames {
		m[n] = l
	}
	if len(LevelNames) != len(m) {
		panic("duplicate level value or name")
	}
	return m
}()

Functions

func Destructure

func Destructure(err error) (attrs []slog.Attr)

Destructure recursively extracts attributes from an error chain with scopedError entries. The error chain is modified! Only the first chain with a scopedError is processed for error trees created via errors.Join.

func Err

func Err(err error) slog.Attr

func ErrColor

func ErrColor(value slog.Attr) slog.Attr

func FromContext

func FromContext(ctx context.Context) *slog.Logger

func InstallGoLogShim

func InstallGoLogShim()

func JSON

func JSON(key string, j json.RawMessage) slog.Attr

func LevelAttrReplacer

func LevelAttrReplacer(_ []string, a slog.Attr) slog.Attr

func MustNewHandler

func MustNewHandler(format Format, level slog.Level) slog.Handler

MustNewHandler forwards to NewHandler and panics when it fails. It is still used by AVAS, can eventually go away for mainutil.

func NewError

func NewError(err error, msg string, attrs ...slog.Attr) error

NewError attaches additional attributes to an error. It is allowed to pass a nil err to create a new leaf error. Use it in place of fmt.Errorf("message %q: %w", "detail", err) for nicer formatting in logs. Use Scope for grouping error attributes.

func NewHandler

func NewHandler(format Format, level slog.Level) (slog.Handler, error)

func NewHandlerTo

func NewHandlerTo(w io.Writer, format Format, level slog.Level) (slog.Handler, error)

func Severity

func Severity(err error, level slog.Level) error

Severity attaches a log level to an error chain.

func Stack

func Stack(skip int) slog.Attr

func User

func User(user UserValue) slog.Attr

func WithLogContext

func WithLogContext(ctx context.Context, log *slog.Logger) context.Context

Types

type Config

type Config struct {
	Level  Level  `usage:"TRACE, DEBUG, INFO, WARN, or ERROR"`
	Format Format `usage:"TEXT or JSON"`
}

func (Config) InstallForProcess

func (c Config) InstallForProcess() error

func (Config) NewHandler

func (c Config) NewHandler() (slog.Handler, error)

type Format

type Format string
const (
	FormatText Format = "TEXT"
	FormatJSON Format = "JSON"
)

func (*Format) Set

func (f *Format) Set(s string) error

func (*Format) String

func (f *Format) String() string

func (*Format) Type

func (f *Format) Type() string

func (*Format) UnmarshalText

func (f *Format) UnmarshalText(text []byte) error

type Level

type Level slog.Level

func (*Level) Set

func (l *Level) Set(s string) error

func (*Level) String

func (l *Level) String() string

func (*Level) Type

func (l *Level) Type() string

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(text []byte) error

type LogWriterShim

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

func (*LogWriterShim) Write

func (l *LogWriterShim) Write(p []byte) (n int, err error)

type Scope

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

Scope collects attributes for later instantiation of a logger or an error.

func NewScope

func NewScope(group string, attrs ...slog.Attr) *Scope

NewScope returns a scope with attributes for later instantiation of a logger or an error. The scope's group name is used for grouping the scope's attributes via slog.Group.

func (*Scope) Add

func (s *Scope) Add(attrs ...slog.Attr)

Add the given attributes to the current scope.

func (*Scope) Err

func (s *Scope) Err(err error, msg string, attrs ...slog.Attr) error

Err wraps an error to propagate the scope's current attributes, plus additional attributes. The inner error should not be nil.

func (*Scope) Log

func (s *Scope) Log(log *slog.Logger, attrs ...slog.Attr) *slog.Logger

Log returns a slog.Logger with the scope's current attributes, plus additional attributes.

func (*Scope) New

func (s *Scope) New(msg string, attrs ...slog.Attr) error

New creates a new error with the given message and attributes. The error inherits the scope's current attributes.

func (*Scope) Sub

func (s *Scope) Sub(attrs ...slog.Attr) *Scope

Sub returns a new Scope with the current scope's group, attributes, plus additional attributes.

type UserValue

type UserValue struct {
	ID    string `json:"id,omitempty"`
	Name  string `json:"name,omitempty"`
	Email string `json:"email,omitempty"`
}

UserValue is a generic user identifier modeled after Datadog's standard log attributes.

Jump to

Keyboard shortcuts

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