Documentation
¶
Overview ¶
Package zaptext provides text-based encoding for structured logging. The ReflectEncoder provides reflection-based encoding of arbitrary Go data structures into a JSON-like format, with object pooling for performance optimization.
Index ¶
- Constants
- func CustomTimeEncoderFactory(layout string) zapcore.TimeEncoder
- func NewTextEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder
- type ReflectEncoder
- type TextEncoder
- func (enc *TextEncoder) AddArray(key string, marshaler zapcore.ArrayMarshaler) (err error)
- func (enc *TextEncoder) AddBinary(key string, value []byte)
- func (enc *TextEncoder) AddBool(key string, value bool)
- func (enc *TextEncoder) AddByteString(key string, value []byte)
- func (enc *TextEncoder) AddComplex128(key string, value complex128)
- func (enc *TextEncoder) AddComplex64(key string, value complex64)
- func (enc *TextEncoder) AddDuration(key string, value time.Duration)
- func (enc *TextEncoder) AddFloat32(key string, value float32)
- func (enc *TextEncoder) AddFloat64(key string, value float64)
- func (enc *TextEncoder) AddInt(key string, value int)
- func (enc *TextEncoder) AddInt16(key string, value int16)
- func (enc *TextEncoder) AddInt32(key string, value int32)
- func (enc *TextEncoder) AddInt64(key string, value int64)
- func (enc *TextEncoder) AddInt8(key string, value int8)
- func (enc *TextEncoder) AddObject(key string, marshaler zapcore.ObjectMarshaler) (err error)
- func (enc *TextEncoder) AddReflected(key string, value any) (err error)
- func (enc *TextEncoder) AddString(key, value string)
- func (enc *TextEncoder) AddTime(key string, value time.Time)
- func (enc *TextEncoder) AddUint(key string, value uint)
- func (enc *TextEncoder) AddUint16(key string, value uint16)
- func (enc *TextEncoder) AddUint32(key string, value uint32)
- func (enc *TextEncoder) AddUint64(key string, value uint64)
- func (enc *TextEncoder) AddUint8(key string, value uint8)
- func (enc *TextEncoder) AddUintptr(key string, value uintptr)
- func (enc *TextEncoder) AppendArray(arr zapcore.ArrayMarshaler) (err error)
- func (enc *TextEncoder) AppendBool(value bool)
- func (enc *TextEncoder) AppendByteString(value []byte)
- func (enc *TextEncoder) AppendComplex128(value complex128)
- func (enc *TextEncoder) AppendComplex64(value complex64)
- func (enc *TextEncoder) AppendDuration(value time.Duration)
- func (enc *TextEncoder) AppendFloat32(value float32)
- func (enc *TextEncoder) AppendFloat64(value float64)
- func (enc *TextEncoder) AppendInt(value int)
- func (enc *TextEncoder) AppendInt16(value int16)
- func (enc *TextEncoder) AppendInt32(value int32)
- func (enc *TextEncoder) AppendInt64(value int64)
- func (enc *TextEncoder) AppendInt8(value int8)
- func (enc *TextEncoder) AppendObject(obj zapcore.ObjectMarshaler) (err error)
- func (enc *TextEncoder) AppendReflected(value any) (err error)
- func (enc *TextEncoder) AppendString(value string)
- func (enc *TextEncoder) AppendTime(value time.Time)
- func (enc *TextEncoder) AppendUint(value uint)
- func (enc *TextEncoder) AppendUint16(value uint16)
- func (enc *TextEncoder) AppendUint32(value uint32)
- func (enc *TextEncoder) AppendUint64(value uint64)
- func (enc *TextEncoder) AppendUint8(value uint8)
- func (enc *TextEncoder) AppendUintptr(value uintptr)
- func (enc *TextEncoder) Clone() zapcore.Encoder
- func (enc *TextEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (buf *buffer.Buffer, err error)
- func (enc *TextEncoder) OpenNamespace(key string)
Constants ¶
const (
// DefaultMaxDepth is the default maximum recursion depth to prevent infinite loops
DefaultMaxDepth = 32
)
Variables ¶
This section is empty.
Functions ¶
func CustomTimeEncoderFactory ¶
func CustomTimeEncoderFactory(layout string) zapcore.TimeEncoder
CustomTimeEncoderFactory return a zapcore.TimeEncoder format time with custom layout
func NewTextEncoder ¶
func NewTextEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder
Types ¶
type ReflectEncoder ¶
type ReflectEncoder struct {
// contains filtered or unexported fields
}
ReflectEncoder provides reflection-based encoding of Go data structures into JSON-like format. It supports object pooling for performance optimization and includes protection against infinite recursion. The encoder handles all Go primitive types, compound types (arrays, slices, maps, structs), and special cases like time.Time formatting.
Example usage:
encoder := zaptext.NewReflectEncoder(os.Stdout) defer encoder.Release() // Return to pool for reuse err := encoder.Encode(myStruct)
func NewReflectEncoder ¶
func NewReflectEncoder(w io.Writer) *ReflectEncoder
NewReflectEncoder creates a new ReflectEncoder from the object pool. The encoder is configured to write to the provided io.Writer. Always call Release() when done to return the encoder to the pool.
func (*ReflectEncoder) Encode ¶
func (enc *ReflectEncoder) Encode(obj any) error
Encode encodes the given object using reflection into JSON-like format. It handles all Go primitive types, compound types, and includes special handling for time.Time. The method includes protection against infinite recursion with a maximum depth limit. If an error occurs, the encoder maintains error state for subsequent calls.
func (*ReflectEncoder) Release ¶ added in v0.0.2
func (enc *ReflectEncoder) Release()
Release returns the encoder back to the pool for reuse. This should always be called when done with an encoder to optimize memory usage.
func (*ReflectEncoder) SetEscapeHTML ¶ added in v0.0.2
func (enc *ReflectEncoder) SetEscapeHTML(escape bool)
SetEscapeHTML configures whether to escape HTML characters in strings. When enabled (default), characters like <, >, and & are escaped to their HTML entities. This is useful when the output might be embedded in HTML documents.
func (*ReflectEncoder) SetMaxDepth ¶ added in v0.0.2
func (enc *ReflectEncoder) SetMaxDepth(depth int)
SetMaxDepth configures the maximum recursion depth to prevent infinite loops. The default value is 32. Setting a lower value may prevent encoding of deeply nested structures, while higher values may risk stack overflow with circular references.
type TextEncoder ¶
type TextEncoder struct {
*zapcore.EncoderConfig
// contains filtered or unexported fields
}
func (*TextEncoder) AddArray ¶
func (enc *TextEncoder) AddArray(key string, marshaler zapcore.ArrayMarshaler) (err error)
Logging-specific marshalers.
func (*TextEncoder) AddBinary ¶
func (enc *TextEncoder) AddBinary(key string, value []byte)
Built-in types. for arbitrary bytes
func (*TextEncoder) AddBool ¶
func (enc *TextEncoder) AddBool(key string, value bool)
func (*TextEncoder) AddByteString ¶
func (enc *TextEncoder) AddByteString(key string, value []byte)
func (*TextEncoder) AddComplex128 ¶
func (enc *TextEncoder) AddComplex128(key string, value complex128)
func (*TextEncoder) AddComplex64 ¶
func (enc *TextEncoder) AddComplex64(key string, value complex64)
func (*TextEncoder) AddDuration ¶
func (enc *TextEncoder) AddDuration(key string, value time.Duration)
func (*TextEncoder) AddFloat32 ¶
func (enc *TextEncoder) AddFloat32(key string, value float32)
func (*TextEncoder) AddFloat64 ¶
func (enc *TextEncoder) AddFloat64(key string, value float64)
func (*TextEncoder) AddInt ¶
func (enc *TextEncoder) AddInt(key string, value int)
func (*TextEncoder) AddInt16 ¶
func (enc *TextEncoder) AddInt16(key string, value int16)
func (*TextEncoder) AddInt32 ¶
func (enc *TextEncoder) AddInt32(key string, value int32)
func (*TextEncoder) AddInt64 ¶
func (enc *TextEncoder) AddInt64(key string, value int64)
func (*TextEncoder) AddInt8 ¶
func (enc *TextEncoder) AddInt8(key string, value int8)
func (*TextEncoder) AddObject ¶
func (enc *TextEncoder) AddObject(key string, marshaler zapcore.ObjectMarshaler) (err error)
func (*TextEncoder) AddReflected ¶
func (enc *TextEncoder) AddReflected(key string, value any) (err error)
AddReflected uses reflection to serialize arbitrary objects, so it can be slow and allocation-heavy.
func (*TextEncoder) AddString ¶
func (enc *TextEncoder) AddString(key, value string)
func (*TextEncoder) AddUint ¶
func (enc *TextEncoder) AddUint(key string, value uint)
func (*TextEncoder) AddUint16 ¶
func (enc *TextEncoder) AddUint16(key string, value uint16)
func (*TextEncoder) AddUint32 ¶
func (enc *TextEncoder) AddUint32(key string, value uint32)
func (*TextEncoder) AddUint64 ¶
func (enc *TextEncoder) AddUint64(key string, value uint64)
func (*TextEncoder) AddUint8 ¶
func (enc *TextEncoder) AddUint8(key string, value uint8)
func (*TextEncoder) AddUintptr ¶
func (enc *TextEncoder) AddUintptr(key string, value uintptr)
func (*TextEncoder) AppendArray ¶
func (enc *TextEncoder) AppendArray(arr zapcore.ArrayMarshaler) (err error)
Logging-specific marshalers.{}
func (*TextEncoder) AppendBool ¶
func (enc *TextEncoder) AppendBool(value bool)
func (*TextEncoder) AppendByteString ¶
func (enc *TextEncoder) AppendByteString(value []byte)
for UTF-8 encoded bytes
func (*TextEncoder) AppendComplex128 ¶
func (enc *TextEncoder) AppendComplex128(value complex128)
func (*TextEncoder) AppendComplex64 ¶
func (enc *TextEncoder) AppendComplex64(value complex64)
func (*TextEncoder) AppendDuration ¶
func (enc *TextEncoder) AppendDuration(value time.Duration)
Time-related types.
func (*TextEncoder) AppendFloat32 ¶
func (enc *TextEncoder) AppendFloat32(value float32)
func (*TextEncoder) AppendFloat64 ¶
func (enc *TextEncoder) AppendFloat64(value float64)
func (*TextEncoder) AppendInt ¶
func (enc *TextEncoder) AppendInt(value int)
func (*TextEncoder) AppendInt16 ¶
func (enc *TextEncoder) AppendInt16(value int16)
func (*TextEncoder) AppendInt32 ¶
func (enc *TextEncoder) AppendInt32(value int32)
func (*TextEncoder) AppendInt64 ¶
func (enc *TextEncoder) AppendInt64(value int64)
func (*TextEncoder) AppendInt8 ¶
func (enc *TextEncoder) AppendInt8(value int8)
func (*TextEncoder) AppendObject ¶
func (enc *TextEncoder) AppendObject(obj zapcore.ObjectMarshaler) (err error)
func (*TextEncoder) AppendReflected ¶
func (enc *TextEncoder) AppendReflected(value any) (err error)
AppendReflected uses reflection to serialize arbitrary objects, so it's slow and allocation-heavy.
func (*TextEncoder) AppendString ¶
func (enc *TextEncoder) AppendString(value string)
func (*TextEncoder) AppendTime ¶
func (enc *TextEncoder) AppendTime(value time.Time)
func (*TextEncoder) AppendUint ¶
func (enc *TextEncoder) AppendUint(value uint)
func (*TextEncoder) AppendUint16 ¶
func (enc *TextEncoder) AppendUint16(value uint16)
func (*TextEncoder) AppendUint32 ¶
func (enc *TextEncoder) AppendUint32(value uint32)
func (*TextEncoder) AppendUint64 ¶
func (enc *TextEncoder) AppendUint64(value uint64)
func (*TextEncoder) AppendUint8 ¶
func (enc *TextEncoder) AppendUint8(value uint8)
func (*TextEncoder) AppendUintptr ¶
func (enc *TextEncoder) AppendUintptr(value uintptr)
func (*TextEncoder) Clone ¶
func (enc *TextEncoder) Clone() zapcore.Encoder
Clone copies the encoder, ensuring that adding fields to the copy doesn't affect the original.
func (*TextEncoder) EncodeEntry ¶
func (enc *TextEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (buf *buffer.Buffer, err error)
EncodeEntry encodes an entry and fields, along with any accumulated context, into a byte buffer and returns it. Any fields that are empty, including fields on the `Entry` type, should be omitted.
func (*TextEncoder) OpenNamespace ¶
func (enc *TextEncoder) OpenNamespace(key string)
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.