1 package zerolog
2
3 import (
4 "fmt"
5 "net"
6 "os"
7 "runtime"
8 "sync"
9 "time"
10 )
11
12 var eventPool = &sync.Pool{
13 New: func() interface{} {
14 return &Event{
15 buf: make([]byte, 0, 500),
16 }
17 },
18 }
19
20
21
22 type Event struct {
23 buf []byte
24 w LevelWriter
25 level Level
26 done func(msg string)
27 stack bool
28 ch []Hook
29 skipFrame int
30 }
31
32 func putEvent(e *Event) {
33
34
35
36
37
38
39 const maxSize = 1 << 16
40 if cap(e.buf) > maxSize {
41 return
42 }
43 eventPool.Put(e)
44 }
45
46
47
48 type LogObjectMarshaler interface {
49 MarshalZerologObject(e *Event)
50 }
51
52
53
54 type LogArrayMarshaler interface {
55 MarshalZerologArray(a *Array)
56 }
57
58 func newEvent(w LevelWriter, level Level) *Event {
59 e := eventPool.Get().(*Event)
60 e.buf = e.buf[:0]
61 e.ch = nil
62 e.buf = enc.AppendBeginMarker(e.buf)
63 e.w = w
64 e.level = level
65 e.stack = false
66 e.skipFrame = 0
67 return e
68 }
69
70 func (e *Event) write() (err error) {
71 if e == nil {
72 return nil
73 }
74 if e.level != Disabled {
75 e.buf = enc.AppendEndMarker(e.buf)
76 e.buf = enc.AppendLineBreak(e.buf)
77 if e.w != nil {
78 _, err = e.w.WriteLevel(e.level, e.buf)
79 }
80 }
81 putEvent(e)
82 return
83 }
84
85
86
87 func (e *Event) Enabled() bool {
88 return e != nil && e.level != Disabled
89 }
90
91
92 func (e *Event) Discard() *Event {
93 if e == nil {
94 return e
95 }
96 e.level = Disabled
97 return nil
98 }
99
100
101
102
103
104 func (e *Event) Msg(msg string) {
105 if e == nil {
106 return
107 }
108 e.msg(msg)
109 }
110
111
112
113
114 func (e *Event) Send() {
115 if e == nil {
116 return
117 }
118 e.msg("")
119 }
120
121
122
123
124
125 func (e *Event) Msgf(format string, v ...interface{}) {
126 if e == nil {
127 return
128 }
129 e.msg(fmt.Sprintf(format, v...))
130 }
131
132 func (e *Event) MsgFunc(createMsg func() string) {
133 if e == nil {
134 return
135 }
136 e.msg(createMsg())
137 }
138
139 func (e *Event) msg(msg string) {
140 for _, hook := range e.ch {
141 hook.Run(e, e.level, msg)
142 }
143 if msg != "" {
144 e.buf = enc.AppendString(enc.AppendKey(e.buf, MessageFieldName), msg)
145 }
146 if e.done != nil {
147 defer e.done(msg)
148 }
149 if err := e.write(); err != nil {
150 if ErrorHandler != nil {
151 ErrorHandler(err)
152 } else {
153 fmt.Fprintf(os.Stderr, "zerolog: could not write event: %v\n", err)
154 }
155 }
156 }
157
158
159
160
161 func (e *Event) Fields(fields interface{}) *Event {
162 if e == nil {
163 return e
164 }
165 e.buf = appendFields(e.buf, fields)
166 return e
167 }
168
169
170
171 func (e *Event) Dict(key string, dict *Event) *Event {
172 if e == nil {
173 return e
174 }
175 dict.buf = enc.AppendEndMarker(dict.buf)
176 e.buf = append(enc.AppendKey(e.buf, key), dict.buf...)
177 putEvent(dict)
178 return e
179 }
180
181
182
183
184 func Dict() *Event {
185 return newEvent(nil, 0)
186 }
187
188
189
190
191 func (e *Event) Array(key string, arr LogArrayMarshaler) *Event {
192 if e == nil {
193 return e
194 }
195 e.buf = enc.AppendKey(e.buf, key)
196 var a *Array
197 if aa, ok := arr.(*Array); ok {
198 a = aa
199 } else {
200 a = Arr()
201 arr.MarshalZerologArray(a)
202 }
203 e.buf = a.write(e.buf)
204 return e
205 }
206
207 func (e *Event) appendObject(obj LogObjectMarshaler) {
208 e.buf = enc.AppendBeginMarker(e.buf)
209 obj.MarshalZerologObject(e)
210 e.buf = enc.AppendEndMarker(e.buf)
211 }
212
213
214 func (e *Event) Object(key string, obj LogObjectMarshaler) *Event {
215 if e == nil {
216 return e
217 }
218 e.buf = enc.AppendKey(e.buf, key)
219 if obj == nil {
220 e.buf = enc.AppendNil(e.buf)
221
222 return e
223 }
224
225 e.appendObject(obj)
226 return e
227 }
228
229
230 func (e *Event) Func(f func(e *Event)) *Event {
231 if e != nil && e.Enabled() {
232 f(e)
233 }
234 return e
235 }
236
237
238 func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event {
239 if e == nil {
240 return e
241 }
242 if obj == nil {
243 return e
244 }
245 obj.MarshalZerologObject(e)
246 return e
247 }
248
249
250 func (e *Event) Str(key, val string) *Event {
251 if e == nil {
252 return e
253 }
254 e.buf = enc.AppendString(enc.AppendKey(e.buf, key), val)
255 return e
256 }
257
258
259 func (e *Event) Strs(key string, vals []string) *Event {
260 if e == nil {
261 return e
262 }
263 e.buf = enc.AppendStrings(enc.AppendKey(e.buf, key), vals)
264 return e
265 }
266
267
268
269 func (e *Event) Stringer(key string, val fmt.Stringer) *Event {
270 if e == nil {
271 return e
272 }
273 e.buf = enc.AppendStringer(enc.AppendKey(e.buf, key), val)
274 return e
275 }
276
277
278
279
280 func (e *Event) Stringers(key string, vals []fmt.Stringer) *Event {
281 if e == nil {
282 return e
283 }
284 e.buf = enc.AppendStringers(enc.AppendKey(e.buf, key), vals)
285 return e
286 }
287
288
289
290
291
292 func (e *Event) Bytes(key string, val []byte) *Event {
293 if e == nil {
294 return e
295 }
296 e.buf = enc.AppendBytes(enc.AppendKey(e.buf, key), val)
297 return e
298 }
299
300
301 func (e *Event) Hex(key string, val []byte) *Event {
302 if e == nil {
303 return e
304 }
305 e.buf = enc.AppendHex(enc.AppendKey(e.buf, key), val)
306 return e
307 }
308
309
310
311
312
313 func (e *Event) RawJSON(key string, b []byte) *Event {
314 if e == nil {
315 return e
316 }
317 e.buf = appendJSON(enc.AppendKey(e.buf, key), b)
318 return e
319 }
320
321
322
323 func (e *Event) AnErr(key string, err error) *Event {
324 if e == nil {
325 return e
326 }
327 switch m := ErrorMarshalFunc(err).(type) {
328 case nil:
329 return e
330 case LogObjectMarshaler:
331 return e.Object(key, m)
332 case error:
333 if m == nil || isNilValue(m) {
334 return e
335 } else {
336 return e.Str(key, m.Error())
337 }
338 case string:
339 return e.Str(key, m)
340 default:
341 return e.Interface(key, m)
342 }
343 }
344
345
346
347 func (e *Event) Errs(key string, errs []error) *Event {
348 if e == nil {
349 return e
350 }
351 arr := Arr()
352 for _, err := range errs {
353 switch m := ErrorMarshalFunc(err).(type) {
354 case LogObjectMarshaler:
355 arr = arr.Object(m)
356 case error:
357 arr = arr.Err(m)
358 case string:
359 arr = arr.Str(m)
360 default:
361 arr = arr.Interface(m)
362 }
363 }
364
365 return e.Array(key, arr)
366 }
367
368
369
370
371
372
373
374
375
376 func (e *Event) Err(err error) *Event {
377 if e == nil {
378 return e
379 }
380 if e.stack && ErrorStackMarshaler != nil {
381 switch m := ErrorStackMarshaler(err).(type) {
382 case nil:
383 case LogObjectMarshaler:
384 e.Object(ErrorStackFieldName, m)
385 case error:
386 if m != nil && !isNilValue(m) {
387 e.Str(ErrorStackFieldName, m.Error())
388 }
389 case string:
390 e.Str(ErrorStackFieldName, m)
391 default:
392 e.Interface(ErrorStackFieldName, m)
393 }
394 }
395 return e.AnErr(ErrorFieldName, err)
396 }
397
398
399
400
401 func (e *Event) Stack() *Event {
402 if e != nil {
403 e.stack = true
404 }
405 return e
406 }
407
408
409 func (e *Event) Bool(key string, b bool) *Event {
410 if e == nil {
411 return e
412 }
413 e.buf = enc.AppendBool(enc.AppendKey(e.buf, key), b)
414 return e
415 }
416
417
418 func (e *Event) Bools(key string, b []bool) *Event {
419 if e == nil {
420 return e
421 }
422 e.buf = enc.AppendBools(enc.AppendKey(e.buf, key), b)
423 return e
424 }
425
426
427 func (e *Event) Int(key string, i int) *Event {
428 if e == nil {
429 return e
430 }
431 e.buf = enc.AppendInt(enc.AppendKey(e.buf, key), i)
432 return e
433 }
434
435
436 func (e *Event) Ints(key string, i []int) *Event {
437 if e == nil {
438 return e
439 }
440 e.buf = enc.AppendInts(enc.AppendKey(e.buf, key), i)
441 return e
442 }
443
444
445 func (e *Event) Int8(key string, i int8) *Event {
446 if e == nil {
447 return e
448 }
449 e.buf = enc.AppendInt8(enc.AppendKey(e.buf, key), i)
450 return e
451 }
452
453
454 func (e *Event) Ints8(key string, i []int8) *Event {
455 if e == nil {
456 return e
457 }
458 e.buf = enc.AppendInts8(enc.AppendKey(e.buf, key), i)
459 return e
460 }
461
462
463 func (e *Event) Int16(key string, i int16) *Event {
464 if e == nil {
465 return e
466 }
467 e.buf = enc.AppendInt16(enc.AppendKey(e.buf, key), i)
468 return e
469 }
470
471
472 func (e *Event) Ints16(key string, i []int16) *Event {
473 if e == nil {
474 return e
475 }
476 e.buf = enc.AppendInts16(enc.AppendKey(e.buf, key), i)
477 return e
478 }
479
480
481 func (e *Event) Int32(key string, i int32) *Event {
482 if e == nil {
483 return e
484 }
485 e.buf = enc.AppendInt32(enc.AppendKey(e.buf, key), i)
486 return e
487 }
488
489
490 func (e *Event) Ints32(key string, i []int32) *Event {
491 if e == nil {
492 return e
493 }
494 e.buf = enc.AppendInts32(enc.AppendKey(e.buf, key), i)
495 return e
496 }
497
498
499 func (e *Event) Int64(key string, i int64) *Event {
500 if e == nil {
501 return e
502 }
503 e.buf = enc.AppendInt64(enc.AppendKey(e.buf, key), i)
504 return e
505 }
506
507
508 func (e *Event) Ints64(key string, i []int64) *Event {
509 if e == nil {
510 return e
511 }
512 e.buf = enc.AppendInts64(enc.AppendKey(e.buf, key), i)
513 return e
514 }
515
516
517 func (e *Event) Uint(key string, i uint) *Event {
518 if e == nil {
519 return e
520 }
521 e.buf = enc.AppendUint(enc.AppendKey(e.buf, key), i)
522 return e
523 }
524
525
526 func (e *Event) Uints(key string, i []uint) *Event {
527 if e == nil {
528 return e
529 }
530 e.buf = enc.AppendUints(enc.AppendKey(e.buf, key), i)
531 return e
532 }
533
534
535 func (e *Event) Uint8(key string, i uint8) *Event {
536 if e == nil {
537 return e
538 }
539 e.buf = enc.AppendUint8(enc.AppendKey(e.buf, key), i)
540 return e
541 }
542
543
544 func (e *Event) Uints8(key string, i []uint8) *Event {
545 if e == nil {
546 return e
547 }
548 e.buf = enc.AppendUints8(enc.AppendKey(e.buf, key), i)
549 return e
550 }
551
552
553 func (e *Event) Uint16(key string, i uint16) *Event {
554 if e == nil {
555 return e
556 }
557 e.buf = enc.AppendUint16(enc.AppendKey(e.buf, key), i)
558 return e
559 }
560
561
562 func (e *Event) Uints16(key string, i []uint16) *Event {
563 if e == nil {
564 return e
565 }
566 e.buf = enc.AppendUints16(enc.AppendKey(e.buf, key), i)
567 return e
568 }
569
570
571 func (e *Event) Uint32(key string, i uint32) *Event {
572 if e == nil {
573 return e
574 }
575 e.buf = enc.AppendUint32(enc.AppendKey(e.buf, key), i)
576 return e
577 }
578
579
580 func (e *Event) Uints32(key string, i []uint32) *Event {
581 if e == nil {
582 return e
583 }
584 e.buf = enc.AppendUints32(enc.AppendKey(e.buf, key), i)
585 return e
586 }
587
588
589 func (e *Event) Uint64(key string, i uint64) *Event {
590 if e == nil {
591 return e
592 }
593 e.buf = enc.AppendUint64(enc.AppendKey(e.buf, key), i)
594 return e
595 }
596
597
598 func (e *Event) Uints64(key string, i []uint64) *Event {
599 if e == nil {
600 return e
601 }
602 e.buf = enc.AppendUints64(enc.AppendKey(e.buf, key), i)
603 return e
604 }
605
606
607 func (e *Event) Float32(key string, f float32) *Event {
608 if e == nil {
609 return e
610 }
611 e.buf = enc.AppendFloat32(enc.AppendKey(e.buf, key), f)
612 return e
613 }
614
615
616 func (e *Event) Floats32(key string, f []float32) *Event {
617 if e == nil {
618 return e
619 }
620 e.buf = enc.AppendFloats32(enc.AppendKey(e.buf, key), f)
621 return e
622 }
623
624
625 func (e *Event) Float64(key string, f float64) *Event {
626 if e == nil {
627 return e
628 }
629 e.buf = enc.AppendFloat64(enc.AppendKey(e.buf, key), f)
630 return e
631 }
632
633
634 func (e *Event) Floats64(key string, f []float64) *Event {
635 if e == nil {
636 return e
637 }
638 e.buf = enc.AppendFloats64(enc.AppendKey(e.buf, key), f)
639 return e
640 }
641
642
643
644
645
646
647 func (e *Event) Timestamp() *Event {
648 if e == nil {
649 return e
650 }
651 e.buf = enc.AppendTime(enc.AppendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat)
652 return e
653 }
654
655
656 func (e *Event) Time(key string, t time.Time) *Event {
657 if e == nil {
658 return e
659 }
660 e.buf = enc.AppendTime(enc.AppendKey(e.buf, key), t, TimeFieldFormat)
661 return e
662 }
663
664
665 func (e *Event) Times(key string, t []time.Time) *Event {
666 if e == nil {
667 return e
668 }
669 e.buf = enc.AppendTimes(enc.AppendKey(e.buf, key), t, TimeFieldFormat)
670 return e
671 }
672
673
674
675
676 func (e *Event) Dur(key string, d time.Duration) *Event {
677 if e == nil {
678 return e
679 }
680 e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
681 return e
682 }
683
684
685
686
687 func (e *Event) Durs(key string, d []time.Duration) *Event {
688 if e == nil {
689 return e
690 }
691 e.buf = enc.AppendDurations(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
692 return e
693 }
694
695
696
697
698 func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event {
699 if e == nil {
700 return e
701 }
702 var d time.Duration
703 if t.After(start) {
704 d = t.Sub(start)
705 }
706 e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
707 return e
708 }
709
710
711 func (e *Event) Interface(key string, i interface{}) *Event {
712 if e == nil {
713 return e
714 }
715 if obj, ok := i.(LogObjectMarshaler); ok {
716 return e.Object(key, obj)
717 }
718 e.buf = enc.AppendInterface(enc.AppendKey(e.buf, key), i)
719 return e
720 }
721
722
723
724 func (e *Event) CallerSkipFrame(skip int) *Event {
725 if e == nil {
726 return e
727 }
728 e.skipFrame += skip
729 return e
730 }
731
732
733
734
735 func (e *Event) Caller(skip ...int) *Event {
736 sk := CallerSkipFrameCount
737 if len(skip) > 0 {
738 sk = skip[0] + CallerSkipFrameCount
739 }
740 return e.caller(sk)
741 }
742
743 func (e *Event) caller(skip int) *Event {
744 if e == nil {
745 return e
746 }
747 pc, file, line, ok := runtime.Caller(skip + e.skipFrame)
748 if !ok {
749 return e
750 }
751 e.buf = enc.AppendString(enc.AppendKey(e.buf, CallerFieldName), CallerMarshalFunc(pc, file, line))
752 return e
753 }
754
755
756 func (e *Event) IPAddr(key string, ip net.IP) *Event {
757 if e == nil {
758 return e
759 }
760 e.buf = enc.AppendIPAddr(enc.AppendKey(e.buf, key), ip)
761 return e
762 }
763
764
765 func (e *Event) IPPrefix(key string, pfx net.IPNet) *Event {
766 if e == nil {
767 return e
768 }
769 e.buf = enc.AppendIPPrefix(enc.AppendKey(e.buf, key), pfx)
770 return e
771 }
772
773
774 func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event {
775 if e == nil {
776 return e
777 }
778 e.buf = enc.AppendMACAddr(enc.AppendKey(e.buf, key), ha)
779 return e
780 }
781
View as plain text