Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aidenwallis/slogctx
Allows for passing extra fields to `slog` calls using context.
https://github.com/aidenwallis/slogctx
context golang logging slog
Last synced: about 5 hours ago
JSON representation
Allows for passing extra fields to `slog` calls using context.
- Host: GitHub
- URL: https://github.com/aidenwallis/slogctx
- Owner: aidenwallis
- License: mit
- Created: 2023-08-10T02:00:01.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-10T02:45:06.000Z (over 1 year ago)
- Last Synced: 2023-09-05T03:40:18.276Z (over 1 year ago)
- Topics: context, golang, logging, slog
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# slogctx
[![codecov](https://codecov.io/gh/aidenwallis/slogctx/branch/main/graph/badge.svg?token=CF9slb1Sjp)](https://codecov.io/gh/aidenwallis/slogctx) [![Go Reference](https://pkg.go.dev/badge/github.com/aidenwallis/slogctx.svg)](https://pkg.go.dev/github.com/aidenwallis/slogctx)
Simple [slog](https://pkg.go.dev/log/slog) wrapping handler that lets you pass fields down to a slog call through context.
## Example
```go
package mainimport (
"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.