-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
sugared.go
55 lines (47 loc) · 1.73 KB
/
sugared.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
package logger
// SugaredLogger extends the base Logger interface with syntactic sugar, similar to zap.SugaredLogger.
type SugaredLogger interface {
Logger
// AssumptionViolation variants log at error level with the message prefix "AssumptionViolation: ".
AssumptionViolation(args ...interface{})
AssumptionViolationf(format string, vals ...interface{})
AssumptionViolationw(msg string, keyvals ...interface{})
// ErrorIf logs the error if present.
ErrorIf(err error, msg string)
// ErrorIfFn calls fn() and logs any returned error along with msg.
// Unlike ErrorIf, this can be deffered inline, since the function call is delayed.
ErrorIfFn(fn func() error, msg string)
}
// Sugared returns a new SugaredLogger wrapping the given Logger.
func Sugared(l Logger) SugaredLogger {
return &sugared{
Logger: l,
h: l.Helper(1),
}
}
type sugared struct {
Logger
h Logger // helper with stack trace skip level
}
// AssumptionViolation wraps Error logs with assumption violation tag.
func (s *sugared) AssumptionViolation(args ...interface{}) {
s.h.Error(append([]interface{}{"AssumptionViolation:"}, args...))
}
// AssumptionViolationf wraps Errorf logs with assumption violation tag.
func (s *sugared) AssumptionViolationf(format string, vals ...interface{}) {
s.h.Errorf("AssumptionViolation: "+format, vals...)
}
// AssumptionViolationw wraps Errorw logs with assumption violation tag.
func (s *sugared) AssumptionViolationw(msg string, keyvals ...interface{}) {
s.h.Errorw("AssumptionViolation: "+msg, keyvals...)
}
func (s *sugared) ErrorIf(err error, msg string) {
if err != nil {
s.h.Errorw(msg, "err", err)
}
}
func (s *sugared) ErrorIfFn(fn func() error, msg string) {
if err := fn(); err != nil {
s.h.Errorw(msg, "err", err)
}
}