general

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Formatter

type Formatter interface {
	// Append at the beginning of the log event.
	AppendBegin([]byte) []byte
	// Append at the end of the log event.
	AppendEnd([]byte) []byte
	// Append when a group with the given name starts.
	AppendOpenGroup(buf []byte, name string) []byte
	// Append when a group with the given name ends.
	AppendCloseGroup(buf []byte, name string) []byte
	// Append an Attr in the context of the given groups.
	AppendAttr(buf []byte, a slog.Attr, groups []string) []byte
	// Append a separator between Attrs, if one is needed.
	AppendSeparatorIfNeeded([]byte) []byte
}

A Formatter formats log output. Each Formatter method takes a []byte buffer, which it should append to and then return.

type Handler

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

Handler implements a slog.Handler that can produce a variety of output formats. It uses the Formatter interface to produce output.

The main requirement of a format is that preformatted material--groups and attributes that were added using WithGroup and WithAttrs--can be concatenated between the built-in attributes and the remaining attributes.

func New

func New(w io.Writer, newFormatter func() Formatter) *Handler

New constructs a Handler with the default options.

func (*Handler) Enabled

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

func (*Handler) Handle

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

func (*Handler) WithAttrs

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

func (*Handler) WithGroup

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

type Options

type Options struct {
	// Level reports the minimum level to log.
	// Levels with lower levels are discarded.
	// If nil, the Handler uses [slog.LevelInfo].
	Level slog.Leveler

	// ReplaceAttr rewrites Attrs.
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr

	// PCAttrs returns the Attrs to use for source location.
	// If nil, no source information is output.
	PCAttrs func(pc uintptr) []slog.Attr
}

Options are options for a Handler.

func (Options) New

func (opts Options) New(w io.Writer, newFormatter func() Formatter) *Handler

New constructs a Handler with the given options.

Jump to

Keyboard shortcuts

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