https://github.com/fishy/ctxslog
Go library provides helpers around slog
https://github.com/fishy/ctxslog
Last synced: 3 months ago
JSON representation
Go library provides helpers around slog
- Host: GitHub
- URL: https://github.com/fishy/ctxslog
- Owner: fishy
- License: bsd-3-clause
- Created: 2023-05-07T05:55:07.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-03T00:58:27.000Z (about 1 year ago)
- Last Synced: 2025-03-20T21:13:52.767Z (3 months ago)
- Language: Go
- Size: 47.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/go.yhsif.com/ctxslog)
[](https://goreportcard.com/report/go.yhsif.com/ctxslog)# ctxslog
Go library provides helpers around slog
## Example
(See [package example on pkg.go.dev][package-example] for up-to-date example)
```go
package mainimport (
"context"
"log/slog"
"net/http"
"os""go.yhsif.com/ctxslog"
)func EndpointHandler(ctx context.Context, traceID string) {
// Inside an endpoint handler, attach trace id for all logs in this context
ctx = ctxslog.Attach(ctx, "trace", traceID)
// Now slog's global log functions with ctx will also have trace info
slog.ErrorContex(ctx, "Not implemented")thirdPartyLibCall := func(ctx context.Context) {
// Some third party library that uses slog and spams logs a lot.
slog.ErrorContext(ctx, "not really an error")
}
thirdPartyLibCall(
ctxslog.AttachLogLevel(ctx, ctxslog.MaxLevel), // now even if it logs at error level it won't shown
)
}func HttpHandler(w http.ResponseWriter, r *http.Request) {
ctx := ctxslog.Attach(
r.Context(),
// Add httpRequest group to every log within this context
slog.Group("httpRequest", ctxslog.HTTPRequest(r, ctxslog.RemoteAddrIP)),
)
// Enable callstack even at debug level for this context
ctx = ctxslog.AttachCallstackLevel(ctx, slog.LevelDebug)
slog.DebugContext(ctx, "foo") // this log will contain httpRequest group and callstack.
}func main() {
// Sets the global slog logger
slog.SetDefault(ctxslog.New(
ctxslog.WithWriter(os.Stderr), // This is the default and can be omitted
ctxslog.WithJSON, // This is the default, use ctxslog.WithText instead if you want non-json logs
ctxslog.WithAddSource(true), // Add source info
ctxslog.WithLevel(slog.LevelDebug), // Keep debug level logs
ctxslog.WithCallstack(slog.LevelError), // For error and above levels, also add callstack info
ctxslog.WithGlobalKVs("version", os.Getenv("VERSION_TAG")), // Add version info to every log
ctxslog.WithReplaceAttr(ctxslog.ChainReplaceAttr(
ctxslog.GCPKeys, // Use Google Cloud Structured logging friendly log keys
ctxslog.StringDuration, // Log time durations as strings
)),
))
// Now you can just use slog's global log functions
slog.Info("Hello, world!", "key", "value")
}
```[package-example]: https://pkg.go.dev/go.yhsif.com/ctxslog#example-package