Err adds the field "error" with serialized err to the *Event context. If err is nil, no field is added. To customize the key name, change zerolog.ErrorFieldName. If Stack() has been called before and zerolog.ErrorStackMarshaler is defined, the err is passed to ErrorStackMarshaler and the result is
(err error)
| 459 | // the err is passed to ErrorStackMarshaler and the result is appended to the |
| 460 | // zerolog.ErrorStackFieldName. |
| 461 | func (e *Event) Err(err error) *Event { |
| 462 | if e == nil { |
| 463 | return e |
| 464 | } |
| 465 | |
| 466 | if e.stack && ErrorStackMarshaler != nil { |
| 467 | switch m := ErrorStackMarshaler(err).(type) { |
| 468 | case nil: |
| 469 | // ErrorStackMarshaler returned nil — the error has no stack trace to |
| 470 | // attach. Fall through and still log the error via AnErr below. |
| 471 | case LogObjectMarshaler: |
| 472 | e = e.Object(ErrorStackFieldName, m) |
| 473 | case error: |
| 474 | e = e.Str(ErrorStackFieldName, m.Error()) |
| 475 | case string: |
| 476 | e = e.Str(ErrorStackFieldName, m) |
| 477 | default: |
| 478 | e = e.Interface(ErrorStackFieldName, m) |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return e.AnErr(ErrorFieldName, err) |
| 483 | } |
| 484 | |
| 485 | // Stack enables stack trace printing for the error passed to Err(). |
| 486 | // |