slogging

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 5 Imported by: 1

README

log/slog goodies

Go Reference

Get with:

go get github.com/nguyengg/go-aws-commons/slogging

Usage:

package main

import (
	"context"
	"errors"
	"io"
	"log/slog"
	"net/http"
	"os"

	. "github.com/nguyengg/go-aws-commons/must"
	"github.com/nguyengg/go-aws-commons/slogging"
)

func main() {
	var (
		ctx = context.Background()
		err = errors.New("this is an error")
	)

	logger := slog.New(slog.NewJSONHandler(os.Stderr, nil))

	// this will log the error as JSON with stack trace.
	logger.LogAttrs(ctx, slog.LevelError, "to err or not to err", slogging.AnError("error", err))

	// you can also provide additional context to the error.
	logger.LogAttrs(ctx, slog.LevelError, "to err or not to err", slogging.Wrapf("error", err, "get s3://%s/%s error", "my-bucket", "my-key"))

	// if you have []byte or string that you believe are valid JSON (such as a GET response), you can do this:
	res := Must(http.Get("https://some.website.com/file.json"))
	defer res.Body.Close()
	data := Must(io.ReadAll(res.Body))
	logger.LogAttrs(ctx, slog.LevelInfo, "i got some json", slogging.JSON("payload", data))
}

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnError

func AnError(key string, err error) slog.Attr

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

func ErrorValue(err error) slog.Value

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

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

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

func GetWith(ctx context.Context, args ...any) (context.Context, *slog.Logger)

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 JSON

func JSON(key string, data []byte) slog.Attr

JSON is a slog.Any wrapper around JSONValue.

func JSONString

func JSONString(key, value string) slog.Attr

JSONString is a slog.Any wrapper around JSONStringValue.

func JSONStringValue

func JSONStringValue(s string) slog.Value

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

func JSONValue(data []byte) slog.Value

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 TryGet

func TryGet(ctx context.Context) (*slog.Logger, bool)

TryGet is a variant of Get that returns (nil, false) if no instance was attached.

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

func WithContext(ctx context.Context, logger *slog.Logger) context.Context

WithContext attaches the given slog.Logger instance to the returned context.

func Wrapf

func Wrapf(key string, err error, format string, a ...any) slog.Attr

Wrapf returns a slog.Any wrapper for the given key and error that will be wrapped using eris.Wrapf.

Types

This section is empty.

Jump to

Keyboard shortcuts

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