package zap
import (
"fmt"
"math"
"time"
"go.uber.org/zap/internal/stacktrace"
"go.uber.org/zap/zapcore"
)
type Field = zapcore .Field
var (
_minTimeInt64 = time .Unix (0 , math .MinInt64 )
_maxTimeInt64 = time .Unix (0 , math .MaxInt64 )
)
func Skip () Field {
return Field {Type : zapcore .SkipType }
}
func nilField (key string ) Field { return Reflect (key , nil ) }
func Binary (key string , val []byte ) Field {
return Field {Key : key , Type : zapcore .BinaryType , Interface : val }
}
func Bool (key string , val bool ) Field {
var ival int64
if val {
ival = 1
}
return Field {Key : key , Type : zapcore .BoolType , Integer : ival }
}
func Boolp (key string , val *bool ) Field {
if val == nil {
return nilField (key )
}
return Bool (key , *val )
}
func ByteString (key string , val []byte ) Field {
return Field {Key : key , Type : zapcore .ByteStringType , Interface : val }
}
func Complex128 (key string , val complex128 ) Field {
return Field {Key : key , Type : zapcore .Complex128Type , Interface : val }
}
func Complex128p (key string , val *complex128 ) Field {
if val == nil {
return nilField (key )
}
return Complex128 (key , *val )
}
func Complex64 (key string , val complex64 ) Field {
return Field {Key : key , Type : zapcore .Complex64Type , Interface : val }
}
func Complex64p (key string , val *complex64 ) Field {
if val == nil {
return nilField (key )
}
return Complex64 (key , *val )
}
func Float64 (key string , val float64 ) Field {
return Field {Key : key , Type : zapcore .Float64Type , Integer : int64 (math .Float64bits (val ))}
}
func Float64p (key string , val *float64 ) Field {
if val == nil {
return nilField (key )
}
return Float64 (key , *val )
}
func Float32 (key string , val float32 ) Field {
return Field {Key : key , Type : zapcore .Float32Type , Integer : int64 (math .Float32bits (val ))}
}
func Float32p (key string , val *float32 ) Field {
if val == nil {
return nilField (key )
}
return Float32 (key , *val )
}
func Int (key string , val int ) Field {
return Int64 (key , int64 (val ))
}
func Intp (key string , val *int ) Field {
if val == nil {
return nilField (key )
}
return Int (key , *val )
}
func Int64 (key string , val int64 ) Field {
return Field {Key : key , Type : zapcore .Int64Type , Integer : val }
}
func Int64p (key string , val *int64 ) Field {
if val == nil {
return nilField (key )
}
return Int64 (key , *val )
}
func Int32 (key string , val int32 ) Field {
return Field {Key : key , Type : zapcore .Int32Type , Integer : int64 (val )}
}
func Int32p (key string , val *int32 ) Field {
if val == nil {
return nilField (key )
}
return Int32 (key , *val )
}
func Int16 (key string , val int16 ) Field {
return Field {Key : key , Type : zapcore .Int16Type , Integer : int64 (val )}
}
func Int16p (key string , val *int16 ) Field {
if val == nil {
return nilField (key )
}
return Int16 (key , *val )
}
func Int8 (key string , val int8 ) Field {
return Field {Key : key , Type : zapcore .Int8Type , Integer : int64 (val )}
}
func Int8p (key string , val *int8 ) Field {
if val == nil {
return nilField (key )
}
return Int8 (key , *val )
}
func String (key string , val string ) Field {
return Field {Key : key , Type : zapcore .StringType , String : val }
}
func Stringp (key string , val *string ) Field {
if val == nil {
return nilField (key )
}
return String (key , *val )
}
func Uint (key string , val uint ) Field {
return Uint64 (key , uint64 (val ))
}
func Uintp (key string , val *uint ) Field {
if val == nil {
return nilField (key )
}
return Uint (key , *val )
}
func Uint64 (key string , val uint64 ) Field {
return Field {Key : key , Type : zapcore .Uint64Type , Integer : int64 (val )}
}
func Uint64p (key string , val *uint64 ) Field {
if val == nil {
return nilField (key )
}
return Uint64 (key , *val )
}
func Uint32 (key string , val uint32 ) Field {
return Field {Key : key , Type : zapcore .Uint32Type , Integer : int64 (val )}
}
func Uint32p (key string , val *uint32 ) Field {
if val == nil {
return nilField (key )
}
return Uint32 (key , *val )
}
func Uint16 (key string , val uint16 ) Field {
return Field {Key : key , Type : zapcore .Uint16Type , Integer : int64 (val )}
}
func Uint16p (key string , val *uint16 ) Field {
if val == nil {
return nilField (key )
}
return Uint16 (key , *val )
}
func Uint8 (key string , val uint8 ) Field {
return Field {Key : key , Type : zapcore .Uint8Type , Integer : int64 (val )}
}
func Uint8p (key string , val *uint8 ) Field {
if val == nil {
return nilField (key )
}
return Uint8 (key , *val )
}
func Uintptr (key string , val uintptr ) Field {
return Field {Key : key , Type : zapcore .UintptrType , Integer : int64 (val )}
}
func Uintptrp (key string , val *uintptr ) Field {
if val == nil {
return nilField (key )
}
return Uintptr (key , *val )
}
func Reflect (key string , val interface {}) Field {
return Field {Key : key , Type : zapcore .ReflectType , Interface : val }
}
func Namespace (key string ) Field {
return Field {Key : key , Type : zapcore .NamespaceType }
}
func Stringer (key string , val fmt .Stringer ) Field {
return Field {Key : key , Type : zapcore .StringerType , Interface : val }
}
func Time (key string , val time .Time ) Field {
if val .Before (_minTimeInt64 ) || val .After (_maxTimeInt64 ) {
return Field {Key : key , Type : zapcore .TimeFullType , Interface : val }
}
return Field {Key : key , Type : zapcore .TimeType , Integer : val .UnixNano (), Interface : val .Location ()}
}
func Timep (key string , val *time .Time ) Field {
if val == nil {
return nilField (key )
}
return Time (key , *val )
}
func Stack (key string ) Field {
return StackSkip (key , 1 )
}
func StackSkip (key string , skip int ) Field {
return String (key , stacktrace .Take (skip +1 ))
}
func Duration (key string , val time .Duration ) Field {
return Field {Key : key , Type : zapcore .DurationType , Integer : int64 (val )}
}
func Durationp (key string , val *time .Duration ) Field {
if val == nil {
return nilField (key )
}
return Duration (key , *val )
}
func Object (key string , val zapcore .ObjectMarshaler ) Field {
return Field {Key : key , Type : zapcore .ObjectMarshalerType , Interface : val }
}
func Inline (val zapcore .ObjectMarshaler ) Field {
return zapcore .Field {
Type : zapcore .InlineMarshalerType ,
Interface : val ,
}
}
func Dict (key string , val ...Field ) Field {
return dictField (key , val )
}
func dictField (key string , val []Field ) Field {
return Object (key , dictObject (val ))
}
type dictObject []Field
func (d dictObject ) MarshalLogObject (enc zapcore .ObjectEncoder ) error {
for _ , f := range d {
f .AddTo (enc )
}
return nil
}
type anyFieldC [T any ] func (string , T ) Field
func (f anyFieldC [T ]) Any (key string , val any ) Field {
v , _ := val .(T )
return f (key , v )
}
func Any (key string , value interface {}) Field {
var c interface { Any (string , any ) Field }
switch value .(type ) {
case zapcore .ObjectMarshaler :
c = anyFieldC [zapcore .ObjectMarshaler ](Object )
case zapcore .ArrayMarshaler :
c = anyFieldC [zapcore .ArrayMarshaler ](Array )
case []Field :
c = anyFieldC [[]Field ](dictField )
case bool :
c = anyFieldC [bool ](Bool )
case *bool :
c = anyFieldC [*bool ](Boolp )
case []bool :
c = anyFieldC [[]bool ](Bools )
case complex128 :
c = anyFieldC [complex128 ](Complex128 )
case *complex128 :
c = anyFieldC [*complex128 ](Complex128p )
case []complex128 :
c = anyFieldC [[]complex128 ](Complex128s )
case complex64 :
c = anyFieldC [complex64 ](Complex64 )
case *complex64 :
c = anyFieldC [*complex64 ](Complex64p )
case []complex64 :
c = anyFieldC [[]complex64 ](Complex64s )
case float64 :
c = anyFieldC [float64 ](Float64 )
case *float64 :
c = anyFieldC [*float64 ](Float64p )
case []float64 :
c = anyFieldC [[]float64 ](Float64s )
case float32 :
c = anyFieldC [float32 ](Float32 )
case *float32 :
c = anyFieldC [*float32 ](Float32p )
case []float32 :
c = anyFieldC [[]float32 ](Float32s )
case int :
c = anyFieldC [int ](Int )
case *int :
c = anyFieldC [*int ](Intp )
case []int :
c = anyFieldC [[]int ](Ints )
case int64 :
c = anyFieldC [int64 ](Int64 )
case *int64 :
c = anyFieldC [*int64 ](Int64p )
case []int64 :
c = anyFieldC [[]int64 ](Int64s )
case int32 :
c = anyFieldC [int32 ](Int32 )
case *int32 :
c = anyFieldC [*int32 ](Int32p )
case []int32 :
c = anyFieldC [[]int32 ](Int32s )
case int16 :
c = anyFieldC [int16 ](Int16 )
case *int16 :
c = anyFieldC [*int16 ](Int16p )
case []int16 :
c = anyFieldC [[]int16 ](Int16s )
case int8 :
c = anyFieldC [int8 ](Int8 )
case *int8 :
c = anyFieldC [*int8 ](Int8p )
case []int8 :
c = anyFieldC [[]int8 ](Int8s )
case string :
c = anyFieldC [string ](String )
case *string :
c = anyFieldC [*string ](Stringp )
case []string :
c = anyFieldC [[]string ](Strings )
case uint :
c = anyFieldC [uint ](Uint )
case *uint :
c = anyFieldC [*uint ](Uintp )
case []uint :
c = anyFieldC [[]uint ](Uints )
case uint64 :
c = anyFieldC [uint64 ](Uint64 )
case *uint64 :
c = anyFieldC [*uint64 ](Uint64p )
case []uint64 :
c = anyFieldC [[]uint64 ](Uint64s )
case uint32 :
c = anyFieldC [uint32 ](Uint32 )
case *uint32 :
c = anyFieldC [*uint32 ](Uint32p )
case []uint32 :
c = anyFieldC [[]uint32 ](Uint32s )
case uint16 :
c = anyFieldC [uint16 ](Uint16 )
case *uint16 :
c = anyFieldC [*uint16 ](Uint16p )
case []uint16 :
c = anyFieldC [[]uint16 ](Uint16s )
case uint8 :
c = anyFieldC [uint8 ](Uint8 )
case *uint8 :
c = anyFieldC [*uint8 ](Uint8p )
case []byte :
c = anyFieldC [[]byte ](Binary )
case uintptr :
c = anyFieldC [uintptr ](Uintptr )
case *uintptr :
c = anyFieldC [*uintptr ](Uintptrp )
case []uintptr :
c = anyFieldC [[]uintptr ](Uintptrs )
case time .Time :
c = anyFieldC [time .Time ](Time )
case *time .Time :
c = anyFieldC [*time .Time ](Timep )
case []time .Time :
c = anyFieldC [[]time .Time ](Times )
case time .Duration :
c = anyFieldC [time .Duration ](Duration )
case *time .Duration :
c = anyFieldC [*time .Duration ](Durationp )
case []time .Duration :
c = anyFieldC [[]time .Duration ](Durations )
case error :
c = anyFieldC [error ](NamedError )
case []error :
c = anyFieldC [[]error ](Errors )
case fmt .Stringer :
c = anyFieldC [fmt .Stringer ](Stringer )
default :
c = anyFieldC [any ](Reflect )
}
return c .Any (key , value )
}
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 .