-
-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zap example #1
Comments
Hi Mehmet, And thanks for this quick feedback! I'm doing a few iterations before the initial release and your feedback is appreciated. I have no experience with Zap. Do you know if we can create custom formatters for Zap? I made one for logrus. Output can be exported using |
I don't have much experience with zap custom formats but I did something like this, as I understand we need to write a custom encoder, I couldn't find an error formatter or settings for jsonEncoder or EncoderConfig. type myCustomEncoder struct {
zapcore.ObjectEncoder
}
func (f *myCustomEncoder) EncodeEntry(zapcore.Entry, []zap.Field) (*buffer.Buffer, error) {
return nil, nil
}
func (f *myCustomEncoder) Clone() zapcore.Encoder {
return nil
}
func main() {
zap.RegisterEncoder("myCustomEncoder", func(config zapcore.EncoderConfig) (zapcore.Encoder, error) {
return &myCustomEncoder{}, nil
})
const timeFormat string = "2006-01-02T15:04:05.999Z"
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "sourceLocation",
FunctionKey: zapcore.OmitKey,
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.TimeEncoderOfLayout(timeFormat),
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
config := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.InfoLevel),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "myCustomEncoder",
EncoderConfig: encoderConfig,
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
logger, _ := config.Build(zap.AddStacktrace(zap.FatalLevel))
err := oops.
In("repository").
Tags("database", "sql").
With("deneme", "mehmet").
Errorf("could not fetch user")
logger.Error(err.Error(), zap.Error(err))
} |
Zap supports only 1 encoder at a time ? |
Hi @samber,
I tried using oops with zap here is my example:
I couldn't achieve the log structure like your example in readme, would be great if I get errorVerbose as a json.
The text was updated successfully, but these errors were encountered: