/
entry.go
42 lines (37 loc) · 889 Bytes
/
entry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package maleozap
import (
"time"
"go.uber.org/zap/zapcore"
"github.com/tigorlazuardi/maleo"
)
type Entry struct {
maleo.Entry
}
func (e Entry) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("level", e.Level().String())
enc.AddString("message", e.Message())
if time.Since(e.Time()) > time.Second {
enc.AddTime("time", e.Time())
}
if key := e.Key(); key != "" {
enc.AddString("key", key)
}
_ = enc.AddObject("caller", Caller{e.Caller()})
if ctx := e.Context(); len(ctx) > 0 {
if len(ctx) == 1 {
err := encodeContextObject(enc, ctx[0])
if err != nil {
enc.AddString("context", err.Error())
}
} else {
fields := toZapFields(ctx)
_ = enc.AddObject("context", zapcore.ObjectMarshalerFunc(func(oe zapcore.ObjectEncoder) error {
for _, field := range fields {
field.AddTo(oe)
}
return nil
}))
}
}
return nil
}