| 1 | # 👞 clog |
| 2 | |
| 3 | [](https://pkg.go.dev/github.com/chainguard-dev/clog) |
| 4 | |
| 5 | Context-aware [`slog`](https://pkg.go.dev/log/slog) |
| 6 | |
| 7 | `slog` was added in Go 1.21, so using this requires Go 1.21 or later. |
| 8 | |
| 9 | ## Usage |
| 10 | |
| 11 | ### Context Logger |
| 12 | |
| 13 | The context Logger can be used to use Loggers from the context. This is |
| 14 | sometimes preferred over the [Context Handler](#context-handler), since this can |
| 15 | make it easier to use different loggers in different contexts (e.g. testing). |
| 16 | |
| 17 | This approach is heavily inspired by |
| 18 | [`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)). |
| 19 | |
| 20 | ```go |
| 21 | package main |
| 22 | |
| 23 | import ( |
| 24 | "context" |
| 25 | "log/slog" |
| 26 | |
| 27 | "github.com/chainguard-dev/clog" |
| 28 | ) |
| 29 | |
| 30 | func main() { |
| 31 | // One-time setup |
| 32 | log := clog.New(slog.Default().Handler()).With("a", "b") |
| 33 | ctx := clog.WithLogger(context.Background(), log) |
| 34 | |
| 35 | f(ctx) |
| 36 | } |
| 37 | |
| 38 | func f(ctx context.Context) { |
| 39 | // Grab logger from context and use. |
| 40 | log := clog.FromContext(ctx) |
| 41 | log.Info("in f") |
| 42 | |
| 43 | // Add logging context and pass on. |
| 44 | ctx = clog.WithLogger(ctx, log.With("f", "hello")) |
| 45 | g(ctx) |
| 46 | } |
| 47 | |
| 48 | func g(ctx context.Context) { |
| 49 | // Grab logger from context and use. |
| 50 | log := clog.FromContext(ctx) |
| 51 | log.Info("in g") |
| 52 | |
| 53 | // Package level context loggers are also aware |
| 54 | clog.ErrorContext(ctx, "asdf") |
| 55 | } |
| 56 | |
| 57 | ``` |
| 58 | |
| 59 | ```sh |
| 60 | $ go run . |
| 61 | 2009/11/10 23:00:00 INFO in f a=b |
| 62 | 2009/11/10 23:00:00 INFO in g a=b f=hello |
| 63 | 2009/11/10 23:00:00 ERROR asdf a=b f=hello |
| 64 | ``` |
| 65 | |
| 66 | #### Testing |
| 67 | |
| 68 | The `slogtest` package provides utilities to make it easy to create loggers that |
| 69 | will use the native testing logging. |
| 70 | |
| 71 | ```go |
| 72 | func TestFoo(t *testing.T) { |
| 73 | ctx := slogtest.TestContextWithLogger(t) |
| 74 | |
| 75 | for _, tc := range []string{"a", "b"} { |
| 76 | t.Run(tc, func(t *testing.T) { |
| 77 | clog.FromContext(ctx).Infof("hello world") |
| 78 | }) |
| 79 | } |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | ```sh |
| 84 | $ go test -v ./examples/logger |
| 85 | === RUN TestLog |
| 86 | === RUN TestLog/a |
| 87 | === NAME TestLog |
| 88 | slogtest.go:20: time=2023-12-12T18:42:53.020-05:00 level=INFO msg="hello world" |
| 89 | |
| 90 | === RUN TestLog/b |
| 91 | === NAME TestLog |
| 92 | slogtest.go:20: time=2023-12-12T18:42:53.020-05:00 level=INFO msg="hello world" |
| 93 | |
| 94 | --- PASS: TestLog (0.00s) |
| 95 | --- PASS: TestLog/a (0.00s) |
| 96 | --- PASS: TestLog/b (0.00s) |
| 97 | PASS |
| 98 | ok github.com/chainguard-dev/clog/examples/logger |
| 99 | ``` |
| 100 | |
| 101 | ### Context Handler |
| 102 | |
| 103 | The context Handler can be used to insert values from the context. |
| 104 | |
| 105 | ```go |
| 106 | func init() { |
| 107 | slog.SetDefault(slog.New(clog.NewHandler(slog.NewTextHandler(os.Stdout, nil)))) |
| 108 | } |
| 109 | |
| 110 | func main() { |
| 111 | ctx := context.Background() |
| 112 | ctx = clog.WithValues(ctx, "foo", "bar") |
| 113 | |
| 114 | // Use slog package directly |
| 115 | slog.InfoContext(ctx, "hello world", slog.Bool("baz", true)) |
| 116 | |
| 117 | // glog / zap style (note: can't pass additional attributes) |
| 118 | clog.ErrorContextf(ctx, "hello %s", "world") |
| 119 | } |
| 120 | ``` |
| 121 | |
| 122 | ```sh |
| 123 | $ go run . |
| 124 | time=2009-11-10T23:00:00.000Z level=INFO msg="hello world" baz=true foo=bar |
| 125 | time=2009-11-10T23:00:00.000Z level=ERROR msg="hello world" foo=bar |
| 126 | ``` |
| 127 | |
| 128 | ### Google Cloud Platform support |
| 129 | |
| 130 | This package also provides a GCP-optimized JSON handler for structured logging and trace attribution. |
| 131 | |
| 132 | See [`./gcp/README.md`](./gcp/README.md) for details. |