diff options
| author | Jonathan Amsterdam <jba@google.com> | 2023-01-11 14:58:50 -0500 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2023-01-13 15:24:39 +0000 |
| commit | 42884c8a7b57132672fbaf14ac54cec2e431157b (patch) | |
| tree | 4039681db220486d9c77ef470118393589a004d7 | |
| parent | f88ca1878ecb385e7f2338858435270ce0b268ea (diff) | |
| download | go-x-proposal-42884c8a7b57132672fbaf14ac54cec2e431157b.tar.xz | |
design/56345-structured-logging.md: remove NewContext, FromContext and Ctx
Since there is a lot of pushback on this feature, remove it from this proposal.
We will create a separate proposal later.
Change-Id: Ic6ddaa28007c11b2444125b5da1039c2d37c0f5e
Reviewed-on: https://go-review.googlesource.com/c/proposal/+/461941
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
| -rw-r--r-- | design/56345-structured-logging.md | 66 |
1 files changed, 3 insertions, 63 deletions
diff --git a/design/56345-structured-logging.md b/design/56345-structured-logging.md index 9a0bde1..efe35d3 100644 --- a/design/56345-structured-logging.md +++ b/design/56345-structured-logging.md @@ -718,45 +718,9 @@ func (l *Logger) WithGroup(name string) *Logger attributes added to the Logger will be qualified by the given name. ``` -### Context Support +### Contexts in Loggers -#### Loggers in contexts - -Passing a logger in a `context.Context` is a common practice and one way to -include dynamically scoped information in log messages. - -The `slog` package has two functions to support this pattern. - -``` -func FromContext(ctx context.Context) *Logger - FromContext returns the Logger stored in ctx by NewContext, or the default - Logger if there is none. - -func NewContext(ctx context.Context, l Logger) context.Context - NewContext returns a context that contains the given Logger. Use FromContext - to retrieve the Logger. -``` - -As an example, an HTTP server might want to create a new `Logger` for each -request. The logger would contain request-wide attributes and be stored in the -context for the request. - -``` -func handle(w http.ResponseWriter, r *http.Request) { - rlogger := slog.FromContext(r.Context()).With( - "method", r.Method, - "url", r.URL, - "traceID", getTraceID(r), - ) - ctx := slog.NewContext(r.Context(), rlogger) - // ... use slog.FromContext(ctx) ... -} -``` - -#### Contexts in Loggers - -Putting a new `Logger` into a context each time a value is added to the context -complicates the code, and is easy to forget to do. +Handlers sometimes need to retrieve values from a context. Tracing spans are a prime example. In tracing packages like the one provided by [Open Telemetry](https://opentelemetry.io), spans can be created at any point in the @@ -766,18 +730,12 @@ code with ctx, span := tracer.Start(ctx, name, opts) ``` -It is too much to require programmers to then write -``` -ctx = slog.NewContext(slog.FromContext(ctx)) -``` -so that subsequent logging has access to the span ID. -So we provide the `Logger.WithContext` method to convey a context to a +We provide the `Logger.WithContext` method to convey a context to a `Handler`. The method returns a new `Logger` that can be stored, or immediately used to produce log output. A `Logger`'s context is available to a `Handler.Handle` method in the `Record.Context` field. -We also provide a convenient shorthand, `slog.Ctx`. ``` func (l *Logger) WithContext(ctx context.Context) *Logger @@ -786,11 +744,6 @@ func (l *Logger) WithContext(ctx context.Context) *Logger func (l *Logger) Context() context.Context Context returns l's context. - -func Ctx(ctx context.Context) *Logger - Ctx retrieves a Logger from the given context using FromContext. Then it - adds the given context to the Logger using WithContext and returns the - result. ``` ### Levels @@ -1021,10 +974,6 @@ func Log(level Level, msg string, args ...any) func LogAttrs(level Level, msg string, attrs ...Attr) LogAttrs calls Logger.LogAttrs on the default logger. -func NewContext(ctx context.Context, l *Logger) context.Context - NewContext returns a context that contains the given Logger. Use FromContext - to retrieve the Logger. - func SetDefault(l *Logger) SetDefault makes l the default Logger. After this call, output from the log package's default Logger (as with log.Print, etc.) will be logged at @@ -1328,18 +1277,9 @@ type Logger struct { To create a new Logger, call New or a Logger method that begins "With". -func Ctx(ctx context.Context) *Logger - Ctx retrieves a Logger from the given context using FromContext. Then it - adds the given context to the Logger using WithContext and returns the - result. - func Default() *Logger Default returns the default Logger. -func FromContext(ctx context.Context) *Logger - FromContext returns the Logger stored in ctx by NewContext, or the default - Logger if there is none. - func New(h Handler) *Logger New creates a new Logger with the given non-nil Handler and a nil context. |
