-
Notifications
You must be signed in to change notification settings - Fork 72
Open
Description
Hey! Thank you for this awesome library!
I would like to include extra fields on my otelzap logger with every message. To do that I'm looking at the otelzap.WithExtraFields() config and also maybe using .With() in the sugared logger.
I'm finding the otelzap.WithExtraFields() config to be respected in the plain logger, but it doesn't look to be used in the sugared logger. Also, using With() in the sugared logger doesn't seem to work when chained with Ctx().
Here's some code to illustrate the above:
package main
import (
"context"
"log"
"github.com/uptrace/opentelemetry-go-extra/otelzap"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func main() {
// Create new Zap logger
var level zapcore.Level
err := level.UnmarshalText([]byte("INFO"))
if err != nil {
log.Fatal(err)
}
config := zap.NewProductionConfig()
config.Level = zap.NewAtomicLevelAt(level)
zapLogger, err := config.Build(zap.AddCallerSkip(0))
if err != nil {
log.Fatal(err)
}
// Create OTEL Zap logger
otelZapLogger := otelzap.New(
zapLogger,
otelzap.WithMinLevel(zapLogger.Level()),
otelzap.WithExtraFields(zap.String("extra_key", "extra_value")),
)
ctx := context.Background()
// Example scenarios
otelZapLogger.Ctx(ctx).Info("plain + ctx, DOES have extra fields")
otelZapLogger.Sugar().Ctx(ctx).Infof("sugared + ctx, DOES NOT have extra fields")
otelZapLogger.Sugar().With("with_key", "with_value").Infof("sugared + with, DOES have 'with' fields, DOES NOT have extra fields")
otelZapLogger.Sugar().With("with_key", "with_value").Ctx(ctx).Infof("sugared + with + ctx, DOES NOT have 'with' fields, DOES NOT have extra fields")
otelZapLogger.Sugar().With("with_key", "with_value").Ctx(ctx).Desugar().Info("desugared + with + ctx, DOES NOT have 'with' fields, DOES have extra fields")
}This produces the following output:
{"level":"info","ts":1721846112.587265,"caller":"test_logger/main.go:37","msg":"plain + ctx, DOES have extra fields","extra_key":"extra_value"}
{"level":"info","ts":1721846112.587362,"caller":"test_logger/main.go:39","msg":"sugared + ctx, DOES NOT have extra fields"}
{"level":"info","ts":1721846112.587384,"caller":"test_logger/main.go:41","msg":"sugared + with, DOES have 'with' fields, DOES NOT have extra fields","with_key":"with_value"}
{"level":"info","ts":1721846112.587411,"caller":"test_logger/main.go:43","msg":"sugared + with + ctx, DOES NOT have 'with' fields, DOES NOT have extra fields"}
{"level":"info","ts":1721846112.587422,"caller":"test_logger/main.go:45","msg":"desugared + with + ctx, DOES NOT have 'with' fields, DOES have extra fields","extra_key":"extra_value"}
Is this a bug, a limitation, or is there some other way to use the sugared logger?
Metadata
Metadata
Assignees
Labels
No labels