|
|
@@ -28,10 +28,14 @@ import (
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
)
|
|
|
|
|
|
+// Field is an alias for Field. Aliasing this type dramatically
|
|
|
+// improves the navigability of this package's API documentation.
|
|
|
+type Field = zapcore.Field
|
|
|
+
|
|
|
// Skip constructs a no-op field, which is often useful when handling invalid
|
|
|
// inputs in other Field constructors.
|
|
|
-func Skip() zapcore.Field {
|
|
|
- return zapcore.Field{Type: zapcore.SkipType}
|
|
|
+func Skip() Field {
|
|
|
+ return Field{Type: zapcore.SkipType}
|
|
|
}
|
|
|
|
|
|
// Binary constructs a field that carries an opaque binary blob.
|
|
|
@@ -39,112 +43,112 @@ func Skip() zapcore.Field {
|
|
|
// Binary data is serialized in an encoding-appropriate format. For example,
|
|
|
// zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text,
|
|
|
// use ByteString.
|
|
|
-func Binary(key string, val []byte) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.BinaryType, Interface: val}
|
|
|
+func Binary(key string, val []byte) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.BinaryType, Interface: val}
|
|
|
}
|
|
|
|
|
|
// Bool constructs a field that carries a bool.
|
|
|
-func Bool(key string, val bool) zapcore.Field {
|
|
|
+func Bool(key string, val bool) Field {
|
|
|
var ival int64
|
|
|
if val {
|
|
|
ival = 1
|
|
|
}
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.BoolType, Integer: ival}
|
|
|
+ return Field{Key: key, Type: zapcore.BoolType, Integer: ival}
|
|
|
}
|
|
|
|
|
|
// ByteString constructs a field that carries UTF-8 encoded text as a []byte.
|
|
|
// To log opaque binary blobs (which aren't necessarily valid UTF-8), use
|
|
|
// Binary.
|
|
|
-func ByteString(key string, val []byte) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.ByteStringType, Interface: val}
|
|
|
+func ByteString(key string, val []byte) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.ByteStringType, Interface: val}
|
|
|
}
|
|
|
|
|
|
// Complex128 constructs a field that carries a complex number. Unlike most
|
|
|
// numeric fields, this costs an allocation (to convert the complex128 to
|
|
|
// interface{}).
|
|
|
-func Complex128(key string, val complex128) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Complex128Type, Interface: val}
|
|
|
+func Complex128(key string, val complex128) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Complex128Type, Interface: val}
|
|
|
}
|
|
|
|
|
|
// Complex64 constructs a field that carries a complex number. Unlike most
|
|
|
// numeric fields, this costs an allocation (to convert the complex64 to
|
|
|
// interface{}).
|
|
|
-func Complex64(key string, val complex64) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Complex64Type, Interface: val}
|
|
|
+func Complex64(key string, val complex64) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Complex64Type, Interface: val}
|
|
|
}
|
|
|
|
|
|
// Float64 constructs a field that carries a float64. The way the
|
|
|
// floating-point value is represented is encoder-dependent, so marshaling is
|
|
|
// necessarily lazy.
|
|
|
-func Float64(key string, val float64) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Float64Type, Integer: int64(math.Float64bits(val))}
|
|
|
+func Float64(key string, val float64) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Float64Type, Integer: int64(math.Float64bits(val))}
|
|
|
}
|
|
|
|
|
|
// Float32 constructs a field that carries a float32. The way the
|
|
|
// floating-point value is represented is encoder-dependent, so marshaling is
|
|
|
// necessarily lazy.
|
|
|
-func Float32(key string, val float32) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Float32Type, Integer: int64(math.Float32bits(val))}
|
|
|
+func Float32(key string, val float32) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Float32Type, Integer: int64(math.Float32bits(val))}
|
|
|
}
|
|
|
|
|
|
// Int constructs a field with the given key and value.
|
|
|
-func Int(key string, val int) zapcore.Field {
|
|
|
+func Int(key string, val int) Field {
|
|
|
return Int64(key, int64(val))
|
|
|
}
|
|
|
|
|
|
// Int64 constructs a field with the given key and value.
|
|
|
-func Int64(key string, val int64) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Int64Type, Integer: val}
|
|
|
+func Int64(key string, val int64) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Int64Type, Integer: val}
|
|
|
}
|
|
|
|
|
|
// Int32 constructs a field with the given key and value.
|
|
|
-func Int32(key string, val int32) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Int32Type, Integer: int64(val)}
|
|
|
+func Int32(key string, val int32) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Int32Type, Integer: int64(val)}
|
|
|
}
|
|
|
|
|
|
// Int16 constructs a field with the given key and value.
|
|
|
-func Int16(key string, val int16) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Int16Type, Integer: int64(val)}
|
|
|
+func Int16(key string, val int16) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Int16Type, Integer: int64(val)}
|
|
|
}
|
|
|
|
|
|
// Int8 constructs a field with the given key and value.
|
|
|
-func Int8(key string, val int8) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Int8Type, Integer: int64(val)}
|
|
|
+func Int8(key string, val int8) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Int8Type, Integer: int64(val)}
|
|
|
}
|
|
|
|
|
|
// String constructs a field with the given key and value.
|
|
|
-func String(key string, val string) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.StringType, String: val}
|
|
|
+func String(key string, val string) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.StringType, String: val}
|
|
|
}
|
|
|
|
|
|
// Uint constructs a field with the given key and value.
|
|
|
-func Uint(key string, val uint) zapcore.Field {
|
|
|
+func Uint(key string, val uint) Field {
|
|
|
return Uint64(key, uint64(val))
|
|
|
}
|
|
|
|
|
|
// Uint64 constructs a field with the given key and value.
|
|
|
-func Uint64(key string, val uint64) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Uint64Type, Integer: int64(val)}
|
|
|
+func Uint64(key string, val uint64) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Uint64Type, Integer: int64(val)}
|
|
|
}
|
|
|
|
|
|
// Uint32 constructs a field with the given key and value.
|
|
|
-func Uint32(key string, val uint32) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Uint32Type, Integer: int64(val)}
|
|
|
+func Uint32(key string, val uint32) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Uint32Type, Integer: int64(val)}
|
|
|
}
|
|
|
|
|
|
// Uint16 constructs a field with the given key and value.
|
|
|
-func Uint16(key string, val uint16) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Uint16Type, Integer: int64(val)}
|
|
|
+func Uint16(key string, val uint16) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Uint16Type, Integer: int64(val)}
|
|
|
}
|
|
|
|
|
|
// Uint8 constructs a field with the given key and value.
|
|
|
-func Uint8(key string, val uint8) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.Uint8Type, Integer: int64(val)}
|
|
|
+func Uint8(key string, val uint8) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.Uint8Type, Integer: int64(val)}
|
|
|
}
|
|
|
|
|
|
// Uintptr constructs a field with the given key and value.
|
|
|
-func Uintptr(key string, val uintptr) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.UintptrType, Integer: int64(val)}
|
|
|
+func Uintptr(key string, val uintptr) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.UintptrType, Integer: int64(val)}
|
|
|
}
|
|
|
|
|
|
// Reflect constructs a field with the given key and an arbitrary object. It uses
|
|
|
@@ -154,8 +158,8 @@ func Uintptr(key string, val uintptr) zapcore.Field {
|
|
|
//
|
|
|
// If encoding fails (e.g., trying to serialize a map[int]string to JSON), Reflect
|
|
|
// includes the error message in the final log output.
|
|
|
-func Reflect(key string, val interface{}) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.ReflectType, Interface: val}
|
|
|
+func Reflect(key string, val interface{}) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.ReflectType, Interface: val}
|
|
|
}
|
|
|
|
|
|
// Namespace creates a named, isolated scope within the logger's context. All
|
|
|
@@ -163,27 +167,27 @@ func Reflect(key string, val interface{}) zapcore.Field {
|
|
|
//
|
|
|
// This helps prevent key collisions when injecting loggers into sub-components
|
|
|
// or third-party libraries.
|
|
|
-func Namespace(key string) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.NamespaceType}
|
|
|
+func Namespace(key string) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.NamespaceType}
|
|
|
}
|
|
|
|
|
|
// Stringer constructs a field with the given key and the output of the value's
|
|
|
// String method. The Stringer's String method is called lazily.
|
|
|
-func Stringer(key string, val fmt.Stringer) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.StringerType, Interface: val}
|
|
|
+func Stringer(key string, val fmt.Stringer) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.StringerType, Interface: val}
|
|
|
}
|
|
|
|
|
|
-// Time constructs a zapcore.Field with the given key and value. The encoder
|
|
|
+// Time constructs a Field with the given key and value. The encoder
|
|
|
// controls how the time is serialized.
|
|
|
-func Time(key string, val time.Time) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.TimeType, Integer: val.UnixNano(), Interface: val.Location()}
|
|
|
+func Time(key string, val time.Time) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.TimeType, Integer: val.UnixNano(), Interface: val.Location()}
|
|
|
}
|
|
|
|
|
|
// Stack constructs a field that stores a stacktrace of the current goroutine
|
|
|
// under provided key. Keep in mind that taking a stacktrace is eager and
|
|
|
// expensive (relatively speaking); this function both makes an allocation and
|
|
|
// takes about two microseconds.
|
|
|
-func Stack(key string) zapcore.Field {
|
|
|
+func Stack(key string) 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
|
|
|
@@ -193,16 +197,16 @@ func Stack(key string) zapcore.Field {
|
|
|
|
|
|
// Duration constructs a field with the given key and value. The encoder
|
|
|
// controls how the duration is serialized.
|
|
|
-func Duration(key string, val time.Duration) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.DurationType, Integer: int64(val)}
|
|
|
+func Duration(key string, val time.Duration) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.DurationType, Integer: int64(val)}
|
|
|
}
|
|
|
|
|
|
// Object constructs a field with the given key and ObjectMarshaler. It
|
|
|
// provides a flexible, but still type-safe and efficient, way to add map- or
|
|
|
// struct-like user-defined types to the logging context. The struct's
|
|
|
// MarshalLogObject method is called lazily.
|
|
|
-func Object(key string, val zapcore.ObjectMarshaler) zapcore.Field {
|
|
|
- return zapcore.Field{Key: key, Type: zapcore.ObjectMarshalerType, Interface: val}
|
|
|
+func Object(key string, val zapcore.ObjectMarshaler) Field {
|
|
|
+ return Field{Key: key, Type: zapcore.ObjectMarshalerType, Interface: val}
|
|
|
}
|
|
|
|
|
|
// Any takes a key and an arbitrary value and chooses the best way to represent
|
|
|
@@ -210,9 +214,9 @@ func Object(key string, val zapcore.ObjectMarshaler) zapcore.Field {
|
|
|
// necessary.
|
|
|
//
|
|
|
// Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between
|
|
|
-// them. To minimize suprise, []byte values are treated as binary blobs, byte
|
|
|
+// them. To minimize surprises, []byte values are treated as binary blobs, byte
|
|
|
// values are treated as uint8, and runes are always treated as integers.
|
|
|
-func Any(key string, value interface{}) zapcore.Field {
|
|
|
+func Any(key string, value interface{}) Field {
|
|
|
switch val := value.(type) {
|
|
|
case zapcore.ObjectMarshaler:
|
|
|
return Object(key, val)
|