使用带上下文的 golang 结构化日志 (slog)。将日志器添加到上下文中并从中检索。向上下文添加属性。自动读取任何自定义上下文
使用带上下文的 golang 结构化日志 (slog)。将日志器添加到上下文中并从中检索。向上下文添加属性。自动读取任何自定义上下文
Use golang structured logging (slog) with context. Add and retrieve logger to and from context. Add attributes to context. Automatically read any custom context values, such as OpenTelemetry TraceID.
This library supports two different workflows for using slog and context. These workflows can be used individually or together at the same time.
Using the Handler lets us Prepend and Append attributes to
log lines, even when a logger is not passed into a function or in code we don't
control. This is done without storing the logger in the context; instead the
attributes are stored in the context and the Handler picks them up later
whenever a new log line is written.
In that same workflow, the HandlerOptions and AttrExtractor types let us
extract any custom values from a context and have them automatically be
prepended or appended to all log lines using that context. By default, there are
extractors for anything added via Prepend and Append, but this repository
contains some optional Extractors that can be added:
slogotel.ExtractTraceSpanID extractor will automatically extract the OTEL
(OpenTelemetry) TraceID and SpanID, and add them to the log record, while also
annotating the Span with an error code if the log is at error level.sloghttp.ExtractAttrCollection extractor will automatically add to the log
record any attributes added by sloghttp.With after the sloghttp.AttrCollection
http middleware. This allows other middlewares to log with attributes that would
normally be out of scope, because they were added by a later middleware or the
final http handler in the chain.Using NewCtx and FromCtx lets us store the logger itself within a context,
and get it back out again. Wrapper methods With/WithGroup/Debug/Info/
Warn/Error/Log/LogAttrs let us work directly with a logger residing
with the context (or the default logger if no logger is stored in the context).
slog-context is compatible with both standard library slog and with logr, which is an alternative logging api/interface/frontend.
If only slog is used, only *slog.Logger's will be stored in the context.
If both slog and logr are used, *slog.Logger will be automatically converted
to a logr.Logger as needed, and vice versa. This allows full interoperability
down the stack and with any libraries that use either slog-context or logr.
go get github.com/veqryn/slog-context
import (
slogctx "github.com/veqryn/slog-context"
)
…
…
…
In order to avoid making all users of this repo require all the OTEL libraries, the OTEL extractor is in a separate module in this repo:
go get github.com/veqryn/slog-context/otel
…
…
…
go import "github.com/veqryn/slog-context" var h = slogcontext.NewHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
To this:
```go
import "github.com/veqryn/slog-context"
var h = slogctx.NewHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
Named imports are unaffected.
暂无开放 Issues,或尚未同步最近议题。