sloggy

package
v0.9.8-beta-8 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package sloggy provides a fairly straightforward slog.Handler implementation. It is feature-complete per observed slog.JSONHandler behavior.

This was an initial, naive attempt to write a "better" handler (because hubris). It is the second "feature complete" handler after slog.JSONHandler (which is admittedly used as a "default behavior" model throughout the verification suite). A user could switch between the two handlers and be reasonably confident that the log output would be the same.

Performance not as good as slog.JSONHandler. The only planned performance enhancement was to use prefix/postfix byte arrays, otherwise this was a green field build with performance left until later. The flash handler, originally a copy of this one, has been tweaked for performance.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComposeAttributes

func ComposeAttributes(buffer *bytes.Buffer, attrs []slog.Attr) error

ComposeAttributes is a public function provided to support benchmark testing in the flash package. It is not intended for any other use.

Types

type Handler

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

Handler provides a fairly straightforward, feature-complete slog.Handler implementation.

Example
var buff bytes.Buffer
logger := slog.New(NewHandler(&buff, nil))
logger.Info("hello", "count", math.Pi)
var logMap map[string]any
_ = json.Unmarshal(buff.Bytes(), &logMap)
fmt.Printf("%s %6.5f\n", logMap["msg"], logMap["count"].(float64))
Output:

hello 3.14159

func NewHandler

func NewHandler(writer io.Writer, options *slog.HandlerOptions) *Handler

NewHandler returns a new sloggy handler with the specified output writer and slog.HandlerOptions. If the options argument is nil it will be set to a level of slog.LevelInfo and nothing else.

func (*Handler) Enabled

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

func (*Handler) Handle

func (h *Handler) Handle(_ context.Context, record 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

Jump to

Keyboard shortcuts

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