logger

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Copyright (c) 2024 Eli Janssen Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.

Copyright (c) 2024 Eli Janssen Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.

Copyright (c) 2024 Eli Janssen Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.

Copyright (c) 2024 Eli Janssen Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.

Copyright (c) 2024 Eli Janssen Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.

Copyright (c) 2024 Eli Janssen Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.

Copyright (c) 2024 Eli Janssen Use of this source code is governed by an MIT-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextExtractor

func ContextExtractor(ctx context.Context, _ time.Time, _ slog.Level, _ string) []slog.Attr

ExtractPrepended is an AttrExtractor that returns the prepended attributes stored in the context. The returned slice should not be appended to or modified in any way. Doing so will cause a race condition.

func DeferOperation

func DeferOperation[T any, V any](value T, f func(T) V) *defOp[T, V]

func Err

func Err(err error) slog.Attr

func Fatal

func Fatal(msg string, args ...any)

func LogAttrsSkip

func LogAttrsSkip(logger *slog.Logger, skip int, level slog.Level,
	ctx context.Context, msg string, attrs ...slog.Attr,
)

func LogSkip

func LogSkip(logger *slog.Logger, skip int, level slog.Level,
	ctx context.Context, msg string, args ...any,
)

func NewConsoleLogger

func NewConsoleLogger(opts Options) *slog.Logger

func NewContextHandler

func NewContextHandler(next slog.Handler, opts Options) *slog.Logger

func NewJsonLogger

func NewJsonLogger(opts Options) *slog.Logger

func NewTestLogger

func NewTestLogger(opts Options) *slog.Logger

func PrependAttr

func PrependAttr(ctx context.Context, args ...any) context.Context

func SetLevel

func SetLevel(level slog.Level)

func SetupLogging

func SetupLogging(mkLogger func(Options) *slog.Logger, opts *Options)

Types

type AttrExtractor

type AttrExtractor func(context.Context, time.Time, slog.Level, string) []slog.Attr

type ContextHandler

type ContextHandler struct {
	slog.Handler
	Prependers []AttrExtractor
	Appenders  []AttrExtractor
	// contains filtered or unexported fields
}

func (*ContextHandler) Handle

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

Handle handles the Record. It will only be called when Enabled returns true. The Context argument is as for Enabled. It is present solely to provide Handlers access to the context's values. Canceling the context should not affect record processing. (Among other things, log messages may be necessary to debug a cancellation-related problem.)

Handle methods that produce output should observe the following rules:

  • If r.Time is the zero time, ignore the time.
  • If r.PC is zero, ignore it.
  • Attr's values should be resolved.
  • If an Attr's key and value are both the zero value, ignore the Attr. This can be tested with attr.Equal(Attr{}).
  • If a group's key is empty, inline the group's Attrs.
  • If a group has no Attrs (even if it has a non-empty key), ignore it.

func (*ContextHandler) Options

func (h *ContextHandler) Options() Options

func (*ContextHandler) WithAttrs

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

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments. The Handler owns the slice: it may retain, modify or discard it.

func (*ContextHandler) WithGroup

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

WithGroup returns a new Handler with the given group appended to the receiver's existing groups. The keys of all subsequent attributes, whether added by With or in a Record, should be qualified by the sequence of group names.

How this qualification happens is up to the Handler, so long as this Handler's attribute keys differ from those of another Handler with a different sequence of group names.

A Handler should treat WithGroup as starting a Group of Attrs that ends at the end of the log event. That is,

logger.WithGroup("s").LogAttrs(level, msg, slog.Int("a", 1), slog.Int("b", 2))

should behave like

logger.LogAttrs(level, msg, slog.Group("s", slog.Int("a", 1), slog.Int("b", 2)))

If the name is empty, WithGroup returns the receiver.

type Options

type Options struct {
	Sink         io.Writer
	Prependers   []AttrExtractor
	Appenders    []AttrExtractor
	UseLocalTime bool
	OmitTime     bool
	OmitSource   bool
}

Jump to

Keyboard shortcuts

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