Documentation
¶
Overview ¶
Package slogging provides utilities to attach and retrieve slog.Logger instances from context.
It also provides slog.Value implementations for JSON and error (with stack trace) types.
Index ¶
- func AnError(key string, err error) slog.Attr
- func ErrorValue(err error) slog.Value
- func Get(ctx context.Context) *slog.Logger
- func GetWith(ctx context.Context, args ...any) (context.Context, *slog.Logger)
- func JSON(key string, data []byte) slog.Attr
- func JSONString(key, value string) slog.Attr
- func JSONStringValue(s string) slog.Value
- func JSONValue(data []byte) slog.Value
- func TryGet(ctx context.Context) (*slog.Logger, bool)
- func UpdateContext(ctx context.Context, fn func(logger *slog.Logger) *slog.Logger) (context.Context, *slog.Logger)
- func WithContext(ctx context.Context, logger *slog.Logger) context.Context
- func Wrapf(key string, err error, format string, a ...any) slog.Attr
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnError ¶
AnError is a slog.Any wrapper around ErrorValue.
eris.Wrap will be used to add the stack trace to the error. Use Wrapf if you'd like to customise the wrap message.
func ErrorValue ¶
ErrorValue returns a slog.Value for the given error.
eris.Wrap will be used to add the stack trace to the error. Use Wrapf if you'd like to customise the wrap message.
func Get ¶
Get retrieves the slog.Logger instance that was attached with WithContext.
If none is available, slog.Default is returned.
func GetWith ¶ added in v0.1.1
GetWith is a variant of Get that allows updating of the logger with attributes.
slog.Logger.With is used to update the logger. You should pass any number slog.Attr as the args.
The modified logger is returned along with the updated context.
func JSONString ¶
JSONString is a slog.Any wrapper around JSONStringValue.
func JSONStringValue ¶
JSONStringValue is a variant of JSONValue that receives a string instead.
If the raw bytes of the string argument is not valid JSON, or if the logger isn't using slog.JSONHandler, the default logic for printing string will be used.
func JSONValue ¶
JSONValue returns a slog.Value that will cause slog.JSONHandler to print the []byte data as-is if it is valid JSON.
If the logger isn't using slog.JSONHandler, or if data is not valid JSON, then the default logic for printing bytes is used, which should be printing its base64 standard encoding.
Useful if you want to log a response from a GET call for example that you expect to be JSON, but you don't want to parse it into valid JSON first. If you have already unmarshalled []byte into a struct, or if you have some type that implements json.Marshaler, you can just use slog.AnyValue directly on the struct.
func UpdateContext ¶
func UpdateContext(ctx context.Context, fn func(logger *slog.Logger) *slog.Logger) (context.Context, *slog.Logger)
UpdateContext is a variant of GetWith that receives a function to update the logger instead of attributes.
The logger returned by fn is attached to context and returned.
Useful if you need to update the logger with slog.Logger.WithGroup, for example, instead of attributes.
func WithContext ¶
WithContext attaches the given slog.Logger instance to the returned context.
Types ¶
This section is empty.