-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
52 lines (44 loc) · 1.29 KB
/
logger.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
package errs
import "errors"
// Logger is an interface that can be implemented to log errors
type Logger interface {
Printf(format string, args ...any)
}
// LogDecisionMaker can be implemented by errors
// to decide if they should be logged.
// Use the package function ShouldLog to check
// if a wrapped error implements the interface
// and get the result of its ShouldLog method.
type LogDecisionMaker interface {
error
// ShouldLog decides if the error should be logged
ShouldLog() bool
}
// ShouldLog checks if the passed error unwraps
// as a LogDecisionMaker and returns the result
// of its ShouldLog method.
// If error does not unwrap to LogDecisionMaker
// and is not nil then ShouldLog returns true.
// A nil error results in false.
func ShouldLog(err error) bool {
if err == nil {
return false
}
var logDecisionMaker LogDecisionMaker
if errors.As(err, &logDecisionMaker) {
return logDecisionMaker.ShouldLog()
}
return true
}
// DontLog wraps the passed error as LogDecisionMaker
// so that ShouldLog returns false.
// A nil error won't be wrapped but returned as nil.
func DontLog(err error) error {
if err == nil {
return nil
}
return dontLog{err}
}
type dontLog struct{ error }
func (dontLog) ShouldLog() bool { return false }
func (e dontLog) Unwrap() error { return e.error }