plog

package
v0.0.1-beta.2 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2024 License: BSD-3-Clause, BSD-3-Clause Imports: 9 Imported by: 0

README

Plonk's Logger Wrapper

Wraps slog. Adds the following:

Documentation

Overview

Package plog - Logging convenience functions

Index

Examples

Constants

View Source
const (
	LKeyError = "error"
)

Canned attr keys.

Variables

View Source
var (
	/* DefaultLevelVar controls the level at which Logger logs. */
	DefaultLevelVar slog.LevelVar

	/* Default is our own default logger.  It may be used instead of
	slog.Default().  Its level is controlled by DefaultLevelVar. */
	Default = slog.New(NewHandler(&DefaultLevelVar, slog.Default().Handler()))
)

Functions

func ErrorAttr

func ErrorAttr(err error) slog.Attr

ErrorAttr turns the error into an slog.Attr with the key KeyError.

func ErrorError

func ErrorError(sl *slog.Logger, msg string, err error, args ...any)

ErrorError logs the error with sl.Error, or slog.Error if sl is nil.

func ErrorLogLogger

func ErrorLogLogger(msg string, logger *slog.Logger, debugREs ...*regexp.Regexp) *log.Logger

ErrorLogLogger returns a log.Logger which logs at level ERROR to logger. Writes to the returned log.Logger are written to the underlying slog.Logger as an attribute pair with the Key LKeyError and message msg. If any regular expressions are passed via debugREs, log messages written to the log.Logger which match any of the regular expressions will be logged at level DEBUG, not level ERROR. This is useful on internet-facing servers for not logging EOFs caused by banner-grabbing.

func FatalError

func FatalError(sl *slog.Logger, msg string, err error, args ...any)

FatalError wraps ErrorError and then calls os.Exit(1).

func InfoError

func InfoError(sl *slog.Logger, msg string, err error, args ...any)

InfoError is like ErrorError, with a level of info.

func NewJSONLogger

func NewJSONLogger(w io.Writer) (*slog.LevelVar, *slog.Logger)

NewJSONLogger returns a new Logger which wraps a slog.NewJSONHandler which writes to w. Only the LevelVar field is set in the slog.HandlerOptions passed to slog.NewJSONHandler.

func NewTestLogger

func NewTestLogger() (*slog.LevelVar, *bytes.Buffer, *slog.Logger)

NewTestLogger returns a new JSON slog.Logger wrapped in a Handler which logs to the returned bytes.Buffer. The logger's Level is set to DEBUG.

func RemoveTimestamp

func RemoveTimestamp(s string) string

RemoveTimestamp removes a timestamp from a log message. This is intended to be used in test functions. The returned string will not end in a newline.

Example
msg := `{"time":"2023-10-19T22:36:37.247404361+02:00",` +
	`"level":"ERROR","msg":"kittens"}`
fmt.Printf("%s\n", RemoveTimestamp(msg))
Output:

{"time":"","level":"ERROR","msg":"kittens"}

func SetDebugLogging

func SetDebugLogging(on bool)

SetDebugLogging enables or disables DEBUG-level logs.

func WarnError

func WarnError(sl *slog.Logger, msg string, err error, args ...any)

WarnError is like Error, with a level of warn.

Types

type AtomicString

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

AtomicString is like atomic.Value, but for a string. It implements slog.LogValuer.

func (*AtomicString) Load

func (a *AtomicString) Load() string

Load gets the stored string. It returns the empty string if no string has been stored.

func (*AtomicString) LogValue

func (a *AtomicString) LogValue() slog.Value

LogValue returns the string in a suitable form for slogging. This is handy for calling slog.Logger.With without needing to worry about someone changing the string in a.

func (*AtomicString) Store

func (a *AtomicString) Store(s string)

Store stores a new value in a.

func (*AtomicString) Swap

func (a *AtomicString) Swap(s string) (old string, hadOld bool)

Swap stores a new value in a. The old value is returned, along with a bool bool indicating if there was actually an old value.

type Handler

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

Handler is identical to the default slog handler except that it allows for a settable level and that it waits until Handle is called to resolve slog.Attrs supplied by WithAttrs. Handler's methods satisfy slog.Handler.

func NewHandler

func NewHandler(lv *slog.LevelVar, h slog.Handler) Handler

NewHandler returns a new Handler wrapping h whose level is set by lv.

func (Handler) Enabled

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

func (Handler) Handle

func (h Handler) Handle(ctx context.Context, rec 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