-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add strict fields into log #876
Comments
This isn't possible for reflected objects (as the encoder interface takes the object to reflect and decides how to encode it, not zap, and the encoder wraps the fields in an object), but it is possible if:
Here's a small example showing it in action: type request struct {
URL string
IP string
}
func (r request) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("url", r.URL)
enc.AddString("ip", r.IP)
return nil
}
func ExampleObject() {
logger := zap.NewExample()
defer logger.Sync()
req := &request{"/test", "127.0.0.1"}
logger.Info("new request, in nested object", zap.Object("req", req))
logger.Info("new request, inline", zap.InlineObject(req))
// Output:
// {"level":"info","msg":"new request, in nested object","req":{"url":"/test","ip":"127.0.0.1"}}
// {"level":"info","msg":"new request, inline","url":"/test","ip":"127.0.0.1"}
} If this seems useful, I can add tests and make a PR out of it. cc @abhinav Does this seem generally useful? |
This can be useful if applications need to have strict custom logging schema. |
Fixes #876 Currently, a `zap.Field` can only represent a single key-value. Add `zap.InlineObject` so to allow adding multiple fields to the current namespace from a type implementing `zap.ObjectMarshaler`. This also solves a more general problem: a single `zap.Field` can now be used to add multiple key/value pairs.
Fixes #876 Currently, a `zap.Field` can only represent a single key-value. Add `zap.InlineObject` so to allow adding multiple fields to the current namespace from a type implementing `zap.ObjectMarshaler`. This also solves a more general problem: a single `zap.Field` can now be used to add multiple key/value pairs.
Fixes #876 Currently, a `zap.Field` can only represent a single key-value. Add `zap.Inline` to allow adding multiple fields to the current namespace from a type implementing `zap.ObjectMarshaler`. This also solves a more general problem: a single `zap.Field` can now be used to add multiple key/value pairs.
This is now possible using |
Fixes #876 Currently, a `zap.Field` can only represent a single key-value. Add `zap.Inline` to allow adding multiple fields to the current namespace from a type implementing `zap.ObjectMarshaler`. This also solves a more general problem: a single `zap.Field` can now be used to add multiple key/value pairs.
Fixes uber-go#876 Currently, a `zap.Field` can only represent a single key-value. Add `zap.Inline` to allow adding multiple fields to the current namespace from a type implementing `zap.ObjectMarshaler`. This also solves a more general problem: a single `zap.Field` can now be used to add multiple key/value pairs.
Hey there,
Let's say if I have a struct
if there a way add it to zap logger in flat without sub field?
I was able easy to do this:
with
sugar.Infow("", zap.Reflect("log", log))
but would be nice to construct something like this instead without using individual zap fields
Thanks
The text was updated successfully, but these errors were encountered: