You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"
)
funcmain() {
// Create new Zap loggervarlevel zapcore.Levelerr:=level.UnmarshalText([]byte("INFO"))
iferr!=nil {
log.Fatal(err)
}
config:=zap.NewProductionConfig()
config.Level=zap.NewAtomicLevelAt(level)
zapLogger, err:=config.Build(zap.AddCallerSkip(0))
iferr!=nil {
log.Fatal(err)
}
// Create OTEL Zap loggerotelZapLogger:=otelzap.New(
zapLogger,
otelzap.WithMinLevel(zapLogger.Level()),
otelzap.WithExtraFields(zap.String("extra_key", "extra_value")),
)
ctx:=context.Background()
// Example scenariosotelZapLogger.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?
The text was updated successfully, but these errors were encountered:
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 theotelzap.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, usingWith()
in the sugared logger doesn't seem to work when chained withCtx()
.Here's some code to illustrate the above:
This produces the following output:
Is this a bug, a limitation, or is there some other way to use the sugared logger?
The text was updated successfully, but these errors were encountered: