flash

package
v0.9.9-beta-9 Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package flash provides a feature-complete, performant slog.Handler implementation.

This handler started as a clone of the sloggy handler and was then tweaked for performance.

Extra Options

When creating a flash handler via NewHandler basic options can be specified by the slog.HandlerOptions options argument. In addition, the handler also supports optional flash.Extras options which can be used to adjust basic logging behavior slightly. This can be used to test the behavior of ReplaceAttr functionality or to match the behavior of another logging library.

Performance Edits

After flash was cloned from sloggy it went through a number of performance-related edits.

Index

Examples

Constants

View Source
const (
	DefaultTimeFormat = time.RFC3339Nano
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Extras

type Extras struct {
	// TimeFormat holds the format of the basic time field.
	// If not set defaults to the value of flash.DefaultTimeFormat (= time.RFC3339Nano).
	TimeFormat string

	// LevelNames holds a map from slog.Level to string.
	// If these fields are configured they replace the usual level names.
	// It is possible to configure only some of the level names.
	// Any level name that is not configured will be set to the appropriate
	// slog global constant (e.g. slog.LevelInfo.String()).
	LevelNames map[slog.Level]string

	// LevelKey specifies the JSON field name for the slog.Level for the log records.
	// If this field is not configured the value of slog.LevelKey is used.
	LevelKey string

	// MessageKey specifies the JSON field name for the log message.
	// If this field is not configured the value of slog.MessageKey is used.
	MessageKey string

	// SourceKey specifies the JSON field name for source data.
	// If this field is not configured the value of slog.SourceKey is used.
	SourceKey string

	// TimeKey specifies the JSON field name for the time the record was logged.
	// If this field is not configured the value of slog.TimeKey is used.
	TimeKey string
}

Extras defines extra options specific to a flash.Handler.

Using these options it is possible to override some of the log/slog "standard" behavior. This supports testing of slog.HandlerOptions.ReplaceAttr functions and may also be used to replicate non-standard behavior in other handlers.

type Handler

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

Handler provides a feature-complete, performant slog.Handler implementation.

Example
var buff bytes.Buffer
logger := slog.New(NewHandler(&buff, nil, 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, extras *Extras) *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. If the extras argument is nil it will be set to defaults that match slog.JSONHandler.

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