-
Notifications
You must be signed in to change notification settings - Fork 1
/
zapper.go
165 lines (133 loc) · 3.3 KB
/
zapper.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package zapper
import (
"github.com/TheZeroSlave/zapsentry"
"github.com/getsentry/sentry-go"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type (
logFunc func(zap *zap.SugaredLogger, args ...any)
logfFunc func(zap *zap.SugaredLogger, msg string, args ...any)
logKvFunc func(zap *zap.SugaredLogger, msg string, keyAndValues ...any)
)
type Zapper interface {
Caller
NewCore(...Core) error
GetServiceCode() uint
GetServiceName() string
GetZap() *zap.Logger
GetSentryClient() *sentry.Client
}
type Zap struct {
zap *zap.Logger
sugar *zap.SugaredLogger
cores []zapcore.Core
sentryCore zapcore.Core
sentryClient *sentry.Client
development bool
timeFormat TimeFormat
stackLevel Level
level func(lvl zapcore.Level) bool
service *service
debug logFunc
debugF logfFunc
debugW logKvFunc
info logFunc
infoF logfFunc
infoW logKvFunc
warn logFunc
warnF logfFunc
warnW logKvFunc
error logFunc
errorF logfFunc
errorW logKvFunc
dPanic logFunc
dPanicF logfFunc
dPanicW logKvFunc
panic logFunc
panicF logfFunc
panicW logKvFunc
fatal logFunc
fatalF logfFunc
fatalW logKvFunc
}
type service struct {
Code uint
Name string
}
// New create new Zap object
func New(development bool, opts ...Option) Zapper {
zapper := new(Zap)
zapper.development = development
zapper.service = new(service)
zapper.stackLevel = Error
zapper.level = func(lvl zapcore.Level) bool {
return lvl >= Debug.zapLevel()
}
for _, opt := range opts {
opt(zapper)
}
zapper.callersLoader()
return zapper
}
// NewCore create cores for zapper
func (z *Zap) NewCore(cores ...Core) error {
if len(cores) == 0 {
cores = append(cores, _defaultCore())
}
useSentry := false
for _, c := range cores {
s, ok := c.(*core)
if ok && s.coreType == SENTRY {
useSentry = true
}
if err := c.init(z); err != nil {
return err
}
}
z.zap = zap.New(zapcore.NewTee(z.cores...), zap.AddCaller(), zap.AddCallerSkip(1), zap.AddStacktrace(z.stackLevel.zapLevel()))
if useSentry {
z.zap = zapsentry.AttachCoreToLogger(z.sentryCore, z.zap)
z.zap = z.zap.With(zapsentry.NewScope())
}
z.sugar = z.zap.Sugar()
return nil
}
func (z *Zap) GetServiceCode() uint {
return z.service.Code
}
func (z *Zap) GetServiceName() string {
return z.service.Name
}
func (z *Zap) GetZap() *zap.Logger {
return z.zap
}
func (z *Zap) GetSentryClient() *sentry.Client {
return z.sentryClient
}
func (z *Zap) callersLoader() {
z.debug = (*zap.SugaredLogger).Debug
z.debugF = (*zap.SugaredLogger).Debugf
z.debugW = (*zap.SugaredLogger).Debugw
z.info = (*zap.SugaredLogger).Info
z.infoF = (*zap.SugaredLogger).Infof
z.infoW = (*zap.SugaredLogger).Infow
z.warn = (*zap.SugaredLogger).Warn
z.warnF = (*zap.SugaredLogger).Warnf
z.warnW = (*zap.SugaredLogger).Warnw
z.error = (*zap.SugaredLogger).Error
z.errorF = (*zap.SugaredLogger).Errorf
z.errorW = (*zap.SugaredLogger).Errorw
z.dPanic = (*zap.SugaredLogger).DPanic
z.dPanicF = (*zap.SugaredLogger).DPanicf
z.dPanicW = (*zap.SugaredLogger).DPanicw
z.panic = (*zap.SugaredLogger).Panic
z.panicF = (*zap.SugaredLogger).Panicf
z.panicW = (*zap.SugaredLogger).Panicw
z.fatal = (*zap.SugaredLogger).Fatal
z.fatalF = (*zap.SugaredLogger).Fatalf
z.fatalW = (*zap.SugaredLogger).Fatalw
}
func _defaultCore() Core {
return ConsoleWriterCore(true)
}