Skip to content

Allows for passing extra fields to `slog` calls using context.

License

Notifications You must be signed in to change notification settings

aidenwallis/slogctx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slogctx

codecov Go Reference

Simple slog wrapping handler that lets you pass fields down to a slog call through context.

Example

package main

import (
	"context"
	"log/slog"
	"os"

	"github.com/aidenwallis/slogctx"
)

var logger = slog.New(slogctx.NewHandler(slog.NewTextHandler(os.Stdout, nil)))

func main() {
	ctx := context.Background()

	logger.InfoContext(ctx, "this message has no extra fields tied to it!")

	ctx = slogctx.WithArgs(ctx, "passed_arg", "yes!")

	deeplyNestedFunc(ctx)
}

func deeplyNestedFunc(ctx context.Context) {
	logger.InfoContext(ctx, "this will attach the fields from above! particularly useful for passing through things like request URLs etc.")
}

Note that this now allows you to pass through fields using your context, rather than either deeply pushing a logger down the stack, and is compatible with the global slog functions.

About

Allows for passing extra fields to `slog` calls using context.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published