Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Example ¶
package main import ( "context" "log/slog" "os" "github.com/felttrip/ctxslog" ) type ComplexData struct { IntField int StrField string BoolField bool SliceField []string } func main() { slog.SetDefault(slog.New(ctxslog.NewHandler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { // Remove time from the output for predictable test output. if a.Key == slog.TimeKey { return slog.Attr{} } return a }, })))) ctx := ctxslog.WithValue(context.Background(), "AccountID", 123456789) ctx = ctxslog.WithValue(ctx, "email", "noone@felttrip.com") ctx = ctxslog.WithValue(ctx, "sender", "greg@BailysInAShoe.lake") slog.InfoContext(ctx, "Info With Context") ctx = ctxslog.WithValues(context.Background(), map[string]interface{}{ "AccountID": 987654321, "email": "bob@TheBuilder.fake", "complexData": ComplexData{ IntField: 123, StrField: "DEADBEEF", BoolField: true, SliceField: []string{"one", "two", "three"}, }, }) slog.ErrorContext(ctx, "Error With Context") }
Output: {"level":"INFO","msg":"Info With Context","AccountID":123456789,"email":"noone@felttrip.com","sender":"greg@BailysInAShoe.lake"} {"level":"ERROR","msg":"Error With Context","AccountID":987654321,"email":"bob@TheBuilder.fake","complexData":{"IntField":123,"StrField":"DEADBEEF","BoolField":true,"SliceField":["one","two","three"]}}
Click to show internal directories.
Click to hide internal directories.