-
Notifications
You must be signed in to change notification settings - Fork 1
/
zerolog.go
132 lines (115 loc) · 3 KB
/
zerolog.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package helper
import (
"bytes"
"io"
"strings"
"sync"
"time"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/errors/errbase"
json "github.com/goccy/go-json"
"github.com/rs/zerolog"
)
var (
zerologHelper *ZerologHelper
zerologHelperOnce sync.Once
)
type ZerologHelper struct{}
func Zerolog() *ZerologHelper {
zerologHelperOnce.Do(func() {
zerologHelper = &ZerologHelper{}
zerologHelper.
SetDefaultGlobalLevel().
SetDefaultGlobalInterfaceMarshalFunc().
SetDefaultGlobalErrorStackMarshaller()
})
return zerologHelper
}
func (h *ZerologHelper) SetDefaultGlobalLevel() *ZerologHelper {
return h.SetGlobalLevel(zerolog.TraceLevel)
}
func (h *ZerologHelper) SetGlobalLevel(level zerolog.Level) *ZerologHelper {
zerolog.SetGlobalLevel(level)
return h
}
func (h *ZerologHelper) SetDefaultGlobalInterfaceMarshalFunc() *ZerologHelper {
return h.SetInterfaceMarshalFunc(func(v any) ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
err := enc.Encode(v)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
})
}
func (h *ZerologHelper) SetInterfaceMarshalFunc(fn func(any) ([]byte, error)) *ZerologHelper {
zerolog.InterfaceMarshalFunc = fn
return h
}
func (h *ZerologHelper) SetDefaultGlobalErrorStackMarshaller() *ZerologHelper {
return h.SetErrorStackMarshaller(func(err error) any {
lastStackTraceErr := err
for ; err != nil; err = errors.Unwrap(err) {
_, ok := err.(errbase.StackTraceProvider)
if ok {
lastStackTraceErr = err
}
}
safeDetails := errors.GetSafeDetails(lastStackTraceErr).SafeDetails
if len(safeDetails) == 1 {
stackMsg, err := parsePII(safeDetails[0])
if err != nil {
return safeDetails[0]
}
return stackMsg
}
res := make([][]StackInfo, 0)
for _, details := range safeDetails {
stackMsg, err := parsePII(details)
if err != nil {
return safeDetails
}
res = append(res, stackMsg)
}
return res
})
}
func (h *ZerologHelper) SetErrorStackMarshaller(fn func(error) any) *ZerologHelper {
zerolog.ErrorStackMarshaler = fn
return h
}
func (h *ZerologHelper) NewLogger(out io.Writer) *zerolog.Logger {
log := zerolog.New(out).With().Timestamp().Caller().Stack().Logger()
return &log
}
func (h *ZerologHelper) NewConsoleLogger() *zerolog.Logger {
return h.NewLogger(h.ConsoleWriter())
}
func (h *ZerologHelper) ConsoleWriter() zerolog.ConsoleWriter {
return zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
w.TimeFormat = time.DateTime + " -0700"
})
}
type StackInfo struct {
Func string `json:"func"`
File string `json:"file"`
}
func parsePII(detail string) ([]StackInfo, error) {
s := strings.TrimSpace(detail)
ss := strings.Split(s, "\n")
if len(ss)%2 != 0 {
return nil, errors.New("invalid PII-free strings")
}
var stackInfos []StackInfo
for i := 0; i < len(ss); i += 2 {
fn := ss[i]
file := ss[i+1]
stackInfos = append(stackInfos, StackInfo{
Func: strings.TrimSpace(fn),
File: strings.TrimSpace(file),
})
}
return stackInfos, nil
}