summaryrefslogtreecommitdiffstats
path: root/vendor/go.uber.org/zap
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/go.uber.org/zap')
-rw-r--r--vendor/go.uber.org/zap/CHANGELOG.md35
-rw-r--r--vendor/go.uber.org/zap/FAQ.md1
-rw-r--r--vendor/go.uber.org/zap/config.go2
-rw-r--r--vendor/go.uber.org/zap/field.go8
-rw-r--r--vendor/go.uber.org/zap/go.mod1
-rw-r--r--vendor/go.uber.org/zap/logger.go41
-rw-r--r--vendor/go.uber.org/zap/options.go19
-rw-r--r--vendor/go.uber.org/zap/sink.go2
-rw-r--r--vendor/go.uber.org/zap/stacktrace.go47
-rw-r--r--vendor/go.uber.org/zap/zapcore/console_encoder.go30
-rw-r--r--vendor/go.uber.org/zap/zapcore/encoder.go42
-rw-r--r--vendor/go.uber.org/zap/zapcore/entry.go14
-rw-r--r--vendor/go.uber.org/zap/zapcore/field.go18
-rw-r--r--vendor/go.uber.org/zap/zapcore/json_encoder.go30
-rw-r--r--vendor/go.uber.org/zap/zapcore/marshaler.go8
15 files changed, 214 insertions, 84 deletions
diff --git a/vendor/go.uber.org/zap/CHANGELOG.md b/vendor/go.uber.org/zap/CHANGELOG.md
index aeff90e4..fa817e6a 100644
--- a/vendor/go.uber.org/zap/CHANGELOG.md
+++ b/vendor/go.uber.org/zap/CHANGELOG.md
@@ -1,16 +1,37 @@
# Changelog
+## 1.16.0 (1 Sep 2020)
+
+Bugfixes:
+* [#828][]: Fix missing newline in IncreaseLevel error messages.
+* [#835][]: Fix panic in JSON encoder when encoding times or durations
+ without specifying a time or duration encoder.
+* [#843][]: Honor CallerSkip when taking stack traces.
+* [#862][]: Fix the default file permissions to use `0666` and rely on the umask instead.
+* [#854][]: Encode `<nil>` for nil `Stringer` instead of a panic error log.
+
+Enhancements:
+* [#629][]: Added `zapcore.TimeEncoderOfLayout` to easily create time encoders
+ for custom layouts.
+* [#697][]: Added support for a configurable delimiter in the console encoder.
+* [#852][]: Optimize console encoder by pooling the underlying JSON encoder.
+* [#844][]: Add ability to include the calling function as part of logs.
+* [#843][]: Add `StackSkip` for including truncated stacks as a field.
+* [#861][]: Add options to customize Fatal behaviour for better testability.
+
+Thanks to @SteelPhase, @tmshn, @lixingwang, @wyxloading, @moul, @segevfiner, @andy-retailnext and @jcorbin for their contributions to this release.
+
## 1.15.0 (23 Apr 2020)
Bugfixes:
* [#804][]: Fix handling of `Time` values out of `UnixNano` range.
-* [#812][]: Fix `IncreaseLevel` being reset after a call to `With`.
+* [#812][]: Fix `IncreaseLevel` being reset after a call to `With`.
Enhancements:
* [#806][]: Add `WithCaller` option to supersede the `AddCaller` option. This
allows disabling annotation of log entries with caller information if
previously enabled with `AddCaller`.
-* [#813][]: Deprecate `NewSampler` constructor in favor of
+* [#813][]: Deprecate `NewSampler` constructor in favor of
`NewSamplerWithOptions` which supports a `SamplerHook` option. This option
adds support for monitoring sampling decisions through a hook.
@@ -399,3 +420,13 @@ upgrade to the upcoming stable release.
[#812]: https://github.com/uber-go/zap/pull/812
[#806]: https://github.com/uber-go/zap/pull/806
[#813]: https://github.com/uber-go/zap/pull/813
+[#629]: https://github.com/uber-go/zap/pull/629
+[#697]: https://github.com/uber-go/zap/pull/697
+[#828]: https://github.com/uber-go/zap/pull/828
+[#835]: https://github.com/uber-go/zap/pull/835
+[#843]: https://github.com/uber-go/zap/pull/843
+[#844]: https://github.com/uber-go/zap/pull/844
+[#852]: https://github.com/uber-go/zap/pull/852
+[#854]: https://github.com/uber-go/zap/pull/854
+[#861]: https://github.com/uber-go/zap/pull/861
+[#862]: https://github.com/uber-go/zap/pull/862
diff --git a/vendor/go.uber.org/zap/FAQ.md b/vendor/go.uber.org/zap/FAQ.md
index 4256d35c..5ec72887 100644
--- a/vendor/go.uber.org/zap/FAQ.md
+++ b/vendor/go.uber.org/zap/FAQ.md
@@ -149,6 +149,7 @@ We're aware of the following extensions, but haven't used them ourselves:
| `github.com/tchap/zapext` | Sentry, syslog |
| `github.com/fgrosse/zaptest` | Ginkgo |
| `github.com/blendle/zapdriver` | Stackdriver |
+| `github.com/moul/zapgorm` | Gorm |
[go-proverbs]: https://go-proverbs.github.io/
[import-path]: https://golang.org/cmd/go/#hdr-Remote_import_paths
diff --git a/vendor/go.uber.org/zap/config.go b/vendor/go.uber.org/zap/config.go
index 192fd1a9..55637fb0 100644
--- a/vendor/go.uber.org/zap/config.go
+++ b/vendor/go.uber.org/zap/config.go
@@ -101,6 +101,7 @@ func NewProductionEncoderConfig() zapcore.EncoderConfig {
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
+ FunctionKey: zapcore.OmitKey,
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
@@ -140,6 +141,7 @@ func NewDevelopmentEncoderConfig() zapcore.EncoderConfig {
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
+ FunctionKey: zapcore.OmitKey,
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
diff --git a/vendor/go.uber.org/zap/field.go b/vendor/go.uber.org/zap/field.go
index dd558fc2..3c0d7d95 100644
--- a/vendor/go.uber.org/zap/field.go
+++ b/vendor/go.uber.org/zap/field.go
@@ -364,11 +364,17 @@ func Timep(key string, val *time.Time) Field {
// expensive (relatively speaking); this function both makes an allocation and
// takes about two microseconds.
func Stack(key string) Field {
+ return StackSkip(key, 1) // skip Stack
+}
+
+// StackSkip constructs a field similarly to Stack, but also skips the given
+// number of frames from the top of the stacktrace.
+func StackSkip(key string, skip int) Field {
// Returning the stacktrace as a string costs an allocation, but saves us
// from expanding the zapcore.Field union struct to include a byte slice. Since
// taking a stacktrace is already so expensive (~10us), the extra allocation
// is okay.
- return String(key, takeStacktrace())
+ return String(key, takeStacktrace(skip+1)) // skip StackSkip
}
// Duration constructs a field with the given key and value. The encoder
diff --git a/vendor/go.uber.org/zap/go.mod b/vendor/go.uber.org/zap/go.mod
index 118abda1..6ef4db70 100644
--- a/vendor/go.uber.org/zap/go.mod
+++ b/vendor/go.uber.org/zap/go.mod
@@ -8,5 +8,6 @@ require (
go.uber.org/atomic v1.6.0
go.uber.org/multierr v1.5.0
golang.org/x/lint v0.0.0-20190930215403-16217165b5de
+ gopkg.in/yaml.v2 v2.2.2
honnef.co/go/tools v0.0.1-2019.2.3
)
diff --git a/vendor/go.uber.org/zap/logger.go b/vendor/go.uber.org/zap/logger.go
index cd6e1955..ea484aed 100644
--- a/vendor/go.uber.org/zap/logger.go
+++ b/vendor/go.uber.org/zap/logger.go
@@ -49,6 +49,7 @@ type Logger struct {
addStack zapcore.LevelEnabler
callerSkip int
+ onFatal zapcore.CheckWriteAction // default is WriteThenFatal
}
// New constructs a new Logger from the provided zapcore.Core and Options. If
@@ -280,7 +281,13 @@ func (log *Logger) check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {
case zapcore.PanicLevel:
ce = ce.Should(ent, zapcore.WriteThenPanic)
case zapcore.FatalLevel:
- ce = ce.Should(ent, zapcore.WriteThenFatal)
+ onFatal := log.onFatal
+ // Noop is the default value for CheckWriteAction, and it leads to
+ // continued execution after a Fatal which is unexpected.
+ if onFatal == zapcore.WriteThenNoop {
+ onFatal = zapcore.WriteThenFatal
+ }
+ ce = ce.Should(ent, onFatal)
case zapcore.DPanicLevel:
if log.development {
ce = ce.Should(ent, zapcore.WriteThenPanic)
@@ -297,15 +304,41 @@ func (log *Logger) check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry {
// Thread the error output through to the CheckedEntry.
ce.ErrorOutput = log.errorOutput
if log.addCaller {
- ce.Entry.Caller = zapcore.NewEntryCaller(runtime.Caller(log.callerSkip + callerSkipOffset))
- if !ce.Entry.Caller.Defined {
+ frame, defined := getCallerFrame(log.callerSkip + callerSkipOffset)
+ if !defined {
fmt.Fprintf(log.errorOutput, "%v Logger.check error: failed to get caller\n", time.Now().UTC())
log.errorOutput.Sync()
}
+
+ ce.Entry.Caller = zapcore.EntryCaller{
+ Defined: defined,
+ PC: frame.PC,
+ File: frame.File,
+ Line: frame.Line,
+ Function: frame.Function,
+ }
}
if log.addStack.Enabled(ce.Entry.Level) {
- ce.Entry.Stack = Stack("").String
+ ce.Entry.Stack = StackSkip("", log.callerSkip+callerSkipOffset).String
}
return ce
}
+
+// getCallerFrame gets caller frame. The argument skip is the number of stack
+// frames to ascend, with 0 identifying the caller of getCallerFrame. The
+// boolean ok is false if it was not possible to recover the information.
+//
+// Note: This implementation is similar to runtime.Caller, but it returns the whole frame.
+func getCallerFrame(skip int) (frame runtime.Frame, ok bool) {
+ const skipOffset = 2 // skip getCallerFrame and Callers
+
+ pc := make([]uintptr, 1)
+ numFrames := runtime.Callers(skip+skipOffset, pc[:])
+ if numFrames < 1 {
+ return
+ }
+
+ frame, _ = runtime.CallersFrames(pc).Next()
+ return frame, frame.PC != 0
+}
diff --git a/vendor/go.uber.org/zap/options.go b/vendor/go.uber.org/zap/options.go
index 59f1b54a..0135c209 100644
--- a/vendor/go.uber.org/zap/options.go
+++ b/vendor/go.uber.org/zap/options.go
@@ -86,15 +86,15 @@ func Development() Option {
})
}
-// AddCaller configures the Logger to annotate each message with the filename
-// and line number of zap's caller. See also WithCaller.
+// AddCaller configures the Logger to annotate each message with the filename,
+// line number, and function name of zap's caller. See also WithCaller.
func AddCaller() Option {
return WithCaller(true)
}
-// WithCaller configures the Logger to annotate each message with the filename
-// and line number of zap's caller, or not, depending on the value of enabled.
-// This is a generalized form of AddCaller.
+// WithCaller configures the Logger to annotate each message with the filename,
+// line number, and function name of zap's caller, or not, depending on the
+// value of enabled. This is a generalized form of AddCaller.
func WithCaller(enabled bool) Option {
return optionFunc(func(log *Logger) {
log.addCaller = enabled
@@ -125,9 +125,16 @@ func IncreaseLevel(lvl zapcore.LevelEnabler) Option {
return optionFunc(func(log *Logger) {
core, err := zapcore.NewIncreaseLevelCore(log.core, lvl)
if err != nil {
- fmt.Fprintf(log.errorOutput, "failed to IncreaseLevel: %v", err)
+ fmt.Fprintf(log.errorOutput, "failed to IncreaseLevel: %v\n", err)
} else {
log.core = core
}
})
}
+
+// OnFatal sets the action to take on fatal logs.
+func OnFatal(action zapcore.CheckWriteAction) Option {
+ return optionFunc(func(log *Logger) {
+ log.onFatal = action
+ })
+}
diff --git a/vendor/go.uber.org/zap/sink.go b/vendor/go.uber.org/zap/sink.go
index ff0becfe..df46fa87 100644
--- a/vendor/go.uber.org/zap/sink.go
+++ b/vendor/go.uber.org/zap/sink.go
@@ -136,7 +136,7 @@ func newFileSink(u *url.URL) (Sink, error) {
case "stderr":
return nopCloserSink{os.Stderr}, nil
}
- return os.OpenFile(u.Path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
+ return os.OpenFile(u.Path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
}
func normalizeScheme(s string) (string, error) {
diff --git a/vendor/go.uber.org/zap/stacktrace.go b/vendor/go.uber.org/zap/stacktrace.go
index 100fac21..0cf8c1dd 100644
--- a/vendor/go.uber.org/zap/stacktrace.go
+++ b/vendor/go.uber.org/zap/stacktrace.go
@@ -22,28 +22,20 @@ package zap
import (
"runtime"
- "strings"
"sync"
"go.uber.org/zap/internal/bufferpool"
)
-const _zapPackage = "go.uber.org/zap"
-
var (
_stacktracePool = sync.Pool{
New: func() interface{} {
return newProgramCounters(64)
},
}
-
- // We add "." and "/" suffixes to the package name to ensure we only match
- // the exact package and not any package with the same prefix.
- _zapStacktracePrefixes = addPrefix(_zapPackage, ".", "/")
- _zapStacktraceVendorContains = addPrefix("/vendor/", _zapStacktracePrefixes...)
)
-func takeStacktrace() string {
+func takeStacktrace(skip int) string {
buffer := bufferpool.Get()
defer buffer.Free()
programCounters := _stacktracePool.Get().(*programCounters)
@@ -51,9 +43,9 @@ func takeStacktrace() string {
var numFrames int
for {
- // Skip the call to runtime.Counters and takeStacktrace so that the
+ // Skip the call to runtime.Callers and takeStacktrace so that the
// program counters start at the caller of takeStacktrace.
- numFrames = runtime.Callers(2, programCounters.pcs)
+ numFrames = runtime.Callers(skip+2, programCounters.pcs)
if numFrames < len(programCounters.pcs) {
break
}
@@ -63,19 +55,12 @@ func takeStacktrace() string {
}
i := 0
- skipZapFrames := true // skip all consecutive zap frames at the beginning.
frames := runtime.CallersFrames(programCounters.pcs[:numFrames])
// Note: On the last iteration, frames.Next() returns false, with a valid
// frame, but we ignore this frame. The last frame is a a runtime frame which
// adds noise, since it's only either runtime.main or runtime.goexit.
for frame, more := frames.Next(); more; frame, more = frames.Next() {
- if skipZapFrames && isZapFrame(frame.Function) {
- continue
- } else {
- skipZapFrames = false
- }
-
if i != 0 {
buffer.AppendByte('\n')
}
@@ -91,24 +76,6 @@ func takeStacktrace() string {
return buffer.String()
}
-func isZapFrame(function string) bool {
- for _, prefix := range _zapStacktracePrefixes {
- if strings.HasPrefix(function, prefix) {
- return true
- }
- }
-
- // We can't use a prefix match here since the location of the vendor
- // directory affects the prefix. Instead we do a contains match.
- for _, contains := range _zapStacktraceVendorContains {
- if strings.Contains(function, contains) {
- return true
- }
- }
-
- return false
-}
-
type programCounters struct {
pcs []uintptr
}
@@ -116,11 +83,3 @@ type programCounters struct {
func newProgramCounters(size int) *programCounters {
return &programCounters{make([]uintptr, size)}
}
-
-func addPrefix(prefix string, ss ...string) []string {
- withPrefix := make([]string, len(ss))
- for i, s := range ss {
- withPrefix[i] = prefix + s
- }
- return withPrefix
-}
diff --git a/vendor/go.uber.org/zap/zapcore/console_encoder.go b/vendor/go.uber.org/zap/zapcore/console_encoder.go
index b7875966..3b68f8c0 100644
--- a/vendor/go.uber.org/zap/zapcore/console_encoder.go
+++ b/vendor/go.uber.org/zap/zapcore/console_encoder.go
@@ -56,6 +56,10 @@ type consoleEncoder struct {
// encoder configuration, it will omit any element whose key is set to the empty
// string.
func NewConsoleEncoder(cfg EncoderConfig) Encoder {
+ if len(cfg.ConsoleSeparator) == 0 {
+ // Use a default delimiter of '\t' for backwards compatibility
+ cfg.ConsoleSeparator = "\t"
+ }
return consoleEncoder{newJSONEncoder(cfg, true)}
}
@@ -89,12 +93,17 @@ func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
nameEncoder(ent.LoggerName, arr)
}
- if ent.Caller.Defined && c.CallerKey != "" && c.EncodeCaller != nil {
- c.EncodeCaller(ent.Caller, arr)
+ if ent.Caller.Defined {
+ if c.CallerKey != "" && c.EncodeCaller != nil {
+ c.EncodeCaller(ent.Caller, arr)
+ }
+ if c.FunctionKey != "" {
+ arr.AppendString(ent.Caller.Function)
+ }
}
for i := range arr.elems {
if i > 0 {
- line.AppendByte('\t')
+ line.AppendString(c.ConsoleSeparator)
}
fmt.Fprint(line, arr.elems[i])
}
@@ -102,7 +111,7 @@ func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
// Add the message itself.
if c.MessageKey != "" {
- c.addTabIfNecessary(line)
+ c.addSeparatorIfNecessary(line)
line.AppendString(ent.Message)
}
@@ -126,7 +135,12 @@ func (c consoleEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
func (c consoleEncoder) writeContext(line *buffer.Buffer, extra []Field) {
context := c.jsonEncoder.Clone().(*jsonEncoder)
- defer context.buf.Free()
+ defer func() {
+ // putJSONEncoder assumes the buffer is still used, but we write out the buffer so
+ // we can free it.
+ context.buf.Free()
+ putJSONEncoder(context)
+ }()
addFields(context, extra)
context.closeOpenNamespaces()
@@ -134,14 +148,14 @@ func (c consoleEncoder) writeContext(line *buffer.Buffer, extra []Field) {
return
}
- c.addTabIfNecessary(line)
+ c.addSeparatorIfNecessary(line)
line.AppendByte('{')
line.Write(context.buf.Bytes())
line.AppendByte('}')
}
-func (c consoleEncoder) addTabIfNecessary(line *buffer.Buffer) {
+func (c consoleEncoder) addSeparatorIfNecessary(line *buffer.Buffer) {
if line.Len() > 0 {
- line.AppendByte('\t')
+ line.AppendString(c.ConsoleSeparator)
}
}
diff --git a/vendor/go.uber.org/zap/zapcore/encoder.go b/vendor/go.uber.org/zap/zapcore/encoder.go
index 6c78f7e4..6601ca16 100644
--- a/vendor/go.uber.org/zap/zapcore/encoder.go
+++ b/vendor/go.uber.org/zap/zapcore/encoder.go
@@ -21,6 +21,7 @@
package zapcore
import (
+ "encoding/json"
"time"
"go.uber.org/zap/buffer"
@@ -151,6 +152,14 @@ func RFC3339NanoTimeEncoder(t time.Time, enc PrimitiveArrayEncoder) {
encodeTimeLayout(t, time.RFC3339Nano, enc)
}
+// TimeEncoderOfLayout returns TimeEncoder which serializes a time.Time using
+// given layout.
+func TimeEncoderOfLayout(layout string) TimeEncoder {
+ return func(t time.Time, enc PrimitiveArrayEncoder) {
+ encodeTimeLayout(t, layout, enc)
+ }
+}
+
// UnmarshalText unmarshals text to a TimeEncoder.
// "rfc3339nano" and "RFC3339Nano" are unmarshaled to RFC3339NanoTimeEncoder.
// "rfc3339" and "RFC3339" are unmarshaled to RFC3339TimeEncoder.
@@ -176,6 +185,35 @@ func (e *TimeEncoder) UnmarshalText(text []byte) error {
return nil
}
+// UnmarshalYAML unmarshals YAML to a TimeEncoder.
+// If value is an object with a "layout" field, it will be unmarshaled to TimeEncoder with given layout.
+// timeEncoder:
+// layout: 06/01/02 03:04pm
+// If value is string, it uses UnmarshalText.
+// timeEncoder: iso8601
+func (e *TimeEncoder) UnmarshalYAML(unmarshal func(interface{}) error) error {
+ var o struct {
+ Layout string `json:"layout" yaml:"layout"`
+ }
+ if err := unmarshal(&o); err == nil {
+ *e = TimeEncoderOfLayout(o.Layout)
+ return nil
+ }
+
+ var s string
+ if err := unmarshal(&s); err != nil {
+ return err
+ }
+ return e.UnmarshalText([]byte(s))
+}
+
+// UnmarshalJSON unmarshals JSON to a TimeEncoder as same way UnmarshalYAML does.
+func (e *TimeEncoder) UnmarshalJSON(data []byte) error {
+ return e.UnmarshalYAML(func(v interface{}) error {
+ return json.Unmarshal(data, v)
+ })
+}
+
// A DurationEncoder serializes a time.Duration to a primitive type.
type DurationEncoder func(time.Duration, PrimitiveArrayEncoder)
@@ -279,6 +317,7 @@ type EncoderConfig struct {
TimeKey string `json:"timeKey" yaml:"timeKey"`
NameKey string `json:"nameKey" yaml:"nameKey"`
CallerKey string `json:"callerKey" yaml:"callerKey"`
+ FunctionKey string `json:"functionKey" yaml:"functionKey"`
StacktraceKey string `json:"stacktraceKey" yaml:"stacktraceKey"`
LineEnding string `json:"lineEnding" yaml:"lineEnding"`
// Configure the primitive representations of common complex types. For
@@ -291,6 +330,9 @@ type EncoderConfig struct {
// Unlike the other primitive type encoders, EncodeName is optional. The
// zero value falls back to FullNameEncoder.
EncodeName NameEncoder `json:"nameEncoder" yaml:"nameEncoder"`
+ // Configures the field separator used by the console encoder. Defaults
+ // to tab.
+ ConsoleSeparator string `json:"consoleSeparator" yaml:"consoleSeparator"`
}
// ObjectEncoder is a strongly-typed, encoding-agnostic interface for adding a
diff --git a/vendor/go.uber.org/zap/zapcore/entry.go b/vendor/go.uber.org/zap/zapcore/entry.go
index 8273abdf..4aa8b4f9 100644
--- a/vendor/go.uber.org/zap/zapcore/entry.go
+++ b/vendor/go.uber.org/zap/zapcore/entry.go
@@ -22,6 +22,7 @@ package zapcore
import (
"fmt"
+ "runtime"
"strings"
"sync"
"time"
@@ -70,10 +71,11 @@ func NewEntryCaller(pc uintptr, file string, line int, ok bool) EntryCaller {
// EntryCaller represents the caller of a logging function.
type EntryCaller struct {
- Defined bool
- PC uintptr
- File string
- Line int
+ Defined bool
+ PC uintptr
+ File string
+ Line int
+ Function string
}
// String returns the full path and line number of the caller.
@@ -158,6 +160,8 @@ const (
// WriteThenNoop indicates that nothing special needs to be done. It's the
// default behavior.
WriteThenNoop CheckWriteAction = iota
+ // WriteThenGoexit runs runtime.Goexit after Write.
+ WriteThenGoexit
// WriteThenPanic causes a panic after Write.
WriteThenPanic
// WriteThenFatal causes a fatal os.Exit after Write.
@@ -230,6 +234,8 @@ func (ce *CheckedEntry) Write(fields ...Field) {
panic(msg)
case WriteThenFatal:
exit.Exit()
+ case WriteThenGoexit:
+ runtime.Goexit()
}
}
diff --git a/vendor/go.uber.org/zap/zapcore/field.go b/vendor/go.uber.org/zap/zapcore/field.go
index 6e05f831..7e255d63 100644
--- a/vendor/go.uber.org/zap/zapcore/field.go
+++ b/vendor/go.uber.org/zap/zapcore/field.go
@@ -205,13 +205,23 @@ func addFields(enc ObjectEncoder, fields []Field) {
}
}
-func encodeStringer(key string, stringer interface{}, enc ObjectEncoder) (err error) {
+func encodeStringer(key string, stringer interface{}, enc ObjectEncoder) (retErr error) {
+ // Try to capture panics (from nil references or otherwise) when calling
+ // the String() method, similar to https://golang.org/src/fmt/print.go#L540
defer func() {
- if v := recover(); v != nil {
- err = fmt.Errorf("PANIC=%v", v)
+ if err := recover(); err != nil {
+ // If it's a nil pointer, just say "<nil>". The likeliest causes are a
+ // Stringer that fails to guard against nil or a nil pointer for a
+ // value receiver, and in either case, "<nil>" is a nice result.
+ if v := reflect.ValueOf(stringer); v.Kind() == reflect.Ptr && v.IsNil() {
+ enc.AddString(key, "<nil>")
+ return
+ }
+
+ retErr = fmt.Errorf("PANIC=%v", err)
}
}()
enc.AddString(key, stringer.(fmt.Stringer).String())
- return
+ return nil
}
diff --git a/vendor/go.uber.org/zap/zapcore/json_encoder.go b/vendor/go.uber.org/zap/zapcore/json_encoder.go
index 7facc1b3..5cf7d917 100644
--- a/vendor/go.uber.org/zap/zapcore/json_encoder.go
+++ b/vendor/go.uber.org/zap/zapcore/json_encoder.go
@@ -236,7 +236,9 @@ func (enc *jsonEncoder) AppendComplex128(val complex128) {
func (enc *jsonEncoder) AppendDuration(val time.Duration) {
cur := enc.buf.Len()
- enc.EncodeDuration(val, enc)
+ if e := enc.EncodeDuration; e != nil {
+ e(val, enc)
+ }
if cur == enc.buf.Len() {
// User-supplied EncodeDuration is a no-op. Fall back to nanoseconds to keep
// JSON valid.
@@ -275,7 +277,9 @@ func (enc *jsonEncoder) AppendTimeLayout(time time.Time, layout string) {
func (enc *jsonEncoder) AppendTime(val time.Time) {
cur := enc.buf.Len()
- enc.EncodeTime(val, enc)
+ if e := enc.EncodeTime; e != nil {
+ e(val, enc)
+ }
if cur == enc.buf.Len() {
// User-supplied EncodeTime is a no-op. Fall back to nanos since epoch to keep
// output JSON valid.
@@ -362,14 +366,20 @@ func (enc *jsonEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
final.AppendString(ent.LoggerName)
}
}
- if ent.Caller.Defined && final.CallerKey != "" {
- final.addKey(final.CallerKey)
- cur := final.buf.Len()
- final.EncodeCaller(ent.Caller, final)
- if cur == final.buf.Len() {
- // User-supplied EncodeCaller was a no-op. Fall back to strings to
- // keep output JSON valid.
- final.AppendString(ent.Caller.String())
+ if ent.Caller.Defined {
+ if final.CallerKey != "" {
+ final.addKey(final.CallerKey)
+ cur := final.buf.Len()
+ final.EncodeCaller(ent.Caller, final)
+ if cur == final.buf.Len() {
+ // User-supplied EncodeCaller was a no-op. Fall back to strings to
+ // keep output JSON valid.
+ final.AppendString(ent.Caller.String())
+ }
+ }
+ if final.FunctionKey != "" {
+ final.addKey(final.FunctionKey)
+ final.AppendString(ent.Caller.Function)
}
}
if final.MessageKey != "" {
diff --git a/vendor/go.uber.org/zap/zapcore/marshaler.go b/vendor/go.uber.org/zap/zapcore/marshaler.go
index 2627a653..c3c55ba0 100644
--- a/vendor/go.uber.org/zap/zapcore/marshaler.go
+++ b/vendor/go.uber.org/zap/zapcore/marshaler.go
@@ -23,6 +23,10 @@ package zapcore
// ObjectMarshaler allows user-defined types to efficiently add themselves to the
// logging context, and to selectively omit information which shouldn't be
// included in logs (e.g., passwords).
+//
+// Note: ObjectMarshaler is only used when zap.Object is used or when
+// passed directly to zap.Any. It is not used when reflection-based
+// encoding is used.
type ObjectMarshaler interface {
MarshalLogObject(ObjectEncoder) error
}
@@ -39,6 +43,10 @@ func (f ObjectMarshalerFunc) MarshalLogObject(enc ObjectEncoder) error {
// ArrayMarshaler allows user-defined types to efficiently add themselves to the
// logging context, and to selectively omit information which shouldn't be
// included in logs (e.g., passwords).
+//
+// Note: ArrayMarshaler is only used when zap.Array is used or when
+// passed directly to zap.Any. It is not used when reflection-based
+// encoding is used.
type ArrayMarshaler interface {
MarshalLogArray(ArrayEncoder) error
}