### Install slog-zap Go Library Source: https://github.com/samber/slog-zap/blob/main/README.md This command installs the `slog-zap` Go library, which provides a `slog.Handler` for `uber-go/zap`. It fetches the module and its dependencies, making it available for use in Go projects. ```Shell go get github.com/samber/slog-zap/v2 ``` -------------------------------- ### Basic slog-zap Logger Initialization and Usage Example in Go Source: https://github.com/samber/slog-zap/blob/main/README.md Demonstrates how to initialize a `slog` logger using `slog-zap` with a `zap.Logger`, add global attributes to the logger, and log messages at different levels. It includes examples of structured error logging with multiple attributes and info logging with grouped attributes. ```go import ( slogzap "github.com/samber/slog-zap/v2" "go.uber.org/zap" "log/slog" ) func main() { zapLogger, _ := zap.NewProduction() logger := slog.New(slogzap.Option{Level: slog.LevelDebug, Logger: zapLogger}.NewZapHandler()) logger = logger. With("environment", "dev"). With("release", "v1.0.0") // log error logger. With("category", "sql"). With("query.statement", "SELECT COUNT(*) FROM users;"). With("query.duration", 1*time.Second). With("error", fmt.Errorf("could not count users")). Error("caramba!") // log user signup logger. With( slog.Group("user", slog.String("id", "user-123"), slog.Time("created_at", time.Now()), ), ). Info("user registration") } ``` -------------------------------- ### Integrate OpenTelemetry Tracing with slog-zap in Go Source: https://github.com/samber/slog-zap/blob/main/README.md Illustrates how to integrate OpenTelemetry tracing with `slog-zap` by configuring a `TracerProvider` and starting a span. It shows how to extract trace and span IDs from the OpenTelemetry context and include them as `slog` attributes using the `slog-otel` library, ensuring trace context propagation in logs. ```go import ( slogzap "github.com/samber/slog-zap" slogotel "github.com/samber/slog-otel" "go.opentelemetry.io/otel/sdk/trace" ) func main() { tp := trace.NewTracerProvider( trace.WithSampler(trace.AlwaysSample()), ) tracer := tp.Tracer("hello/world") ctx, span := tracer.Start(context.Background(), "foo") defer span.End() span.AddEvent("bar") logger := slog.New( slogzap.Option{ // ... AttrFromContext: []func(ctx context.Context) []slog.Attr{ slogotel.ExtractOtelAttrFromContext([]string{"tracing"}, "trace_id", "span_id"), }, }.NewZapHandler(), ) logger.ErrorContext(ctx, "a message") } ``` -------------------------------- ### Development and Testing Commands for slog-zap Source: https://github.com/samber/slog-zap/blob/main/README.md Provides essential `make` commands for developers to set up the development environment, run unit tests, and continuously watch for changes while testing the `slog-zap` project. These commands streamline the development workflow for contributors. ```bash # Install some dev dependencies make tools # Run tests make test # or make watch-test ``` -------------------------------- ### Configure Global slog-zap Parameters in Go Source: https://github.com/samber/slog-zap/blob/main/README.md Sets global parameters for the `slog-zap` library, including the key used for source information in log output, a list of string keys that should be treated as errors, and a mapping between `slog.Level` and `zapcore.Level` for log level translation. ```go slogzap.SourceKey = "source" slogzap.ErrorKeys = []string{"error", "err"} slogzap.LogLevels = map[slog.Level]zapcore.Level{...} ``` -------------------------------- ### Define slog-zap Handler Options in Go Source: https://github.com/samber/slog-zap/blob/main/README.md Defines the configuration options for the `slog-zap` handler, allowing customization of the log level, underlying Zap logger instance, attribute conversion logic, context-based attribute extraction, and standard `slog.HandlerOptions` like source addition and attribute replacement. ```go type Option struct { // log level (default: debug) Level slog.Leveler // optional: zap logger (default: zap.L()) Logger *zap.Logger // optional: customize json payload builder Converter Converter // optional: fetch attributes from context AttrFromContext []func(ctx context.Context) []slog.Attr // optional: see slog.HandlerOptions AddSource bool ReplaceAttr func(groups []string, a slog.Attr) slog.Attr } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.