本文主要研究一下golang的zap的ReflectType
文章插图
sweetenFieldszap@v1.16.0/sugar.go
func (s *SugaredLogger) sweetenFields(args []interface{}) []Field { if len(args) == 0 { return nil } // Allocate enough space for the worst case; if users pass only structured // fields, we shouldn't penalize them with extra allocations. fields := make([]Field, 0, len(args)) var invalid invalidPairs for i := 0; i < len(args); { // This is a strongly-typed field. Consume it and move on. if f, ok := args[i].(Field); ok { fields = Append(fields, f) i++ continue } // Make sure this element isn't a dangling key. if i == len(args)-1 { s.base.DPanic(_oddNumberErrMsg, Any("ignored", args[i])) break } // Consume this value and the next, treating them as a key-value pair. If the // key isn't a string, add this pair to the slice of invalid pairs. key, val := args[i], args[i+1] if keyStr, ok := key.(string); !ok { // Subsequent errors are likely, so allocate once up front. if cap(invalid) == 0 { invalid = make(invalidPairs, 0, len(args)/2) } invalid = append(invalid, invalidPair{i, key, val}) } else { fields = append(fields, Any(keyStr, val)) } i += 2 } // If we encountered any invalid key-value pairs, log an error. if len(invalid) > 0 { s.base.DPanic(_nonStringKeyErrMsg, Array("invalid", invalid)) } return fields}
sweetenFields方法执行的是fields = append(fields, Any(keyStr, val))Anyzap@v1.16.0/field.go
func Any(key string, value interface{}) Field { switch val := value.(type) { case zapcore.ObjectMarshaler: return Object(key, val) case zapcore.ArrayMarshaler: return Array(key, val) case bool: return Bool(key, val) case *bool: return Boolp(key, val) case []bool: return Bools(key, val) case complex128: return Complex128(key, val) case *complex128: return Complex128p(key, val) case []complex128: return Complex128s(key, val) case complex64: return Complex64(key, val) case *complex64: return Complex64p(key, val) case []complex64: return Complex64s(key, val) case float64: return Float64(key, val) case *float64: return Float64p(key, val) case []float64: return Float64s(key, val) case float32: return Float32(key, val) case *float32: return Float32p(key, val) case []float32: return Float32s(key, val) case int: return Int(key, val) case *int: return Intp(key, val) case []int: return Ints(key, val) case int64: return Int64(key, val) case *int64: return Int64p(key, val) case []int64: return Int64s(key, val) case int32: return Int32(key, val) case *int32: return Int32p(key, val) case []int32: return Int32s(key, val) case int16: return Int16(key, val) case *int16: return Int16p(key, val) case []int16: return Int16s(key, val) case int8: return Int8(key, val) case *int8: return Int8p(key, val) case []int8: return Int8s(key, val) case string: return String(key, val) case *string: return Stringp(key, val) case []string: return Strings(key, val) case uint: return Uint(key, val) case *uint: return Uintp(key, val) case []uint: return Uints(key, val) case uint64: return Uint64(key, val) case *uint64: return Uint64p(key, val) case []uint64: return Uint64s(key, val) case uint32: return Uint32(key, val) case *uint32: return Uint32p(key, val) case []uint32: return Uint32s(key, val) case uint16: return Uint16(key, val) case *uint16: return Uint16p(key, val) case []uint16: return Uint16s(key, val) case uint8: return Uint8(key, val) case *uint8: return Uint8p(key, val) case []byte: return Binary(key, val) case uintptr: return Uintptr(key, val) case *uintptr: return Uintptrp(key, val) case []uintptr: return Uintptrs(key, val) case time.Time: return Time(key, val) case *time.Time: return Timep(key, val) case []time.Time: return Times(key, val) case time.Duration: return Duration(key, val) case *time.Duration: return Durationp(key, val) case []time.Duration: return Durations(key, val) case error: return NamedError(key, val) case []error: return Errors(key, val) case fmt.Stringer: return Stringer(key, val) default: return Reflect(key, val) }}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 富春山居图是中国十大传名画之一这幅画的作者是 富春山居图是中国十大传世名画之一这幅画的作者是什么
- 端午节又称什么 端午节的别称分别是什么
- 提高微服务安全性的11个方法
- 绿茶的保质期,红茶的保质期
- 8款药膳花茶,茶药膳疗法对保健起着不可忽视的作用
- 白福鼎老白茶价格,白茶的价格
- 老公眼里觉得你很“贱”的7个举动
- 男人不同时期想要的女人
- 女人单身太久的十大负面表现
- 女人硬起来其实比男人的还硬