{"id":20068893,"url":"https://github.com/zknill/slogmw","last_synced_at":"2025-10-17T05:16:01.732Z","repository":{"id":187220449,"uuid":"676516284","full_name":"zknill/slogmw","owner":"zknill","description":"Middleware for the go standard library log/slog package","archived":false,"fork":false,"pushed_at":"2023-08-10T09:36:51.000Z","size":9,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2023-09-05T04:21:57.675Z","etag":null,"topics":["golang","json","logging","slog","structured-logging"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zknill.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-08-09T11:30:04.000Z","updated_at":"2023-08-25T20:25:44.000Z","dependencies_parsed_at":"2023-08-09T13:03:19.830Z","dependency_job_id":null,"html_url":"https://github.com/zknill/slogmw","commit_stats":null,"previous_names":["zknill/slogmw"],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zknill%2Fslogmw","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zknill%2Fslogmw/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zknill%2Fslogmw/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zknill%2Fslogmw/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zknill","download_url":"https://codeload.github.com/zknill/slogmw/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224461714,"owners_count":17315116,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","json","logging","slog","structured-logging"],"created_at":"2024-11-13T14:10:00.619Z","updated_at":"2025-10-17T05:15:56.713Z","avatar_url":"https://github.com/zknill.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go `log/slog` middleware\n\nThe `slogmw` package makes it easier to use the standard library `log/slog` package.\n\n- [`log/slog`](https://pkg.go.dev/log/slog)\n\nGo 1.21 includes a new structured logger, but it's hard to modify to your specific logging requirements. \nThis package provides middleware to make it easier to add, change, or edit fields of the standard library structured logger.\n\n\nThe slog package, and the slogmw package comes with two hooks:\n- `slog.HandlerOptions` has a `ReplaceAttr` function. We use `slogwm.FormatChain(...)` to create a chain of formatters that edit attributes using this hook.\n- `slog.Handler`. You can use `slowmw.WrapHandler(...)` to wrap a handler and edit the log events.\n\n## Examples\n\nFormat the time field of the log events: \n```go\nopts := \u0026slog.HandlerOptions{\n  // Create a chain of slog formatters\n  ReplaceAttr: slogmw.FormatChain(\n    // pass the key to edit \"slog.TimeKey\" and \n    // the date time format to use \"time.DateTime\"\n    slogmw.FormatTime(slog.TimeKey, time.DateTime)\n  ),\n}\n\nslog.SetDefault(slog.NewJSONHandler(os.Stdout, opts))\n```\n\nChange the key of a log field:\n```go\nopts := \u0026slog.HandlerOptions{\n  // Create a chain of slog formatters\n  ReplaceAttr: slogmw.FormatChain(\n    // The default log event message key is \"msg\"\n    // this is exported as the constant slog.MessageKey.\n    // Here we can change the msg key to \"log\". All\n    // log events will print out the log field as \"log\"\n    // and not \"msg\".\n    slogmw.FormatKey(slog.MessageKey, \"log\")\n  ),\n}\n\nslog.SetDefault(slog.NewJSONHandler(os.Stdout, opts))\nslog.Info(\"hello world\")\n\n// Output: {\"level\": \"INFO\", \"log\": \"hello world\" ...trimmed}\n```\n\nInclude a field from the `context.Context` in every log event:\n```go\n// create a function that can extract the\n// attributes from the context passed.\nctxValueFn := func(ctx context.Context) []slog.Attr {\n  v := ctx.Value(\"userid\").(string)\n  return []slog.Attr{slog.String(\"user_id\", v)}\n}\n\nh := slogmw.WrapHandler(\n  // Pass the built in JSONHandler to be wrapped\n  slog.NewJSONHandler(os.Stdout, opts),\n  // Pass the IncludeContext middleware to extract\n  // values from the context and include them as log fields.\n  slogmw.IncludeContext(ctxValueFn),\n)\n\nslog.SetDefault(h)\n\n// The context passed to InfoContext will be\n// available to the function in slogmw.IncludeContext. \n// If just slog.Info(...) is called, the context will\n// be context.Background()\nslog.InfoContext(ctx, \"hello world\")\n\n// Output: {\"level\": \"INFO\", \"msg\": \"hello world\", \"user_id\": \"...\"}\n```\n\nInclude a static field in every log event:\n```go\nh := slogmw.WrapHandler(\n  // Pass the built in JSONHandler to be wrapped\n  slog.NewJSONHandler(os.Stdout, opts),\n  // Include env=prod in every log event\n  slogmw.IncludeStatic(slog.String(\"env\", \"prod\"))\n)\n\nslog.SetDefault(h)\n\nslog.Info(\"hello world\")\n// Output: {\"level\": \"INFO\", \"msg\": \"hello world\", \"env\": \"prod\" .. trimmed}\n```\n\nMore example usage can be found in the [`middleware_test.go`](https://github.com/zknill/slogmw/blob/main/middleware_test.go) file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzknill%2Fslogmw","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzknill%2Fslogmw","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzknill%2Fslogmw/lists"}