{"id":42034436,"url":"https://github.com/nickbryan/slogutil","last_synced_at":"2026-01-26T05:02:59.370Z","repository":{"id":266156845,"uuid":"893356276","full_name":"nickbryan/slogutil","owner":"nickbryan","description":"A utility library for Go's log/slog package that provides a context aware and in-memory handler whilst handling deduplication of attributes.","archived":false,"fork":false,"pushed_at":"2025-11-20T19:35:20.000Z","size":76,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-20T21:15:43.474Z","etag":null,"topics":["go","golang","golang-library","golang-package","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/nickbryan.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-11-24T08:09:57.000Z","updated_at":"2025-11-20T19:30:48.000Z","dependencies_parsed_at":"2025-01-13T13:29:41.237Z","dependency_job_id":"36e6cb17-5fb5-4ed7-b4b0-117f83a28a9c","html_url":"https://github.com/nickbryan/slogutil","commit_stats":null,"previous_names":["nickbryan/slogutil"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/nickbryan/slogutil","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickbryan%2Fslogutil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickbryan%2Fslogutil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickbryan%2Fslogutil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickbryan%2Fslogutil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nickbryan","download_url":"https://codeload.github.com/nickbryan/slogutil/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nickbryan%2Fslogutil/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28767014,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-26T03:54:34.369Z","status":"ssl_error","status_checked_at":"2026-01-26T03:54:33.031Z","response_time":59,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["go","golang","golang-library","golang-package","logging"],"created_at":"2026-01-26T05:02:57.868Z","updated_at":"2026-01-26T05:02:59.347Z","avatar_url":"https://github.com/nickbryan.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# slogutil\nPackage `slogutil` provides enhanced logging capabilities for the standard library `log/slog` package, focusing on\ncontext integration and testability.\n\n\u003cdiv align=\"center\"\u003e\n\n[![Test](https://github.com/nickbryan/slogutil/actions/workflows/test.yml/badge.svg)](https://github.com/nickbryan/slogutil/actions)\n[![Coverage](https://raw.githubusercontent.com/nickbryan/slogutil/badges/.badges/main/coverage.svg)](https://github.com/nickbryan/slogutil/actions)\n[![Go Report Card](https://goreportcard.com/badge/nickbryan/slogutil)](https://goreportcard.com/report/nickbryan/slogutil)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nickbryan/slogutil/blob/master/LICENSE)\n\n\u003c/div\u003e\n\n## Features\n* **Context-Aware Logging:**  Enriches log records with contextual information from `context.Context`:\n    * The `slogctx` sub-package provides a handler (`slogctx.Handler`) and an `Extractor` API to extract values from the context.\n    * Supports adding attributes to the root of the log context or appending them within the current log group.\n\n* **Testability:** Enables easy testing of log output:\n    * Provides an in-memory handler (`slogmem`) to capture log records during tests, allowing for assertions and verification.\n\n* **Attribute Consistency:** Provides consistent handling of log attributes:\n    * Deduplicates attributes with the same respecting groups. For example: `duplicate`, `duplicate#01`, `duplcate#02`.\n\n## Quick Start\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"log/slog\"\n\n\t\"github.com/nickbryan/slogutil\"\n\t\"github.com/nickbryan/slogutil/slogctx\"\n)\n\nfunc main() {\n\tctx := slogctx.WithAttrs(context.Background(), slog.String(\"my_appended_attribute\", \"my_appended_value\"))\n\tctx = slogctx.WithRootAttrs(ctx, slog.String(\"my_root_attribute\", \"my_root_value\"))\n\n\tlogger := slogutil.NewJSONLogger().WithGroup(\"my_group\")\n\tlogger.InfoContext(ctx, \"Info log message\", slog.String(\"my_attribute\", \"my_value\"))\n\n\t// Output:\n\t// {\"time\":\"2024-11-29T10:27:41.460372Z\",\"level\":\"INFO\",\"source\":{\"function\":\"main.main\",\"file\":main.go\",\"line\":16},\"msg\":\"Info log message\",\"my_root_attribute\":\"my_root_value\",\"my_group\":{\"my_attribute\":\"my_value\",\"my_appended_attribute\":\"my_appended_value\"}}\n}\n```\n## Testing\n`slogutil` provides an in-memory handler (`slogmem`) to capture log records, enabling you to test log output effectively:\n\n```go\nfunc TestThing(t *testing.T) {\n\tctx := slogctx.WithPrependAttrs(context.Background(), slog.String(\"prepend_attribute\", \"prepend_value\"))\n\tctx = slogctx.WithAppendAttrs(ctx, slog.String(\"append_attribute\", \"append_value\"))\n\n\tlogger, logs := slogutil.NewInMemoryLogger(slog.LevelDebug)\n\tlogger = logger.With(slog.Int(\"my_root_attribute\", 123))\n\tlogger = logger.WithGroup(\"my_group\")\n\n\tlogger.InfoContext(ctx, \"Info log message\", slog.String(\"my_grouped_attribute\", \"my_value\"))\n\n\tif ok, diff := logs.Contains(slogmem.RecordQuery{\n\t\tLevel:   slog.LevelInfo,\n\t\tMessage: \"Info log message\",\n\t\tAttrs: map[string]slog.Value{\n\t\t\t\"prepend_attribute\":             slog.StringValue(\"prepend_value\"),\n\t\t\t\"my_root_attribute\":             slog.IntValue(123),\n\t\t\t\"my_group.my_grouped_attribute\": slog.StringValue(\"my_value\"),\n\t\t\t\"my_group.append_attribute\":     slog.StringValue(\"append_value\"),\n\t\t},\n\t}); !ok {\n\t\tt.Errorf(\"expected log not written, got: %s\", diff)\n\t}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickbryan%2Fslogutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnickbryan%2Fslogutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnickbryan%2Fslogutil/lists"}