Go library provides helpers around slog
Find a file
Yuxuan 'fishy' Wang e0a5c21c55 Remove all side-effects
Remove init function, and also remove the call to slog.SetDefault in
New.
2024-06-02 17:57:31 -07:00
slogtest Add slogtest package 2023-11-18 17:29:28 -08:00
attr.go Add StringInt 2023-08-05 15:55:25 -07:00
doc.go Initial version 2023-05-06 22:56:34 -07:00
doc_test.go Remove all side-effects 2024-06-02 17:57:31 -07:00
go.mod Switch to stdlib log/slog and target go 1.21+ 2023-06-22 22:05:34 -07:00
handler.go Remove all side-effects 2024-06-02 17:57:31 -07:00
handler_test.go Remove all side-effects 2024-06-02 17:57:31 -07:00
http.go GCPRemoteIP: Don't spam logs when x-forwarded-for is empty 2024-05-26 16:13:27 -07:00
http_test.go Add test for GCPRealIP 2023-05-06 23:38:44 -07:00
LICENSE Initial commit 2023-05-06 22:55:08 -07:00
logger.go Remove all side-effects 2024-06-02 17:57:31 -07:00
README.md Remove all side-effects 2024-06-02 17:57:31 -07:00

PkgGoDev Go Report Card

ctxslog

Go library provides helpers around slog

Example

(See package example on pkg.go.dev for up-to-date example)

package main

import (
	"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")
}