猿人部落吧 关注:16贴子:127
  • 0回复贴,共1

聊聊golang的zap的marshaler

只看楼主收藏回复


本文主要研究一下golang的zap的marshaler
ObjectMarshaler
zap@v1.16.0/zapcore/marshaler.go
type ObjectMarshaler interface { MarshalLogObject(ObjectEncoder) error}// ObjectMarshalerFunc is a type adapter that turns a function into an// ObjectMarshaler.type ObjectMarshalerFunc func(ObjectEncoder) error// MarshalLogObject calls the underlying function.func (f ObjectMarshalerFunc) MarshalLogObject(enc ObjectEncoder) error { return f(enc)}
ObjectMarshaler接口定义了MarshalLogObject方法;ObjectMarshalerFunc类型定义了MarshalLogObject方法,使得ObjectMarshalerFunc实现ObjectMarshaler接口
ArrayMarshaler
zap@v1.16.0/zapcore/marshaler.go
type ArrayMarshaler interface { MarshalLogArray(ArrayEncoder) error}// ArrayMarshalerFunc is a type adapter that turns a function into an// ArrayMarshaler.type ArrayMarshalerFunc func(ArrayEncoder) error// MarshalLogArray calls the underlying function.func (f ArrayMarshalerFunc) MarshalLogArray(enc ArrayEncoder) error { return f(enc)}
ArrayMarshaler接口定义了MarshalLogArray方法;ArrayMarshalerFunc类型定义了MarshalLogArray方法,使得ArrayMarshalerFunc实现了MarshalLogArray接口
ObjectEncoder
zap@v1.16.0/zapcore/encoder.go
type ObjectEncoder interface { // Logging-specific marshalers. AddArray(key string, marshaler ArrayMarshaler) error AddObject(key string, marshaler ObjectMarshaler) error // Built-in types. AddBinary(key string, value []byte) // for arbitrary bytes AddByteString(key string, value []byte) // for UTF-8 encoded bytes AddBool(key string, value bool) AddComplex128(key string, value complex128) AddComplex64(key string, value complex64) AddDuration(key string, value time.Duration) AddFloat64(key string, value float64) AddFloat32(key string, value float32) AddInt(key string, value int) AddInt64(key string, value int64) AddInt32(key string, value int32) AddInt16(key string, value int16) AddInt8(key string, value int8) AddString(key, value string) AddTime(key string, value time.Time) AddUint(key string, value uint) AddUint64(key string, value uint64) AddUint32(key string, value uint32) AddUint16(key string, value uint16) AddUint8(key string, value uint8) AddUintptr(key string, value uintptr) // AddReflected uses reflection to serialize arbitrary objects, so it can be // slow and allocation-heavy. AddReflected(key string, value interface{}) error // OpenNamespace opens an isolated namespace where all subsequent fields will // be added. Applications can use namespaces to prevent key collisions when // injecting loggers into sub-components or third-party libraries. OpenNamespace(key string)}
ObjectEncoder的AddArray方法需要ArrayMarshaler参数,AddObject方法需要ObjectMarshaler参数


1楼2020-12-19 23:56回复