1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package zapcore
22
23 import (
24 "bytes"
25 "fmt"
26 "math"
27 "reflect"
28 "time"
29 )
30
31
32
33 type FieldType uint8
34
35 const (
36
37 UnknownType FieldType = iota
38
39 ArrayMarshalerType
40
41 ObjectMarshalerType
42
43 BinaryType
44
45 BoolType
46
47 ByteStringType
48
49 Complex128Type
50
51 Complex64Type
52
53 DurationType
54
55 Float64Type
56
57 Float32Type
58
59 Int64Type
60
61 Int32Type
62
63 Int16Type
64
65 Int8Type
66
67 StringType
68
69
70 TimeType
71
72 TimeFullType
73
74 Uint64Type
75
76 Uint32Type
77
78 Uint16Type
79
80 Uint8Type
81
82 UintptrType
83
84
85 ReflectType
86
87
88 NamespaceType
89
90 StringerType
91
92 ErrorType
93
94 SkipType
95
96
97
98 InlineMarshalerType
99 )
100
101
102
103
104 type Field struct {
105 Key string
106 Type FieldType
107 Integer int64
108 String string
109 Interface interface{}
110 }
111
112
113
114 func (f Field) AddTo(enc ObjectEncoder) {
115 var err error
116
117 switch f.Type {
118 case ArrayMarshalerType:
119 err = enc.AddArray(f.Key, f.Interface.(ArrayMarshaler))
120 case ObjectMarshalerType:
121 err = enc.AddObject(f.Key, f.Interface.(ObjectMarshaler))
122 case InlineMarshalerType:
123 err = f.Interface.(ObjectMarshaler).MarshalLogObject(enc)
124 case BinaryType:
125 enc.AddBinary(f.Key, f.Interface.([]byte))
126 case BoolType:
127 enc.AddBool(f.Key, f.Integer == 1)
128 case ByteStringType:
129 enc.AddByteString(f.Key, f.Interface.([]byte))
130 case Complex128Type:
131 enc.AddComplex128(f.Key, f.Interface.(complex128))
132 case Complex64Type:
133 enc.AddComplex64(f.Key, f.Interface.(complex64))
134 case DurationType:
135 enc.AddDuration(f.Key, time.Duration(f.Integer))
136 case Float64Type:
137 enc.AddFloat64(f.Key, math.Float64frombits(uint64(f.Integer)))
138 case Float32Type:
139 enc.AddFloat32(f.Key, math.Float32frombits(uint32(f.Integer)))
140 case Int64Type:
141 enc.AddInt64(f.Key, f.Integer)
142 case Int32Type:
143 enc.AddInt32(f.Key, int32(f.Integer))
144 case Int16Type:
145 enc.AddInt16(f.Key, int16(f.Integer))
146 case Int8Type:
147 enc.AddInt8(f.Key, int8(f.Integer))
148 case StringType:
149 enc.AddString(f.Key, f.String)
150 case TimeType:
151 if f.Interface != nil {
152 enc.AddTime(f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location)))
153 } else {
154
155 enc.AddTime(f.Key, time.Unix(0, f.Integer))
156 }
157 case TimeFullType:
158 enc.AddTime(f.Key, f.Interface.(time.Time))
159 case Uint64Type:
160 enc.AddUint64(f.Key, uint64(f.Integer))
161 case Uint32Type:
162 enc.AddUint32(f.Key, uint32(f.Integer))
163 case Uint16Type:
164 enc.AddUint16(f.Key, uint16(f.Integer))
165 case Uint8Type:
166 enc.AddUint8(f.Key, uint8(f.Integer))
167 case UintptrType:
168 enc.AddUintptr(f.Key, uintptr(f.Integer))
169 case ReflectType:
170 err = enc.AddReflected(f.Key, f.Interface)
171 case NamespaceType:
172 enc.OpenNamespace(f.Key)
173 case StringerType:
174 err = encodeStringer(f.Key, f.Interface, enc)
175 case ErrorType:
176 err = encodeError(f.Key, f.Interface.(error), enc)
177 case SkipType:
178 break
179 default:
180 panic(fmt.Sprintf("unknown field type: %v", f))
181 }
182
183 if err != nil {
184 enc.AddString(fmt.Sprintf("%sError", f.Key), err.Error())
185 }
186 }
187
188
189
190 func (f Field) Equals(other Field) bool {
191 if f.Type != other.Type {
192 return false
193 }
194 if f.Key != other.Key {
195 return false
196 }
197
198 switch f.Type {
199 case BinaryType, ByteStringType:
200 return bytes.Equal(f.Interface.([]byte), other.Interface.([]byte))
201 case ArrayMarshalerType, ObjectMarshalerType, ErrorType, ReflectType:
202 return reflect.DeepEqual(f.Interface, other.Interface)
203 default:
204 return f == other
205 }
206 }
207
208 func addFields(enc ObjectEncoder, fields []Field) {
209 for i := range fields {
210 fields[i].AddTo(enc)
211 }
212 }
213
214 func encodeStringer(key string, stringer interface{}, enc ObjectEncoder) (retErr error) {
215
216
217 defer func() {
218 if err := recover(); err != nil {
219
220
221
222 if v := reflect.ValueOf(stringer); v.Kind() == reflect.Ptr && v.IsNil() {
223 enc.AddString(key, "<nil>")
224 return
225 }
226
227 retErr = fmt.Errorf("PANIC=%v", err)
228 }
229 }()
230
231 enc.AddString(key, stringer.(fmt.Stringer).String())
232 return nil
233 }
234
View as plain text