slogext

package
v0.0.0-...-63b1cac Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2024 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package slogext provides slog helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAtomicBool

func NewAtomicBool(t bool) *atomic.Bool

NewAtomicBool is a convenience function to returns an atomic.Bool with a specified state.

Types

type GoID

type GoID struct {
	slog.Handler
}

GoID is a slog.Handler that adds the calling goroutine's goid.

func (GoID) Handle

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

func (GoID) WithAttrs

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

func (GoID) WithGroup

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

type HandlerOptions

type HandlerOptions struct {
	// AddSource causes the handler to compute the source code position
	// of the log statement and add a SourceKey attribute to the output.
	// A nil AddSource is false.
	AddSource *atomic.Bool

	// Level reports the minimum record level that will be logged.
	Level slog.Leveler

	// ReplaceAttr is called to rewrite each non-group attribute before
	// it is logged.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}

HandlerOptions are options for a JSONHandler. It is derived from the slog.HandlerOptions with a changed AddSource field type to allow dynamically changing AddSource behaviour during run time. A zero HandlerOptions consists entirely of default values.

type JSONHandler

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

JSONHandler is a slog.Handler that writes Records to an io.Writer as line-delimited JSON objects. It differs from the standard library JSONHandler by allowing alteration of the AddSource behaviour after construction.

func NewJSONHandler

func NewJSONHandler(w io.Writer, opts *HandlerOptions) *JSONHandler

NewJSONHandler creates a JSONHandler that writes to w, using the given options. If opts is nil, the default options are used.

func (*JSONHandler) Enabled

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

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*JSONHandler) Handle

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

Handle formats its argument Record as a JSON object on a single line.

See slog.JSONHandler.Handle for details.

func (*JSONHandler) WithAttrs

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

WithAttrs returns a new JSONHandler whose attributes consists of h's attributes followed by attrs.

func (*JSONHandler) WithGroup

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

WithGroup returns a new Handler with the given group appended to h's existing groups.

type PrefixHandler

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

Prefix is a slog.Handler that logs messages with a prefix.

func (*PrefixHandler) Enabled

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

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*PrefixHandler) Handle

func (h *PrefixHandler) Handle(ctx context.Context, rec slog.Record) error

Handle formats its according to the group's handler, but prefixed with the prefix used to initialise h.

See slog.JSONHandler.Handle for details.

func (*PrefixHandler) WithAttrs

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

WithAttrs returns a new JSONHandler whose attributes consists of h's attributes followed by attrs.

func (*PrefixHandler) WithGroup

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

WithGroup returns a new Handler with the given group appended to h's existing groups.

func (*PrefixHandler) Write

func (h *PrefixHandler) Write(b []byte) (int, error)

Write implements the io.Writer interface, writing to the group's io.Writer. Each write to h is prefixed with the receiver's prefix string. If a write spans multiple lines each line is prefixed.

type PrefixHandlerGroup

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

PrefixHandlerGroup is a coordinated set of slog.Handlers sharing a single io.Writer.

func NewPrefixHandlerGroup

func NewPrefixHandlerGroup(w io.Writer, h slog.Handler) *PrefixHandlerGroup

NewPrefixHandlerGroup returns a handler group sharing w and h. w must be the io.Writer used to initialise h.

func (*PrefixHandlerGroup) NewHandler

func (g *PrefixHandlerGroup) NewHandler(prefix string) *PrefixHandler

NewHandler returns a PrefixHandler within g's group. Log messages written to the handler will be prefixed with the provided string.

type PrivateRedact

type PrivateRedact struct {
	Val any
	Tag string
}

func (PrivateRedact) LogValue

func (v PrivateRedact) LogValue() slog.Value

type Request

type Request struct {
	*jsonrpc2.Request
}

Request implements slog.LogValuer for jsonrpc2.Request.

func (Request) LogValue

func (v Request) LogValue() slog.Value

type RequestRedactPrivate

type RequestRedactPrivate struct {
	*jsonrpc2.Request
}

RequestRedactPrivate implements slog.LogValuer for jsonrpc2.Request, redacting fields using private.Redact.

func (RequestRedactPrivate) LogValue

func (v RequestRedactPrivate) LogValue() slog.Value

type Stringer

type Stringer struct {
	fmt.Stringer
}

Stringer implements slog.LogValuer for fmt.Stringer.

func (Stringer) LogValue

func (v Stringer) LogValue() slog.Value

Jump to

Keyboard shortcuts

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