lg

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package lg contains utility functions for working with slog. It implements the slog.NewContext and slog.FromContext funcs that have recently been zapped from the slog proposal. I think you had it right the first time, Go team. Hopefully this package is short-lived and those funcs are put back.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Depth added in v0.47.0

func Depth(log *slog.Logger, level slog.Level, depth int, msg string, args ...any)

Depth logs a message with the given call (pc skip) depth. This is useful for logging inside a helper function.

func Discard

func Discard() *slog.Logger

Discard returns a new *slog.Logger that discards output.

func From added in v0.48.0

func From(c Contexter) *slog.Logger

From returns the Logger stored in c's context by NewContext, or the Discard logger if there is none. It is a companion to FromContext.

func FromContext

func FromContext(ctx context.Context) *slog.Logger

FromContext returns the Logger stored in ctx by NewContext, or the Discard logger if there is none.

func IsDiscard added in v0.47.3

func IsDiscard(log *slog.Logger) bool

IsDiscard returns true if log was returned by lg.Discard.

func NewContext

func NewContext(ctx context.Context, l *slog.Logger) context.Context

NewContext returns a context that contains the given Logger. Use FromContext to retrieve the Logger.

func Unexpected

func Unexpected(log *slog.Logger, err error)

Unexpected is a convenience function for logging unexpected errors for which there may not be any useful context message.

func WarnIfCloseError

func WarnIfCloseError(log *slog.Logger, msg string, c io.Closer)

WarnIfCloseError executes c.Close if is non-nil, and logs a warning if c.Close returns an error.

func WarnIfError

func WarnIfError(log *slog.Logger, msg string, err error)

WarnIfError logs a warning if err is non-nil.

func WarnIfFuncError

func WarnIfFuncError(log *slog.Logger, msg string, fn func() error)

WarnIfFuncError executes fn (if non-nil), and logs a warning if fn returns an error.

Types

type Contexter added in v0.48.0

type Contexter interface {
	Context() context.Context
}

Contexter is an interface for types that can return a context.Context.

Directories

Path Synopsis
Package devlog contains a custom slog.Handler for developer-friendly log output.
Package devlog contains a custom slog.Handler for developer-friendly log output.
Package lga ("log attribute") holds constants for log attribute names.
Package lga ("log attribute") holds constants for log attribute names.
Package lgm ("log message") contains constants for log messages.
Package lgm ("log message") contains constants for log messages.
Package lgt provides a mechanism for getting a *slog.Logger that outputs to testing.T. See lgt.New.
Package lgt provides a mechanism for getting a *slog.Logger that outputs to testing.T. See lgt.New.
Package slogbuf implements a Buffer that stores log records that can later be replayed on a slog.Handler.
Package slogbuf implements a Buffer that stores log records that can later be replayed on a slog.Handler.
Package userlogdir has a single function, UserLogDir, that returns an OS-specific path for storing user logs.
Package userlogdir has a single function, UserLogDir, that returns an OS-specific path for storing user logs.

Jump to

Keyboard shortcuts

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