{"id":20148226,"url":"https://github.com/chainguard-dev/clog","last_synced_at":"2025-07-26T12:01:58.911Z","repository":{"id":211182196,"uuid":"728428105","full_name":"chainguard-dev/clog","owner":"chainguard-dev","description":"Context aware slog","archived":false,"fork":false,"pushed_at":"2025-03-03T23:21:06.000Z","size":277,"stargazers_count":22,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-22T00:05:28.031Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chainguard-dev.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}},"created_at":"2023-12-06T23:21:06.000Z","updated_at":"2025-03-03T23:20:46.000Z","dependencies_parsed_at":"2024-05-19T21:59:11.196Z","dependency_job_id":"1cf58d5e-20f2-4240-91da-171deb513c0e","html_url":"https://github.com/chainguard-dev/clog","commit_stats":null,"previous_names":["wlynch/slogctx","chainguard-dev/slogctx"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainguard-dev%2Fclog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainguard-dev%2Fclog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainguard-dev%2Fclog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainguard-dev%2Fclog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chainguard-dev","download_url":"https://codeload.github.com/chainguard-dev/clog/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103907,"owners_count":21048244,"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":[],"created_at":"2024-11-13T22:35:48.636Z","updated_at":"2025-04-09T19:51:25.612Z","avatar_url":"https://github.com/chainguard-dev.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 👞 clog\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/chainguard-dev/clog.svg)](https://pkg.go.dev/github.com/chainguard-dev/clog)\n\nContext-aware [`slog`](https://pkg.go.dev/log/slog)\n\n`slog` was added in Go 1.21, so using this requires Go 1.21 or later.\n\n## Usage\n\n### Context Logger\n\nThe context Logger can be used to use Loggers from the context. This is\nsometimes preferred over the [Context Handler](#context-handler), since this can\nmake it easier to use different loggers in different contexts (e.g. testing).\n\nThis approach is heavily inspired by\n[`knative.dev/pkg/logging`](https://pkg.go.dev/knative.dev/pkg/logging), but with [zero dependencies outside the standard library](https://github.com/chainguard-dev/clog/blob/main/go.mod) (compare with [`pkg/logging`'s deps](https://pkg.go.dev/knative.dev/pkg/logging?tab=imports)).\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"log/slog\"\n\n\t\"github.com/chainguard-dev/clog\"\n)\n\nfunc main() {\n\t// One-time setup\n\tlog := clog.New(slog.Default().Handler()).With(\"a\", \"b\")\n\tctx := clog.WithLogger(context.Background(), log)\n\n\tf(ctx)\n}\n\nfunc f(ctx context.Context) {\n\t// Grab logger from context and use.\n\tlog := clog.FromContext(ctx)\n\tlog.Info(\"in f\")\n\n\t// Add logging context and pass on.\n\tctx = clog.WithLogger(ctx, log.With(\"f\", \"hello\"))\n\tg(ctx)\n}\n\nfunc g(ctx context.Context) {\n\t// Grab logger from context and use.\n\tlog := clog.FromContext(ctx)\n\tlog.Info(\"in g\")\n\n\t// Package level context loggers are also aware\n\tclog.ErrorContext(ctx, \"asdf\")\n}\n\n```\n\n```sh\n$ go run .\n2009/11/10 23:00:00 INFO in f a=b\n2009/11/10 23:00:00 INFO in g a=b f=hello\n2009/11/10 23:00:00 ERROR asdf a=b f=hello\n```\n\n#### Testing\n\nThe `slogtest` package provides utilities to make it easy to create loggers that\nwill use the native testing logging.\n\n```go\nfunc TestFoo(t *testing.T) {\n\tctx := slogtest.TestContextWithLogger(t)\n\n\tfor _, tc := range []string{\"a\", \"b\"} {\n\t\tt.Run(tc, func(t *testing.T) {\n\t\t\tclog.FromContext(ctx).Infof(\"hello world\")\n\t\t})\n\t}\n}\n```\n\n```sh\n$ go test -v ./examples/logger\n=== RUN   TestLog\n=== RUN   TestLog/a\n=== NAME  TestLog\n    slogtest.go:20: time=2023-12-12T18:42:53.020-05:00 level=INFO msg=\"hello world\"\n\n=== RUN   TestLog/b\n=== NAME  TestLog\n    slogtest.go:20: time=2023-12-12T18:42:53.020-05:00 level=INFO msg=\"hello world\"\n\n--- PASS: TestLog (0.00s)\n    --- PASS: TestLog/a (0.00s)\n    --- PASS: TestLog/b (0.00s)\nPASS\nok      github.com/chainguard-dev/clog/examples/logger\n```\n\n### Context Handler\n\nThe context Handler can be used to insert values from the context.\n\n```go\nfunc init() {\n\tslog.SetDefault(slog.New(clog.NewHandler(slog.NewTextHandler(os.Stdout, nil))))\n}\n\nfunc main() {\n\tctx := context.Background()\n\tctx = clog.WithValues(ctx, \"foo\", \"bar\")\n\n\t// Use slog package directly\n\tslog.InfoContext(ctx, \"hello world\", slog.Bool(\"baz\", true))\n\n\t// glog / zap style (note: can't pass additional attributes)\n\tclog.ErrorContextf(ctx, \"hello %s\", \"world\")\n}\n```\n\n```sh\n$ go run .\ntime=2009-11-10T23:00:00.000Z level=INFO msg=\"hello world\" baz=true foo=bar\ntime=2009-11-10T23:00:00.000Z level=ERROR msg=\"hello world\" foo=bar\n```\n\n### Google Cloud Platform support\n\nThis package also provides a GCP-optimized JSON handler for structured logging and trace attribution.\n\nSee [`./gcp/README.md`](./gcp/README.md) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchainguard-dev%2Fclog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchainguard-dev%2Fclog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchainguard-dev%2Fclog/lists"}