Source File
field.go
Belonging Package
go.uber.org/zap/zapcore
// Copyright (c) 2016 Uber Technologies, Inc.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to deal// in the Software without restriction, including without limitation the rights// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell// copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions://// The above copyright notice and this permission notice shall be included in// all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN// THE SOFTWARE.package zapcoreimport ()// A FieldType indicates which member of the Field union struct should be used// and how it should be serialized.type FieldType uint8const (// UnknownType is the default field type. Attempting to add it to an encoder will panic.UnknownType FieldType = iota// ArrayMarshalerType indicates that the field carries an ArrayMarshaler.ArrayMarshalerType// ObjectMarshalerType indicates that the field carries an ObjectMarshaler.ObjectMarshalerType// BinaryType indicates that the field carries an opaque binary blob.BinaryType// BoolType indicates that the field carries a bool.BoolType// ByteStringType indicates that the field carries UTF-8 encoded bytes.ByteStringType// Complex128Type indicates that the field carries a complex128.Complex128Type// Complex64Type indicates that the field carries a complex128.Complex64Type// DurationType indicates that the field carries a time.Duration.DurationType// Float64Type indicates that the field carries a float64.Float64Type// Float32Type indicates that the field carries a float32.Float32Type// Int64Type indicates that the field carries an int64.Int64Type// Int32Type indicates that the field carries an int32.Int32Type// Int16Type indicates that the field carries an int16.Int16Type// Int8Type indicates that the field carries an int8.Int8Type// StringType indicates that the field carries a string.StringType// TimeType indicates that the field carries a time.Time that is// representable by a UnixNano() stored as an int64.TimeType// TimeFullType indicates that the field carries a time.Time stored as-is.TimeFullType// Uint64Type indicates that the field carries a uint64.Uint64Type// Uint32Type indicates that the field carries a uint32.Uint32Type// Uint16Type indicates that the field carries a uint16.Uint16Type// Uint8Type indicates that the field carries a uint8.Uint8Type// UintptrType indicates that the field carries a uintptr.UintptrType// ReflectType indicates that the field carries an interface{}, which should// be serialized using reflection.ReflectType// NamespaceType signals the beginning of an isolated namespace. All// subsequent fields should be added to the new namespace.NamespaceType// StringerType indicates that the field carries a fmt.Stringer.StringerType// ErrorType indicates that the field carries an error.ErrorType// SkipType indicates that the field is a no-op.SkipType// InlineMarshalerType indicates that the field carries an ObjectMarshaler// that should be inlined.InlineMarshalerType)// A Field is a marshaling operation used to add a key-value pair to a logger's// context. Most fields are lazily marshaled, so it's inexpensive to add fields// to disabled debug-level log statements.type Field struct {Key stringType FieldTypeInteger int64String stringInterface interface{}}// AddTo exports a field through the ObjectEncoder interface. It's primarily// useful to library authors, and shouldn't be necessary in most applications.func ( Field) ( ObjectEncoder) {var errorswitch .Type {case ArrayMarshalerType:= .AddArray(.Key, .Interface.(ArrayMarshaler))case ObjectMarshalerType:= .AddObject(.Key, .Interface.(ObjectMarshaler))case InlineMarshalerType:= .Interface.(ObjectMarshaler).MarshalLogObject()case BinaryType:.AddBinary(.Key, .Interface.([]byte))case BoolType:.AddBool(.Key, .Integer == 1)case ByteStringType:.AddByteString(.Key, .Interface.([]byte))case Complex128Type:.AddComplex128(.Key, .Interface.(complex128))case Complex64Type:.AddComplex64(.Key, .Interface.(complex64))case DurationType:.AddDuration(.Key, time.Duration(.Integer))case Float64Type:.AddFloat64(.Key, math.Float64frombits(uint64(.Integer)))case Float32Type:.AddFloat32(.Key, math.Float32frombits(uint32(.Integer)))case Int64Type:.AddInt64(.Key, .Integer)case Int32Type:.AddInt32(.Key, int32(.Integer))case Int16Type:.AddInt16(.Key, int16(.Integer))case Int8Type:.AddInt8(.Key, int8(.Integer))case StringType:.AddString(.Key, .String)case TimeType:if .Interface != nil {.AddTime(.Key, time.Unix(0, .Integer).In(.Interface.(*time.Location)))} else {// Fall back to UTC if location is nil..AddTime(.Key, time.Unix(0, .Integer))}case TimeFullType:.AddTime(.Key, .Interface.(time.Time))case Uint64Type:.AddUint64(.Key, uint64(.Integer))case Uint32Type:.AddUint32(.Key, uint32(.Integer))case Uint16Type:.AddUint16(.Key, uint16(.Integer))case Uint8Type:.AddUint8(.Key, uint8(.Integer))case UintptrType:.AddUintptr(.Key, uintptr(.Integer))case ReflectType:= .AddReflected(.Key, .Interface)case NamespaceType:.OpenNamespace(.Key)case StringerType:= encodeStringer(.Key, .Interface, )case ErrorType:= encodeError(.Key, .Interface.(error), )case SkipType:breakdefault:panic(fmt.Sprintf("unknown field type: %v", ))}if != nil {.AddString(fmt.Sprintf("%sError", .Key), .Error())}}// Equals returns whether two fields are equal. For non-primitive types such as// errors, marshalers, or reflect types, it uses reflect.DeepEqual.func ( Field) ( Field) bool {if .Type != .Type {return false}if .Key != .Key {return false}switch .Type {case BinaryType, ByteStringType:return bytes.Equal(.Interface.([]byte), .Interface.([]byte))case ArrayMarshalerType, ObjectMarshalerType, ErrorType, ReflectType:return reflect.DeepEqual(.Interface, .Interface)default:return ==}}func ( ObjectEncoder, []Field) {for := range {[].AddTo()}}func ( string, interface{}, ObjectEncoder) ( error) {// Try to capture panics (from nil references or otherwise) when calling// the String() method, similar to https://golang.org/src/fmt/print.go#L540defer func() {if := recover(); != 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 := reflect.ValueOf(); .Kind() == reflect.Ptr && .IsNil() {.AddString(, "<nil>")return}= fmt.Errorf("PANIC=%v", )}}().AddString(, .(fmt.Stringer).String())return nil}
![]() |
The pages are generated with Golds v0.6.7. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |