|
1 | 1 | # slog-context
|
2 |
| -Add keys and values to golang slog log lines. Have slog automatically read from context to add details. |
| 2 | +[](https://github.com/veqryn/slog-context/releases) |
| 3 | + |
| 4 | +[](https://pkg.go.dev/github.com/veqryn/slog-context) |
| 5 | + |
| 6 | +[](https://goreportcard.com/report/github.com/veqryn/slog-context) |
| 7 | +[](https://codecov.io/gh/veqryn/slog-context) |
| 8 | +[](https://github.com/veqryn/slog-context/graphs/contributors) |
| 9 | +[](./LICENSE) |
| 10 | + |
| 11 | +Use golang structured logging (slog) with context. |
| 12 | +Add attributes to context. Add and retrieve logger to and from context. |
| 13 | + |
| 14 | +This library supports two different workflows for using slog and context. |
| 15 | +These workflows can be used separately or together. |
| 16 | + |
| 17 | +Using the `slogcontext.Handler` lets us `Prepend` and `Append` attributes to |
| 18 | +log lines, even when a logger is not passed into a function or in code we don't |
| 19 | +control. This is done without storing the logger in the context; instead the |
| 20 | +attributes are stored in the context and the Handler picks them up later |
| 21 | +whenever a new log line is written. |
| 22 | + |
| 23 | +Using `ToCtx` and `Logger` lets us store the logger itself within a context, |
| 24 | +and get it back out again. Wrapper methods `With`/`WithGroup`/`Debug`/`Info`/ |
| 25 | +`Warn`/`Error`/`Log`/`LogAttrs` let us work directly with a logger residing |
| 26 | +with the context (or the default logger if no logger is stored in the context). |
| 27 | + |
| 28 | +## Install |
| 29 | +`go get github.com/veqryn/slog-context` |
| 30 | + |
| 31 | +```go |
| 32 | +import ( |
| 33 | + slogcontext "github.com/veqryn/slog-context" |
| 34 | +) |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | +### Add attributes to context workflow |
| 39 | +```go |
| 40 | +package main |
| 41 | + |
| 42 | +import ( |
| 43 | + "context" |
| 44 | + "log/slog" |
| 45 | + "os" |
| 46 | + |
| 47 | + slogcontext "github.com/veqryn/slog-context" |
| 48 | +) |
| 49 | + |
| 50 | +// This workflow lets us use slog as normal, while adding the ability to put |
| 51 | +// slog attributes into the context which will then show up at the start or end |
| 52 | +// of log lines. |
| 53 | +// |
| 54 | +// This is useful when you are not passing a *slog.Logger around to different |
| 55 | +// functions (because you are making use of the default package-level slog), |
| 56 | +// but you are passing a context.Context around. |
| 57 | +// |
| 58 | +// This can also be used when a library or vendor code you don't control is |
| 59 | +// using the default log methods, default logger, or doesn't accept a slog |
| 60 | +// Logger to all functions you wish to add attributes to. |
| 61 | +// |
| 62 | +// Attributes and key-value pairs like request-id, trace-id, user-id, etc, can |
| 63 | +// be added to the context, and the *slogcontext.Handler will make sure they |
| 64 | +// are prepended to the start, or appended to the end, of any log lines using |
| 65 | +// that context. |
| 66 | +func main() { |
| 67 | + // Create the *slogcontext.Handler middleware |
| 68 | + h := slogcontext.NewHandler(slog.NewJSONHandler(os.Stdout, nil), nil) |
| 69 | + slog.SetDefault(slog.New(h)) |
| 70 | + |
| 71 | + ctx := context.Background() |
| 72 | + |
| 73 | + // Prepend some slog attributes to the start of future log lines: |
| 74 | + ctx = slogcontext.Prepend(ctx, "prependKey", "prependValue") |
| 75 | + |
| 76 | + // Append some slog attributes to the end of future log lines: |
| 77 | + // Prepend and Append have the same args signature as slog methods, |
| 78 | + // and can take a mix of slog.Attr and key-value pairs. |
| 79 | + ctx = slogcontext.Append(ctx, slog.String("appendKey", "appendValue")) |
| 80 | + |
| 81 | + // Use the logger like normal: |
| 82 | + slog.WarnContext(ctx, "main message", "mainKey", "mainValue") |
| 83 | + /* |
| 84 | + { |
| 85 | + "time": "2023-11-15T18:43:23.290798-07:00", |
| 86 | + "level": "WARN", |
| 87 | + "msg": "main message", |
| 88 | + "prependKey": "prependValue", |
| 89 | + "mainKey": "mainValue", |
| 90 | + "appendKey": "appendValue" |
| 91 | + } |
| 92 | + */ |
| 93 | + |
| 94 | + // Use the logger like normal; add attributes, create groups, pass it around: |
| 95 | + log := slog.With("rootKey", "rootValue") |
| 96 | + log = log.WithGroup("someGroup") |
| 97 | + log = log.With("subKey", "subValue") |
| 98 | + |
| 99 | + // The prepended/appended attributes end up in all log lines that use that context |
| 100 | + log.InfoContext(ctx, "main message", "mainKey", "mainValue") |
| 101 | + /* |
| 102 | + { |
| 103 | + "time": "2023-11-14T00:37:03.805196-07:00", |
| 104 | + "level": "INFO", |
| 105 | + "msg": "main message", |
| 106 | + "prependKey": "prependValue", |
| 107 | + "rootKey": "rootValue", |
| 108 | + "someGroup": { |
| 109 | + "subKey": "subValue", |
| 110 | + "mainKey": "mainValue", |
| 111 | + "appendKey": "appendValue" |
| 112 | + } |
| 113 | + } |
| 114 | + */ |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### Add logger to context workflow |
| 119 | +```go |
| 120 | +package main |
| 121 | + |
| 122 | +import ( |
| 123 | + "context" |
| 124 | + "log/slog" |
| 125 | + "os" |
| 126 | + |
| 127 | + slogcontext "github.com/veqryn/slog-context" |
| 128 | +) |
| 129 | + |
| 130 | +// This workflow has us pass the *slog.Logger around inside a context.Context. |
| 131 | +// This lets us add attributes and groups to the logger, while naturally |
| 132 | +// keeping the logger scoped just like the context itself is scoped. |
| 133 | +// |
| 134 | +// This eliminates the need to use the default package-level slog, and also |
| 135 | +// eliminates the need to add a *slog.Logger as yet another argument to all |
| 136 | +// functions. |
| 137 | +// |
| 138 | +// You can still get the Logger out of the context at any time, and pass it |
| 139 | +// around manually if needed, but since contexts are already passed to most |
| 140 | +// functions, passing the logger explicitly is now optional. |
| 141 | +// |
| 142 | +// Attributes and key-value pairs like request-id, trace-id, user-id, etc, can |
| 143 | +// be added to the logger in the context, and as the context propagates the |
| 144 | +// logger and its attributes will propagate with it, adding these to any log |
| 145 | +// lines using that context. |
| 146 | +func main() { |
| 147 | + h := slog.NewJSONHandler(os.Stdout, nil) |
| 148 | + slog.SetDefault(slog.New(h)) |
| 149 | + |
| 150 | + // Store the logger inside the context: |
| 151 | + ctx := slogcontext.ToCtx(context.Background(), slog.Default()) |
| 152 | + |
| 153 | + // Get the logger back out again at any time, for manual usage: |
| 154 | + log := slogcontext.Logger(ctx) |
| 155 | + log.Warn("warning") |
| 156 | + |
| 157 | + // Add attributes directly to the logger in the context: |
| 158 | + ctx = slogcontext.With(ctx, "rootKey", "rootValue") |
| 159 | + |
| 160 | + // Create a group directly on the logger in the context: |
| 161 | + ctx = slogcontext.WithGroup(ctx, "someGroup") |
| 162 | + |
| 163 | + // With and wrapper methods have the same args signature as slog methods, |
| 164 | + // and can take a mix of slog.Attr and key-value pairs. |
| 165 | + ctx = slogcontext.With(ctx, slog.String("subKey", "subValue")) |
| 166 | + |
| 167 | + // Access the logger in the context directly with handy wrappers for Debug/Info/Warn/Error/Log/LogAttrs: |
| 168 | + slogcontext.Info(ctx, "main message", "mainKey", "mainValue") |
| 169 | + /* |
| 170 | + { |
| 171 | + "time":"2023-11-14T00:53:46.363072-07:00", |
| 172 | + "level":"INFO", |
| 173 | + "msg":"main message", |
| 174 | + "rootKey":"rootValue", |
| 175 | + "someGroup":{ |
| 176 | + "subKey":"subValue", |
| 177 | + "mainKey":"mainValue" |
| 178 | + } |
| 179 | + } |
| 180 | + */ |
| 181 | +} |
| 182 | +``` |
0 commit comments